2016-10-07 51 views
0

我的數據: -需要從排有同樣的數據消除空

UserId |ShopId |PlanName 
------------------------ 
241 | 17679 |NULL 
241 | 20037 |NULL 
241 | 20037 |440 

我需要的數據: -

UserId |ShopId |PlanName 
------------------------ 
241 | 17679 |NULL 
241 | 20037 |440 

這是我的查詢

select distinct so.UserId,so.ShopId, (select sa1.beatplanid from beatplansetting bps1 
inner join shopassign sa1 on bps1.beatplanid = sa1.beatplanid 
where 
so.shopid = bps1.shopid 
and 
sa.userid = sa1.userid 
) as PlanName from 
shopinandoutlog so 
left join beatplansetting bps on so.shopid = bps.shopid 
left join shopassign sa on so.userid = sa.userid and bps.beatplanid = sa.beatplanid 
where so.userid=241 
and 
convert(varchar,mobiletransactiondate,106)='01 oct 2016' 
and 
so.shopid in (20037,17679) 

回答

0

我想這就是你想要的:

select UserId, ShopId, MAX(PlanName) as PlanName 
from t 
group by UserId, ShopId; 

我不知道你的查詢與實際問題有什麼關係。您的查詢具有查詢或示例數據中未提及的多個表和列。