如何对重复值进行分组但保留sqlite中的其他列

问题描述 投票:0回答:1

我已将表格连接在一起

SELECT od.EmployeeID, nv.LastName, nv.FirstName, od.OrderDate, od.ShipperID, od.OrderID, prod.ProductName, ord.Quantity  from Orders as od
INNER JOIN Employees as nv
ON od.EmployeeID = nv.EmployeeID
INNER JOIN Shippers as ship
ON od.ShipperID = ship.ShipperID
INNER JOIN OrderDetails as ord
ON od.OrderID = ord.OrderID
INNER JOIN Products as prod
on prod.ProductID = ord.ProductID
WHERE od.EmployeeID = '8' and date(od.OrderDate) >= date('1997-01-01') and ship.ShipperID = '2'

结果:

enter image description here

如果您使用命令“GROUP BY od.OrderID”

enter image description here

但我想要的结果是只有“OrderID”列被分组。

ProductName 列将完整显示(以逗号分隔),如下所示:

Chang、Gustafs Knäckebröd、乔瓦尼马苏里拉奶酪

sqlite
1个回答
0
投票

使用

GROUP_CONCAT()
并通过
OrderID
聚合:

SELECT od.OrderID, GROUP_CONCAT(prod.ProductName) AS products
FROM Orders AS od
INNER JOIN OrderDetails AS ord
    ON od.OrderID = ord.OrderID
INNER JOIN Products AS prod
    ON prod.ProductID = ord.ProductID
GROUP BY od.OrderID;
© www.soinside.com 2019 - 2024. All rights reserved.