具有2个条件的MySQL交叉表聚合
作者:互联网
我有一个创建交叉表的查询.结果是branda的txn_id计数,以及brandb的txn_id计数.
txn_id不是唯一的.这是交易表的一个例子:
txn_id | nationality_id | sku | sales | units
1 | 1 | 1 | 20 | 2
1 | 1 | 2 | 15 | 1
2 | 4 | 1 | 20 | 2
3 | 2 | 1 | 10 | 1
4 | 3 | 2 | 15 | 1
5 | 4 | 1 | 10 | 1
还有2个表(产品) – (sku,品牌,产品名称)和(国籍) – (nationality_id,国籍).
我想添加第三列,让我获得购买BOTH品牌的txn_id数量
输出应该是
nationality | branda | brandb | combined
1 | 1 | 1 | 1
2 | 1 | 0 | 0
3 | 0 | 1 | 0
4 | 2 | 0 | 0
当前查询.
SELECT
nationalities.nationality,
COUNT((CASE brand WHEN 'branda' THEN txn_id ELSE NULL END)) AS branda,
COUNT((CASE brand WHEN 'brandb' THEN txn_id ELSE NULL END)) AS brandb
<I want my 3rd column here>
FROM
transaction_data
INNER JOIN
products USING (sku)
INNER JOIN
nationalities USING (nationality_id)
GROUP BY nationality
ORDER BY branda DESC
LIMIT 20;
我尝试过使用:
COUNT((CASE品牌WHEN’brandb’OR’brandb’THEN txn_id ELSE NULL END))AS结合 – 但这显然会返回太多(无论是否一起购买,都会返回branda或brandb).我知道我不能使用AND,因为很明显没有一个单元格将成为branda和brandb.
我也试过用:
COUNT((CASE品牌WHEN IN(‘branda’,’brandb’)THEN txn_id ELSE NULL END))AS组合 – 但这不是有效的语法.
我觉得我应该使用HAVING子句,但我不确定这在列列表中是如何工作的.
解决方法:
我认为你需要两个级别的聚合:
SELECT n.nationality,
sum(branda), sum(brandb), sum(branda * brandb)
FROM (SELECT t.txn_id, n.nationality,
MAX(CASE brand WHEN 'branda' THEN 1 ELSE 0 END) AS branda,
MAX(CASE brand WHEN 'brandb' THEN 1 ELSE 0 END) AS brandb
FROM transaction_data t INNER JOIN
products p
USING (sku) INNER JOIN
nationalities n
USING (nationality_id)
GROUP BY t.txn_id, n.nationality
) tn
GROUP BY n.nationality
ORDER BY max(txn_id) DESC
LIMIT 20;
标签:mysql,sql,crosstab 来源: https://codeday.me/bug/20190711/1432058.html