SQL Query将结果集值添加为列值

时间:2015-12-22 22:09:07

标签: sql

我想在不创建表格的情况下将以下查询添加为列值。

Select 'NetworkKey' as AuthKey
Select count(NetworkSK) as Totalcount from EDW.Fact.AuthorizationRequest
Select count(*) as NUllcount from EDW.Fact.AuthorizationRequest where NetworkSK is NULL
Select count(*) as NotNullcount from EDW.Fact.AuthorizationRequest where NetworkSK is Not NULL

我的结果应该是这样的,而不是在物理上创建一个表...

AuthKey         Totalcount      Nullcount                      NotNullCount

NetworkKey         100               5                        95

1 个答案:

答案 0 :(得分:3)

  

你想要这样做,因为这将解决你的问题。

SELECT 'NetworkKey' AS AuthKey,
        COUNT(*) AS TotalCount,
        SUM(CASE WHEN NetworkSK IS NULL THEN 1 ELSE 0 END) AS NUllcount,
        SUM(CASE WHEN NetworkSK IS NOT NULL THEN 1 ELSE 0 END) AS NotNullcount
FROM EDW.Fact.AuthorizationRequest

节日快乐。