我在plsql
中有以下表格Company | StoreNum | Sku | Units | Dollars
首先查询:
select company, storeNum, sku, SUM(units) AS Units, SUM(dollars) AS dollars
FROM Sales
GROUP BY company, storeNum, sku
第二个查询:
select COUNT(DISTINCT company), COUNT(DISTINCT(storeNum), sku,
SUM(units) AS Units, SUM(dollars) AS dollars
FROM Sales
GROUP BY sku
HAVING COUNT(DISTINCT company) >= 2
第二个查询获得sku
的总计,其中至少有两个companies
正在销售sku
。我需要一种方法来找出哪家公司,商店组成这个清单?
答案 0 :(得分:0)
您可以使用LISTAGG
获取以逗号分隔的公司列表和商店列表,如下所示。
SELECT LISTAGG(company,',') WITHIN GROUP (
ORDER BY company) company_list,
LISTAGG(StoreNum,',') WITHIN GROUP (
ORDER BY storeNum) Store_list,
COUNT(DISTINCT company),
COUNT(DISTINCT storeNum),
sku,
SUM(units) AS Units,
SUM(dollars) AS dollars
FROM Sales
GROUP BY sku
HAVING COUNT(DISTINCT company) >= 2;
注意:这适用于Oracle 11g
及更高版本。