我有一个创建交叉表的查询。结果是txn_id
branda
的计数,以及txn_id
的{{1}}计数。
txn_id不是唯一的。这是事务表的一个示例。:
brandb
另外还有2个表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
和(products) - (sku, brand, product name)
。
我想添加第三栏,让我获得购买两种品牌的(nationalities) - (nationality_id, nationality)
的数量
输出应为
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;
- 然而这显然会返回太多(无论是否一起购买,都会返回branda或brandb)。我知道我不能使用AND,因为很明显没有一个单元格会成为branda和brandb。
我也尝试过使用:
COUNT((CASE brand WHEN 'brandb' OR 'brandb' THEN txn_id ELSE NULL END)) AS combined
- 但这不是有效的语法。
我觉得我应该使用COUNT((CASE brand WHEN IN('branda', 'brandb') THEN txn_id ELSE NULL END)) AS combined
条款,但我不确定这在列列表中是如何工作的。
答案 0 :(得分:0)
我认为您需要两个级别的聚合:
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;