【发布时间】:2017-01-04 17:42:03
【问题描述】:
我有一个产生以下结果的脚本:
所需的结果集:
这是我使用的查询。
Select
SiteName,
(coalesce(country, '') + cast(row_number() over (partition by country order by country) as varchar(255))) Country,
Completed,
(Total - Completed-Deleted-Rejected) Remaining,
Deleted,
Total
from dbo.Statistics
where Date between '12/10/2016' and '12/29/2016'
and SiteName='pcltb.co.in'
union all
select
'Total',
'Total',
sum(Completed),
sum((Total - Completed-Deleted-Rejected)),
sum(Deleted),
sum(Total)
from dbo.Statistics
where Date between '12/10/2016' and '12/29/2016'
and SiteName='pcltb.co.in'
现在,为了优化查询,我尝试使用 ROLL UP 和 CUBE 但无法获得所需的结果。
PS:我想替换或替代查询中使用的 UNION。
请建议。
【问题讨论】:
-
请避免在显示结果时使用图像,将预期结果粘贴为文本,当前结果
-
SQL Server 是一个数据库管理系统,最适合存储和检索数据,对于数据展示,我们有其他报告工具,如 SSRS、Qlikview 等,使用适当的工具进行数据展示 SQL Server 不是为这个。
标签: sql sql-server optimization