简体   繁体   English

如何对同一张表的两个SQL查询的结果进行join和统计

[英]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客户ID metadata_id元数据_id text_value文本值
customer_1客户_1 metadata_1元数据_1 yes!是的!
customer_1客户_1 metadata_2元数据_2 random value随机值
customer_2客户_2 metadata_1元数据_1 yes!是的!
customer_2客户_2 metadata_2元数据_2 never mind没关系
customer_3客户_3 metadata_1元数据_1 no!不!
customer_3客户_3 metadata_2元数据_2 another value另一个值
customer_4客户_4 metadata_1元数据_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_1customer_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')

See a demo .看一个演示

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 2个

Fiddle小提琴

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM