我在T-SQL和SRSS上遇到了以下问题。
情况:电子邮件票务系统。 每个传入的邮件都有一个ID。此外,如果用户是我们数据库中的VIP,则每封电子邮件都会获得“VIP”-Flag。我现在要做的是获得以下4个值。
到目前为止,我收到了以下声明:
WITH T1 AS (
SELECT
increcsystem.registrationtime,
count(*) AS [Total],
increcsystem.viplevel,
FROM
increcsystem
WHERE
GROUP BY increcsystem.registrationtime, increcsystem.viplevel
)
Select
(Select count(*) From T1) AS Total,
(Select count(*) From T1 WHERE viplevel = '100629') AS VIP_Tickets,
registrationtime AS Date
FROM T1
但我无法确定百分比:(
有人能看到错误吗?
最好的问候, PhiBu
答案 0 :(得分:1)
这是一个应该有效的简单查询:
select registrationtime
, TotalMails = count(1)
, VIPMails = sum(case when viplevel = '100629' then 1 else 0 end)
, VIPPercent = sum(case when viplevel = '100629' then 1.0 else 0.0 end) / count(1)
from increcsystem
group by registrationtime
order by registrationtime
这是显示工作查询的SQL Fiddle demo。