SQL 服务器查询汇总数据
SQL Server query to roll up data
我有以下 SQL 声明。它连接了三个表:Person
、Deliverable
和 DeliverableActions
select
p.first_name, p. last_name, d.title, da.type
from
Deliverable d
right join
Person p on d.person_responsible_id = p.id
right join
DeliverableAction da on da.DeliverableID = d.id
where
d.date_deadline >= @startDate and
d.date_deadline <= @endDate
order by
d.title
结果如下:
first_name | last_name | title | type
-----------+-------------+--------------+------
Joe | Kewl | My_Report_1 | 2
Joe | Kewl | My_Report_1 | 3
Joe | Kewl | My_Report_1 | 1
Sly | Foxx | Other_Rep_1 | 1
Sly | Foxx | Other_Rep_1 | 2
我的目标结果是得到下表:
first_name | last_name | title | type_1 | type_2 | type_3 | type_4
-----------+------------+--------------+--------+--------+--------+---------
Joe | Kewl | My_report_1 | 1 | 1 | 1 | 0
Sly | Foxx | Other_Rep_1 | 1 | 1 | 0 | 0
不幸的是,我不知道用什么词来形容我在做什么。我搜索了 'grouping' 和 'aggregation',但没有找到答案,所以我将其发布到社区。预先感谢您的帮助。
你可以用case based aggregation
或者你也可以用pivot
select p.first_name,
p. last_name,
d.title,
sum(case when da.type = 1 then 1 else 0 end) as type_1,
sum(case when da.type = 2 then 1 else 0 end) as type_2,
sum(case when da.type = 3 then 1 else 0 end) as type_3,
sum(case when da.type = 4 then 1 else 0 end) as type_4,
from Deliverable d
right join Person p on d.person_responsible_id = p.id
right join DeliverableAction da on da.DeliverableID = d.id
where d.date_deadline >= @startDate and
d.date_deadline <= @endDate
group by p.first_name, p.last_name, d.title
select
first_name, last_name, title,
sum(case when type = 1 then 1 else 0 end) as type_1
from
(
select p.first_name, p. last_name, d.title, da.type from Deliverable d
right join Person p on d.person_responsible_id = p.id
right join DeliverableAction da on da.DeliverableID = d.id
where d.date_deadline >= @startDate and
d.date_deadline <= @endDate
) as a
group by first_name, last_name, title
您正在寻找 PIVOT
如果您使用的是 SQL Server 2008+,它具有 http://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx
中所述的数据透视功能
基本上,您可以这样写(抱歉,我只是粘贴了引用的 link 中的示例,但这应该能让您有所了解):
-- Pivot table with one row and five columns
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
我有以下 SQL 声明。它连接了三个表:Person
、Deliverable
和 DeliverableActions
select
p.first_name, p. last_name, d.title, da.type
from
Deliverable d
right join
Person p on d.person_responsible_id = p.id
right join
DeliverableAction da on da.DeliverableID = d.id
where
d.date_deadline >= @startDate and
d.date_deadline <= @endDate
order by
d.title
结果如下:
first_name | last_name | title | type
-----------+-------------+--------------+------
Joe | Kewl | My_Report_1 | 2
Joe | Kewl | My_Report_1 | 3
Joe | Kewl | My_Report_1 | 1
Sly | Foxx | Other_Rep_1 | 1
Sly | Foxx | Other_Rep_1 | 2
我的目标结果是得到下表:
first_name | last_name | title | type_1 | type_2 | type_3 | type_4
-----------+------------+--------------+--------+--------+--------+---------
Joe | Kewl | My_report_1 | 1 | 1 | 1 | 0
Sly | Foxx | Other_Rep_1 | 1 | 1 | 0 | 0
不幸的是,我不知道用什么词来形容我在做什么。我搜索了 'grouping' 和 'aggregation',但没有找到答案,所以我将其发布到社区。预先感谢您的帮助。
你可以用case based aggregation
或者你也可以用pivot
select p.first_name,
p. last_name,
d.title,
sum(case when da.type = 1 then 1 else 0 end) as type_1,
sum(case when da.type = 2 then 1 else 0 end) as type_2,
sum(case when da.type = 3 then 1 else 0 end) as type_3,
sum(case when da.type = 4 then 1 else 0 end) as type_4,
from Deliverable d
right join Person p on d.person_responsible_id = p.id
right join DeliverableAction da on da.DeliverableID = d.id
where d.date_deadline >= @startDate and
d.date_deadline <= @endDate
group by p.first_name, p.last_name, d.title
select
first_name, last_name, title,
sum(case when type = 1 then 1 else 0 end) as type_1
from
(
select p.first_name, p. last_name, d.title, da.type from Deliverable d
right join Person p on d.person_responsible_id = p.id
right join DeliverableAction da on da.DeliverableID = d.id
where d.date_deadline >= @startDate and
d.date_deadline <= @endDate
) as a
group by first_name, last_name, title
您正在寻找 PIVOT
如果您使用的是 SQL Server 2008+,它具有 http://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx
中所述的数据透视功能基本上,您可以这样写(抱歉,我只是粘贴了引用的 link 中的示例,但这应该能让您有所了解):
-- Pivot table with one row and five columns
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;