英文:
pull a column from a select function and add it on an existing select query
问题
我正在尝试获得一个输出,其中我正在向现有的选择查询中添加另一列。
第1个输出:
SELECT
product_category,
COUNT(product_category)
FROM marketingimpression
GROUP BY product_category
第2个输出:
SELECT DISTINCT COUNT(is_click) FROM marketingimpression
WHERE is_click = true
GROUP BY product_category
有没有办法将这两个查询合并?数据在同一张表中。
英文:
I am trying to get an output where I am adding another column to an existing select query.
SELECT
product_category,
COUNT (product_category)
FROM marketingimpression
GROUP BY product_category
1st output
SELECT DISTINCT COUNT (is_click) FROM marketingimpression
WHERE is_click = true
GROUP BY product_category
2nd output
Is there a way to get these 2 combined?
The data are in the same table.
答案1
得分: 0
这是一种方法:
SELECT COUNT(product_category) AS count_categories, COUNT(CASE WHEN is_click=true THEN 1 END) AS count_click
FROM marketingimpression
GROUP BY product_category
DISTINCT
不需要,因为你会得到每个 product_category 的一个计数。
英文:
This is a way to do it :
SELECT COUNT (product_category) as count_categories, COUNT (case when is_click=true then 1 end) as count_click
FROM marketingimpression
GROUP BY product_category
DISTINCT
is not needed since you will get one count per product_category
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论