SQL Server - Select using CASE inside FROM -
i'm trying select
using case
clause inside from
didn't figure how can it.
actually, how select like:
select a.userid, isnull(sent, 0) sent, isnull(received,0) received (select tt1.userid userid, sum(totalusers) sent #tabletemp1 tt1 inner join #tabletemp2 tt2 on tt1.userid = tt2.userid tt1.status = -1 group tt1.userid) left join .....
i need change statement inside () this:
select a.userid, isnull(sent, 0) sent, isnull(received, 0) received (case tt1.exportdate when null select tt1.userid userid, totalsent sent sentdata inner join dataconsolidated dc on cde.userid = dc.userid else select tt1.userid userid, sum(totalusers) sent #tabletemp1 tt1 inner join #tabletemp2 tt2 on tt1.userid = tt2.userid tt1.status = -1 group tt1.userid end) left join ......
i know don't have 'tt1' when call case i'm kinda lost in sql clauses.
any appreciated.
if in stored procedure, recommend putting logic in if/else block. however, if doing inline query, might better-off running both sets (with conditions result in empty set) , union results. this:
select a.userid, isnull(sent,0) sent, isnull(received,0) received ( select tt1.userid userid, totalsent sent sentdata inner join dataconsolidated dc on cde.userid = dc.userid tt1.exportdate null union select tt1.userid userid, sum(totalusers) sent #tabletemp1 tt1 inner join #tabletemp2 tt2 on tt1.userid = tt2.userid tt1.status = -1 , tt1.exportdate not null group tt1.userid ) left join ......
Comments
Post a Comment