简体   繁体   English

SQL将一个表的多条记录合并成另一表的多列

[英]SQL combine multiple records of one table into multiple columns of another table

I am working in SQL and have a destination table:我正在使用 SQL 并且有一个目标表:

Event ID (key) |   Road    |     total count  |     motorcycles   |    cars    |   trucks   |   bus

And I have a record like table:我有一个类似表的记录:

[Event ID    |    mode of transport      |       count
1           |      bus                  |          3
1           |      cars                 |         20
1           |      trucks               |          2 
1           |      motorcycles          |          5
2           |      bus                  |          1
2           |      cars                 |          12 
2           |      motorcycles          |          1][1]

(combination of Event ID and mode of transport combination is unique) (Event ID和交通方式组合的组合是唯一的)

How do I combine the data from the second table into the first easily as result:我如何轻松地将第二个表中的数据合并到第一个表中:

Event ID (key) |   Road    |     total count    |     motorcycles   |    cars    |   trucks   |   bus
1              |     ...   |      ...           |        5          |     20     |    2       |    3
2              |     ...   |      ...           |        1          |     12     |            |    1  

在此处输入图片说明

I am looking for a way that can incorporate the record data from the second table in one SQL structure / statement.我正在寻找一种可以将第二个表中的记录数据合并到一个 SQL 结构/语句中的方法。 Thank you!谢谢!

You can use conditional aggregation:您可以使用条件聚合:

select eventid, road,
       sum(count) as total_count,
       sum(case when mode = 'motorcycles' then count end) as cnt_motorcycles,
       sum(case when mode = 'car' then count end) as cnt_car,
       sum(case when mode = 'bus' then count end) as cnt_bus,
       sum(case when mode = 'truck' then count end) as cnt_truck
from t
group by eventid, road;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用SQL将另一张表中的多行值组合为一行的多列吗? - Can I combine values from multiple rows in another table into multiple columns of one row using SQL? 按多列显示一个表中的记录,该表不在另一个表中 - Display records from one table which is not in another table by multiple columns 根据多个列查找一个表中存在但另一个表中不存在的记录 - Find records that exists in one table but not another based on multiple columns 根据第三个表中的字段将一个表中的多个记录合并到另一个表中的一个字段中 - Combine multiple records in a table into one field in another table based on fields in a third table SQL将一个表中的多行插入另一个表的列中 - SQL insert multiple rows from one table into columns on another table 具有多个具有匹配列的记录的SQL查询表 - SQL Query table with multiple records with matching columns SQL将一张表的多行合并为多列的一行 - SQL Combine multiple rows from one table into one row with multiple columns SQL:将一个表中的 2 列与另一表中的 1 列进行比较 - SQL: combine 2 columns in one table to compare with 1 column in another table 将多个列从一个SQL表复制到另一个 - Copying multiple columns from one SQL table to another 根据多个条件,将记录从一个表中的特定列插入到另一表中 - inserting records from specific columns in one table to another table depending on multiple criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM