简体   繁体   English

如何汇总2列中引用另一个表的数据?

[英]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 为了最安全起见,您需要使用单独的结果集进行similarIncidentCostcrimeIncidentCost结果集包含similarIncidentCostcrimeIncidentCost

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

Here a working demo 这是一个工作演示

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.

相关问题 如何汇总来自引用另一张表的2列中的数据,并获取过去3个月的月度总计? - How do i aggregate data from 2 columns referencing another table and also get the monthly totals for the past 3 months? 如何通过汇总另一个表中另一个列的数据来创建派生列 - how to I create a derived column by aggregate data from another column in another table MySQL-如何根据另一个表中的列排序 - MySQL - How do I order based on columns from another table 如何使用同一表中另一行的数据在UPDATE中使用聚合函数? - How would I use aggregate function in UPDATE using data from another row in same table? 如何将数据从一个表复制到另一表? - How do i copy data from one table to another table? 如何从 MySQL 中的另一个表中获取数据? - How do I get data from another table in MySQL? 如何在MySQL中将数据从一个表移到另一个表? - How do I move data from one table into another in MySQL? 如何在mysql中将数据从一个表插入到另一个表? - How do I insert data from one table to another in mysql? 如何通过计算将数据从一个表获取到另一个表? - How do I get a data from one table to another but with calculations? 如何轻松地从另一个表的多列插入表中的数据? - How can I easily INSERT data in a table from multiple columns from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM