通过查询将百分比列添加到简单的 t-SQL 组
Add a percentage column to a simple t-SQL group by query
我有这个简单的查询
select status,count(*) as [Count] from sms_deliverylog
where dt_log>'2015-03-23' and dt_log <'2015-03-24'
group by status with rollup
给出这个结果
status Count
ACCEPTD 33
DELIVRD 554
EXPIRED 2
PENDING 72
REJECTD 1
UNDELIV 2
NULL 664
- 如何添加基于计数的简单百分比列。
- 如何将上面的 NULL 重命名为 "Total"
谢谢
编辑:
请问是否有不涉及使用 CTE
的简单 t-sql 替代方案
不能称之为简单,但这是我的尝试:
SELECT
status,
count,
((CONVERT(DECIMAL, count)/(SELECT COUNT(*) FROM sms_deliverylog WHERE dt_log>'2015-03-23' and dt_log <'2015-03-24'))) * 100 AS '%'
FROM
(
SELECT
CASE
WHEN status IS NULL THEN 'Total'
ELSE status
END AS [status],
COUNT(*) AS [count]
FROM
sms_deliverylog
WHERE
dt_log>'2015-03-23' and dt_log <'2015-03-24'
GROUP BY STATUS WITH ROLLUP
) AS statuscounts
提琴手 Link: http://sqlfiddle.com/#!6/5549f/8/0
我有这个简单的查询
select status,count(*) as [Count] from sms_deliverylog
where dt_log>'2015-03-23' and dt_log <'2015-03-24'
group by status with rollup
给出这个结果
status Count
ACCEPTD 33
DELIVRD 554
EXPIRED 2
PENDING 72
REJECTD 1
UNDELIV 2
NULL 664
- 如何添加基于计数的简单百分比列。
- 如何将上面的 NULL 重命名为 "Total"
谢谢
编辑: 请问是否有不涉及使用 CTE
的简单 t-sql 替代方案不能称之为简单,但这是我的尝试:
SELECT
status,
count,
((CONVERT(DECIMAL, count)/(SELECT COUNT(*) FROM sms_deliverylog WHERE dt_log>'2015-03-23' and dt_log <'2015-03-24'))) * 100 AS '%'
FROM
(
SELECT
CASE
WHEN status IS NULL THEN 'Total'
ELSE status
END AS [status],
COUNT(*) AS [count]
FROM
sms_deliverylog
WHERE
dt_log>'2015-03-23' and dt_log <'2015-03-24'
GROUP BY STATUS WITH ROLLUP
) AS statuscounts
提琴手 Link: http://sqlfiddle.com/#!6/5549f/8/0