使用 COALESCE 添加列名称

问题描述 投票:0回答:1

我有这个存储过程,我想将列名添加到我得到的值中。我应该在哪里添加

作为“报纸总数”

(或我必须添加的任何内容)如果我使用COALESCE

SELECT COALESCE ((SELECT 
  count(distinct Appendage.AppendageName) as "TotalNewspapers" 
  FROM Edition
   inner join SourceInformation on (SourceInformation.SourceInformationID =  Edition.SourceInformationID)
   left join Appendage on (Appendage.AppendageID = Edition.AppendageID)
   inner join Pages on (Edition.EditionID = Pages.EditionID)
  where 
    Edition.publishdate >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
    and Edition.publishdate <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)+1 
    and Pages.PullBy is null
    and Edition.FinishDate is null
group by Appendage.AppendageName, SourceInformation.SourceInformationName, Pages.PullBy, Edition.FinishDate, Appendage.AppendageID
having count(Pages.PullBy) > 1) , 0);
sql sql-server coalesce
1个回答
2
投票

您可以使用 ISNULLCOALESCE:

SELECT ISNULL(( --OR COALESCE
    SELECT count(distinct a.AppendageName) as [TotalNewspapers]
    FROM Edition e
    inner join SourceInformation s
        on (s.SourceInformationID =  e.SourceInformationID)
    left join Appendage a
        on (a.AppendageID = e.AppendageID)
    inner join Pages p
        on (e.EditionID = p.EditionID)
    where 
        e.publishdate >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
        and e.publishdate <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)+1 
        and p.PullBy is null
        and e.FinishDate is null
    group by a.AppendageName, s.SourceInformationName, p.PullBy, e.FinishDate, a.AppendageID
    having count(p.PullBy) > 1
),0) as [TotalNewspapers]

我删除了子查询,您所需的一切都可以在一个 select 语句中完成。还要添加别名

© www.soinside.com 2019 - 2024. All rights reserved.