在情况下使用子查询别名
Using subquery alias in case
我很难在我的 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)
知道我该如何解决吗?
提前致谢。
您可以为此使用 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
我将子查询移动到 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;
您可以使用以下方法。
您可以执行和测试查询 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
我很难在我的 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)
知道我该如何解决吗?
提前致谢。
您可以为此使用 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
我将子查询移动到 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;
您可以使用以下方法。
您可以执行和测试查询 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