[英]How do i aggregate data from 2 columns referencing another table?
Using the data below, how do i get the sum of the cost of each incident based on 2 columns(crime_incidentid, similar_incidentid) in the listofincidents table. 使用以下数据,我如何基于事件列表中的2列(crime_incidentid,相似_事件ID)获取每个事件的成本总和。
create table crimeincidents
(
id int not null,
name varchar(20),
primary key (id)
);
create table listofincidents
(
id int not null,
crime_incidentid int not null,
similar_incidentid int not null,
cost_to_city decimal(8,2),
primary key (id),
FOREIGN KEY (crime_incidentid) REFERENCES crimeincidents(id),
FOREIGN KEY (similar_incidentid) REFERENCES crimeincidents(id)
);
insert into crimeincidents (id,name) values (1,'Burglary'),(2,'Theft'), (3, 'Grand theft auto');
insert into listofincidents (id, crime_incidentid, similar_incidentid, cost_to_city)
values (1, 1, 2, 900), (2, 2, 3, 800), (3, 3, 1, 1500.10), (4, 1, 3, 800.23);
I want to get an aggregate data similar to the table below.
---------------------------------------------------------------
id name | similarIncidentCost | crimeIncidentCost
1 Burglary | 1500.10 | 1700.23
2 Theft | 900 | 800
3 Grand Theft Auto | 1600.23 | 1500.1
For safest side you would need to do the join with separate result set containing for similarIncidentCost
and crimeIncidentCost
为了最安全起见,您需要使用单独的结果集进行
similarIncidentCost
, crimeIncidentCost
结果集包含similarIncidentCost
和crimeIncidentCost
select c.id, sm.similarIncidentCost, cr.crimeIncidentCost from crimeincidents c
inner join (
select c.id, sum(s.cost_to_city) similarIncidentCost
from crimeincidents c inner join listofincidents s
on s.similar_incidentid = c.id
group by c.id
) sm on sm.id = c.id
inner join (
select c.id, sum(cr.cost_to_city) crimeIncidentCost
from crimeincidents c inner join listofincidents cr
on cr.crime_incidentid = c.id
group by c.id
) cr on cr.id = c.id
In similar way, you could also explore via union all
同样,您也可以通过
union all
探索union all
select id, sum(case when type = 'sim' then cost_to_city else 0 end) similarIncidentCost,
sum(case when type = 'cr' then cost_to_city else 0 end) crimeIncidentCost
from
(
select c.id, s.cost_to_city, 'sim' as type
from crimeincidents c left join listofincidents s
on s.similar_incidentid = c.id
union all
select c.id, cr.cost_to_city, 'cr' as type
from crimeincidents c left join listofincidents cr
on cr.crime_incidentid = c.id
)t
group by id
you can use the following 您可以使用以下内容
with report as(
select c.id,c.name,sum(cost_to_city) as total,Type='crimeIncidentCost'
from crimeincidents c
inner join listofincidents l on l.crime_incidentid = c.id
group by c.id,c.name
union all
select c.id,c.name,sum(cost_to_city) as total,Type='similarIncidentCost'
from crimeincidents c
inner join listofincidents l on l.similar_incidentid = c.id
group by c.id,c.name)
select id,name, [similarIncidentCost],[crimeIncidentCost]
from report r
pivot( sum(total) for Type in([similarIncidentCost],[crimeIncidentCost])) p
Result 结果
id name crime_incidentid similar_incidentid
1 Burglary 1700.23 1500.1
3 Grand theft auto 1500.1 1600.23
2 Theft 800 900
Hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.