如何将查询转换为具有不同列名的单行。
Select count(distinct accountid) as x
from table1
where active = 1
and expiredate >= GetDate()
and acceptedon is not null
union
Select count(distinct accountid) as x
from table1
where active = 1
and expiredate <= GetDate()
and acceptedon is not null
我明白了
x
5
4
我想要
x y
5 4
答案 0 :(得分:3)
您想要简单的条件聚合:
Select count(distinct case when expiredate >= GetDate() then accountid end) as x,
count(distinct case when expiredate <= GetDate() then accountid end) as y
from table1
where active = 1 and acceptedon is not null;