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

标签 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。

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

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

最佳答案

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

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

关于SQL Case When - 从输出中删除 'empty' 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072631/

相关文章:

javafx - 从 GridPane 中删除一行

sql-server - SQL 级联删除多列

sql - 错误 - 未在预期位置找到关键字

mysql - 用另一行的平均值更新一行

html - 按最后一个元素填充行中的剩余空间

sql - Microsoft t sql 如何在用户定义的函数中声明临时变量?

entity-framework - Entity Framework 'Update Model from Database'在基类和子类之间重新创建关联

mysql - 内部 JOIN 返回重复行

sql - SQL Azure 上的计划任务

python - 将具有较少值的行添加到 pandas 数据框中