我的数据: -
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 :(得分:0)
我认为这可以满足您的需求:
select UserId, ShopId, MAX(PlanName) as PlanName
from t
group by UserId, ShopId;
我不知道您的查询与实际问题有什么关系。您的查询具有查询或示例数据中未提及的多个表和列。