SQL Case When - 从输出中删除 'empty' 行

SQL Case When - delete 'empty' rows from output

我在编写查询时遇到困难。查询如下所示:

SELECT case when Year(LOAN_START_DATE) = 2010 then 
            max(LOAN_RES_BANK_CODE) else 0 end as '2010',
       case when Year(LOAN_START_DATE) = 2011 then
            max(LOAN_RES_BANK_CODE) else 0 end as '2011',
       case when Year(LOAN_START_DATE) = 2012 then 
            max(LOAN_RES_BANK_CODE) else 0 end as '2012',
       case when Year(LOAN_START_DATE) = 2013 then 
            max(LOAN_RES_BANK_CODE) else 0 end as '2013',
       case when Year(LOAN_START_DATE) = 2014 then 
            max(LOAN_RES_BANK_CODE) else 0 end as '2014',
       case when Year(LOAN_START_DATE) = 2015 then 
            max(LOAN_RES_BANK_CODE) else 0 end as '2015'
from LLOAN l
inner join LACMSTR c on c.ACCT_NUMBER = l.LOAN_ACCT_1 and c.SourceID = l.SourceID
where l.SourceID = 1
and c.ACCT_NUMBER = 1065
group by LOAN_START_DATE

这给出了以下输出:

2010  2011  2012  2013  2014  2015
----------------------------------
 0     0     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0
 0     0     0     0     0     0
 1     0     0     0     0     0
 0     0     3     0     0     0
 0     0     0     2     0     0
 0     0     0     2     0     0
 0     0     0     0     2     0

我希望输出只有一行,如下所示:

2010  2011  2012  2013  2014  2015
----------------------------------
 1     0     3     2     2     0

我找到了一种方法,但它确实是一个丑陋且缓慢(相对而言)的查询:

select (select ISNULL(max(LOAN_RES_BANK_CODE),0)
             from PfeDs.dbo.LLOAN t1, pfeds.dbo.LACMSTR t2
             where t1.SourceID = t2.SourceID
             and t2.SourceID = 1
             and ACCT_NUMBER = LOAN_ACCT_1
             and ACCT_NUMBER = 1065
             and year(LOAN_START_DATE) = 2010) as '2010',
             (select ISNULL(max(LOAN_RES_BANK_CODE),0)
             from PfeDs.dbo.LLOAN t1, pfeds.dbo.LACMSTR t2
             where t1.SourceID = t2.SourceID
             and t2.SourceID = 1
             and ACCT_NUMBER = LOAN_ACCT_1
             and ACCT_NUMBER = 1065
             and year(LOAN_START_DATE) = 2011) as '2011',
             (select ISNULL(max(LOAN_RES_BANK_CODE),0)
             from PfeDs.dbo.LLOAN t1, pfeds.dbo.LACMSTR t2
             where t1.SourceID = t2.SourceID
             and t2.SourceID = 1
             and ACCT_NUMBER = LOAN_ACCT_1
             and ACCT_NUMBER = 1065
             and year(LOAN_START_DATE) = 2012) as '2012',
             (select ISNULL(max(LOAN_RES_BANK_CODE),0)
             from PfeDs.dbo.LLOAN t1, pfeds.dbo.LACMSTR t2
             where t1.SourceID = t2.SourceID
             and t2.SourceID = 1
             and ACCT_NUMBER = LOAN_ACCT_1
             and ACCT_NUMBER = 1065
             and year(LOAN_START_DATE) = 2013) as '2013',
             (select ISNULL(max(LOAN_RES_BANK_CODE),0)
             from PfeDs.dbo.LLOAN t1, pfeds.dbo.LACMSTR t2
             where t1.SourceID = t2.SourceID
             and t2.SourceID = 1
             and ACCT_NUMBER = LOAN_ACCT_1
             and ACCT_NUMBER = 1065
             and year(LOAN_START_DATE) = 2014) as '2014',
             (select ISNULL(max(LOAN_RES_BANK_CODE),0)
             from PfeDs.dbo.LLOAN t1, pfeds.dbo.LACMSTR t2
             where t1.SourceID = t2.SourceID
             and t2.SourceID = 1
             and ACCT_NUMBER = LOAN_ACCT_1
             and ACCT_NUMBER = 1065
             and year(LOAN_START_DATE) = 2015) as '2015'

SQL不是我的强项。我应该以何种方式更改我的原始查询以获得我正在寻找的结果。我正在使用 SQL Server 2014。

--------------------编辑-----------------

Jarlh 给出了很好的解决方案,下面的查询给出了正确的输出:

select max(J10) as '2010', max(J11) as '2011', max(J12) '2012', max(J13) as '2013', max(J14) as '2014', max(J15) as '2015'
from (
SELECT case when Year(LOAN_START_DATE) = 2010 then max(LOAN_RES_BANK_CODE) else 0 end as J10,
case when Year(LOAN_START_DATE) = 2011 then max(LOAN_RES_BANK_CODE) else 0 end as J11,
case when Year(LOAN_START_DATE) = 2012 then max(LOAN_RES_BANK_CODE) else 0 end as J12,
case when Year(LOAN_START_DATE) = 2013 then max(LOAN_RES_BANK_CODE) else 0 end as J13,
case when Year(LOAN_START_DATE) = 2014 then max(LOAN_RES_BANK_CODE) else 0 end as J14,
case when Year(LOAN_START_DATE) = 2015 then max(LOAN_RES_BANK_CODE) else 0 end as J15
from pfeds.dbo.LLOAN l
inner join PfeDs.dbo.LACMSTR c on c.ACCT_NUMBER = l.LOAN_ACCT_1 and c.SourceID = l.SourceID
where l.SourceID = 1
and c.ACCT_NUMBER = 1065
group by LOAN_START_DATE) as test

使用原始查询作为派生 table,然后 MAX 列:

select MAX('2010'), ... 
from (
SELECT case when Year(LOAN_START_DATE) = 2010 then 
            max(LOAN_RES_BANK_CODE) else 0 end as '2010',
...
)

如果您想隐藏所有值为 0 的行,为什么不尝试在 where 子句中添加其中一个?

and max(LOAN_RES_BANK_CODE) > 0

and LOAN_RES_BANK_CODE >0