简体   繁体   English

在简单数据库中组合来自多行的数据

[英]Combining Data from Multiple Rows in Simple Database

I am new to using SQL.我是使用 SQL 的新手。 I have constructed the following simple database, which has the ID of music albums and the number of copies bought.我构建了以下简单的数据库,其中包含音乐专辑的 ID 和购买的份数。

在此处输入图像描述

In the album_ID column, there are two pieces of data with ID 1. They have quantity_bought values of 10 and 4. Is it possible to combine these two rows into one row which has ID 1 and a total quantity_bought value of 14?album_ID列中,有两条ID 为1 的数据,它们的quantity_bought值为10 和4。是否可以将这两行合并为ID 为1 且总quantity_bought值为14 的一行?

WITH
-- your input
sold(album_id,qty_bought) AS (
          SELECT 1,10
UNION ALL SELECT 2, 5
UNION ALL SELECT 1, 4
)
SELECT
  album_id
, SUM(qty_bought) AS quantity_bought
FROM sold
GROUP BY album_id;
album_id|quantity_bought
       1|             14
       2|              5

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM