SQL案例时 - 从输出中删除“空”行

时间:2015-05-06 09:23:38

标签: sql row sql-server-2014 case-when

我正在编写一个我正在写的查询有困难。查询如下所示:

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。

--------------------- ------------------ EDIT

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

2 个答案:

答案 0 :(得分:2)

将原始查询用作派生表,然后MAX列:

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

答案 1 :(得分:1)

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

and max(LOAN_RES_BANK_CODE) > 0

and LOAN_RES_BANK_CODE >0