从一个选择函数中提取一列并将其添加到现有的选择查询中。

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月25日 14:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定