[英]How to join and count the results of two SQL queries on the same table
I have one table that contains all kind of metadata about customers.我有一张表,其中包含有关客户的所有类型的元数据。 It is structured like this:
它的结构如下:
customer_id, metadata_id, text_value
I want to count all customers that have a specific text_value
for metadata_id='metadata_1'
and for whom an entry for metadata_id='metadata_2'
exists.我想统计所有具有
metadata_id='metadata_1'
的特定text_value
并且存在 metadata_id=' metadata_id='metadata_2'
' 条目的客户。
customer_id![]() |
metadata_id![]() |
text_value![]() |
---|---|---|
customer_1![]() |
metadata_1![]() |
yes!![]() |
customer_1![]() |
metadata_2![]() |
random value![]() |
customer_2![]() |
metadata_1![]() |
yes!![]() |
customer_2![]() |
metadata_2![]() |
never mind![]() |
customer_3![]() |
metadata_1![]() |
no!![]() |
customer_3![]() |
metadata_2![]() |
another value![]() |
customer_4![]() |
metadata_1![]() |
yes!![]() |
I can easily get the two parts:我可以很容易地得到这两个部分:
SELECT customer_id
FROM my_table
WHERE metadata_id='metadata_1' AND text_value='yes!';
SELECT customer_id
FROM my_table
WHERE metadata_id='metadata_2';
But how do I combine them and count them?但是我如何组合它们并计算它们呢?
Here customer_1
and customer_2
fulfil both conditions.这里
customer_1
和customer_2
满足两个条件。 So the answer for the example would be 2.所以这个例子的答案是 2。
You can use EXISTS
operator as the following:您可以使用
EXISTS
运算符,如下所示:
SELECT COUNT(*) AS CNT
FROM my_table T
WHERE metadata_id='metadata_1' AND text_value='yes!'
AND EXISTS(SELECT 1 FROM my_table D WHERE D.customer_id=T.customer_id AND D.metadata_id='metadata_2')
If there is a duplicate ids and and you want to count only distinct ids you can use COUNT(DISTINCT customer_id)
.如果存在重复的 ID,并且您只想计算不同的 ID,则可以使用
COUNT(DISTINCT customer_id)
。
And if you want to get the customer ids:如果您想获取客户 ID:
SELECT customer_id
FROM my_table T
WHERE metadata_id='metadata_1' AND text_value='yes!'
AND EXISTS(SELECT 1 FROM my_table D WHERE D.customer_id=T.customer_id AND D.metadata_id='metadata_2')
We mark the customer_id
that follows condition one.我们标记条件一之后的
customer_id
。 Then we mark the customer_id
that follows condition two and then we count the customer_id
we have left.然后我们标记条件二后面的
customer_id
,然后我们计算我们剩下的customer_id
。
select count(distinct customer_id) as cnt
from (
select *
,count(case when metadata_id = 'metadata_1' and text_value = 'yes!' then 1 end) over(partition by customer_id) as mrk1
,count(case when metadata_id = 'metadata_2' then 1 end) over(partition by customer_id) as mrk2
from t
) t
where mrk1 > 0
and mrk2 > 0
cnt![]() |
---|
2 ![]() |
I think this should do it我认为应该这样做
;WITH
T1
AS
(
SELECT customer_id
FROM my_table
WHERE metadata_id='metadata_1' AND text_value='yes!'
),
T2
AS
(
SELECT customer_id
FROM my_table
WHERE metadata_id='metadata_2'
)
SELECT COUNT(1)
FROM T1
INNER JOIN
T2 ON T1.customer_id = T2.customer_id
A UNION
should work I think我认为
UNION
应该可以工作
SELECT COUNT(customer_id)
FROM (
SELECT customer_id
FROM my_table
WHERE metadata_id = 'metadata_1'
AND text_value = 'yes!'
UNION
SELECT customer_id
FROM my_table
WHERE metadata_id = 'metadata_2'
) alpha;
you can use OR operator and merge your conditions.您可以使用 OR 运算符并合并您的条件。
SELECT
COUNT(*)
FROM
(
SELECT
customer_id
FROM
my_table
WHERE
( ( metadata_id= 'metadata_1' AND text_value= 'yes!' )
OR metadata_id= 'metadata_2' )
GROUP BY
customer_id
HAVING
COUNT(*) > 1
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.