难以在 case 语句中给出别名
difficulty giving aliases inside case statement
我有一个查询,它给我的输出如下面的屏幕截图所示。
select
a.storeId, b.district, b.region,
count (case when a.photoAppImage is null then 1 end) as via_u,
count (case when a.photoAppImage =1 then 1 end) as via_p
from
used_listings_V2 a
left outer join
locations b on a.storeId = b.storeID
where
convert(date, a.imageUpdateDate) = convert(date, GETDATE()-1)
group by
a.storeId, b.district, b.region
已编辑
我想将我在一行中得到的两个不同列中的计数分开。具有空 photoAppImage 的所有记录的计数应显示在“Via_u”下方,而具有 1 个 photoAppImage 的记录的计数应显示在“Via_p”下方。预期的输出将如下所示:
基本上我认为我很难在 case 语句中给出别名。有人可以帮我解决这个问题吗?
使用:
Count(case when a.photoappimage is null then 1 end) as via_u,
Count(case when a.photoappimage is null then 1 end)/100. as via_u_pct,
Count(case when a.photoappimage=1 then 1 end ) as via_p,
Count(case when a.photoAppImage is null or a.photoAppImage=1 then 1 end) as via_u_or_p
更新:合并所有(并使用子查询来简化 pct 计算):
select
storeId,
district,
region,
via_u,
via_p,
via_u/100./(via_u+via_p) as via_u_pct
from (
select
a.storeId, b.district, b.region,
count (case when a.photoAppImage is null then 1 end) as via_u,
count (case when a.photoAppImage =1 then 1 end) as via_p
from
used_listings_V2 a
left outer join
locations b on a.storeId = b.storeID
where
convert(date, a.imageUpdateDate) = convert(date, GETDATE()-1)
group by
a.storeId, b.district, b.region
) Step1
我有一个查询,它给我的输出如下面的屏幕截图所示。
select
a.storeId, b.district, b.region,
count (case when a.photoAppImage is null then 1 end) as via_u,
count (case when a.photoAppImage =1 then 1 end) as via_p
from
used_listings_V2 a
left outer join
locations b on a.storeId = b.storeID
where
convert(date, a.imageUpdateDate) = convert(date, GETDATE()-1)
group by
a.storeId, b.district, b.region
已编辑
我想将我在一行中得到的两个不同列中的计数分开。具有空 photoAppImage 的所有记录的计数应显示在“Via_u”下方,而具有 1 个 photoAppImage 的记录的计数应显示在“Via_p”下方。预期的输出将如下所示:
基本上我认为我很难在 case 语句中给出别名。有人可以帮我解决这个问题吗?
使用:
Count(case when a.photoappimage is null then 1 end) as via_u,
Count(case when a.photoappimage is null then 1 end)/100. as via_u_pct,
Count(case when a.photoappimage=1 then 1 end ) as via_p,
Count(case when a.photoAppImage is null or a.photoAppImage=1 then 1 end) as via_u_or_p
更新:合并所有(并使用子查询来简化 pct 计算):
select
storeId,
district,
region,
via_u,
via_p,
via_u/100./(via_u+via_p) as via_u_pct
from (
select
a.storeId, b.district, b.region,
count (case when a.photoAppImage is null then 1 end) as via_u,
count (case when a.photoAppImage =1 then 1 end) as via_p
from
used_listings_V2 a
left outer join
locations b on a.storeId = b.storeID
where
convert(date, a.imageUpdateDate) = convert(date, GETDATE()-1)
group by
a.storeId, b.district, b.region
) Step1