在案例中使用子查询别名

时间:2017-10-24 05:52:46

标签: sql sql-server subquery case alias

我很难在我的CASE之一中使用子查询别名,花了我的时间在文档中后,我无法自己找到解决方案。

 SELECT 
    t1.cardcode as Kundennummer,
    t1.cardname,
    t3.groupname,

    (select 
        sum(t11.doctotal)as sum24  
    from 
        oinv t11 
    where 
        t11.cardcode=t1.cardcode
        and t11.docduedate > dateadd(month, -24, getdate()) and t11.docduedate < dateadd(month, -12, getdate())
    )as Umsatz24,

    (select 
        sum(t10.doctotal) as sum12 
    from 
        oinv t10 
    where 
        t10.cardcode=t1.cardcode
        and t10.docduedate > dateadd(month, -12, getdate())
    )as Umsatz12,
    case
        when t3.groupname = 'C' and Umsatz12 = 0
            then 'Dummy 0'
        when t3.groupname = 'C' and Umsatz12 > 0
            then 'Dummy 15'
        when t3.groupname = 'B' and Umsatz24 = 0
            then 'Dummy 15'
        else
            'Dummy 15/30?'
    end as 'Dummy'

from 
(... some code)

我知道如何解决它?

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以使用CTE。

;WITH T AS
(
    SELECT 
        t1.cardcode as Kundennummer,
        t1.cardname,
        t3.groupname,

        (select 
            sum(t11.doctotal)as sum24  
        from 
            oinv t11 
        where 
            t11.cardcode=t1.cardcode
            and t11.docduedate > dateadd(month, -24, getdate()) and t11.docduedate < dateadd(month, -12, getdate())
        )as Umsatz24,

        (select 
            sum(t10.doctotal) as sum12 
        from 
            oinv t10 
        where 
            t10.cardcode=t1.cardcode
            and t10.docduedate > dateadd(month, -12, getdate())
        )as Umsatz12
    from 
    (... some code)
)
SELECT *,
 case
        when groupname = 'C' and Umsatz12 = 0
            then 'Dummy 0'
        when groupname = 'C' and Umsatz12 > 0
            then 'Dummy 15'
        when groupname = 'B' and Umsatz24 = 0
            then 'Dummy 15'
        else
            'Dummy 15/30?'
    end as 'Dummy'
 FROM T 

答案 1 :(得分:1)

我将子查询移动到FROM子句,并通过使用条件聚合使这一个查询而不是两个:

select 
  t1.cardcode as kundennummer,
  t1.cardname,
  t3.groupname,
  umsatz.umsatz24,
  umsatz.umsatz12,
  case
    when t3.groupname = 'C' and umsatz.umsatz12 = 0 then 'Dummy 0'
    when t3.groupname = 'C' and umsatz.umsatz12 > 0 then 'Dummy 15'
    when t3.groupname = 'B' and umsatz.umsatz24 = 0 then 'Dummy 15'
    else                                                 'Dummy 15/30?'
  end as dummy
from  (... some code)
left join
(
  select 
    cardcode, 
    sum(case when oinv.docduedate > dateadd(month, -24, getdate()) 
              and oinv.docduedate < dateadd(month, -12, getdate())
             then oinv.doctotal end) as umsatz24 
    sum(case when oinv.docduedate > dateadd(month, -12, getdate())
             then oinv.doctotal end) as umsatz12 
  from oinv 
  group by cardcode
) umsatz on umsatz.cardcode = t1.cardcode;

答案 2 :(得分:1)

您可以使用以下方法。

您可以执行并测试查询here

select cc.CID,cc.CustomerName,cc.ContactName,cc.City,cc.Umsatz24,cc.Umsatz12,
 case
        when cc.City = 'London' and cc.Umsatz12 = 0
            then 'Dummy 0'
        when cc.City = 'London' and cc.Umsatz12 > 0
            then 'Dummy 15'
        when cc.City = 'Paris' and cc.Umsatz24 = 0
            then 'Dummy 15'
        else
            'Dummy 15/30?'
    end as 'Dummy'
from (
SELECT 
    t1.CustomerID as CID,
    t1.CustomerName,
    t1.ContactName,
    t1.City,
    (select 
        sum(t11.CustomerID)as sum24  
    from 
        Customers t11 
    where 
        t11.CustomerID=t1.CustomerID
        and t11.City = 'Paris'
    )as Umsatz24,

    (select 
        sum(t10.CustomerID) as sum12 
    from 
        Customers t10 
    where 
        t10.CustomerID=t1.CustomerID
        and t10.City = 'London'
    )as Umsatz12
 from Customers t1
 ) cc