简体   繁体   English

SQL:使用CASE将一个表中的记录与另一个表中的记录连接起来

[英]SQL: Join records in one table with count from another using CASE

I'm new to SQL so need help of this smart community. 我是SQL的新手,所以需要这个智能社区的帮助。 I have two tables - table1 with a list of page_id and table2 which contains multiple records of referral_url for each of these page_id and many more. 我有两个表 - table1 ,其中包含page_idtable2的列表,其中包含每个page_idreferral_url多个记录以及更多。

Table1: 表格1:

page_id | views
-----------------
0       |234
2       |567
9       |890
1       |123

Table 2: 表2:

    page_id | referral_url
    ---------------------------
    0       |link.facebook.com
    1       |link.google.com
    0       |link.facebook.com
    3       |link.instagram.com
    1       |link.twitter.com
   ...      | .....

The desired result should look like: 期望的结果应如下所示:

page_id | views  | social | search 
-----------------------------------
0       |234     | 2      | 0
2       |567     | 4      | 2  
9       |890     | 6      | 0
1       |123     | 0      | 1

I tried this with no luck: 我试了这个没有运气:

SELECT A.page_id, B.referrer
FROM table1 A
LEFT JOIN (
SELECT page_id,

COUNT(
CASE
        WHEN referrer_url LIKE '%%facebook%%'
        OR referrer_url LIKE '%%instagram%%' 
        OR referrer_url LIKE '%%twitter%%' 
            THEN 'social' 
END) AS social,

COUNT(
CASE
        WHEN referrer_url LIKE '%%google%%'
        OR referrer_url LIKE '%%bing%%' 
        OR referrer_url LIKE '%%yahoo%%' 
            THEN 'search' 
END) AS search

FROM table2
WHERE dt BETWEEN '20190401' AND '20190430') B 
ON B.page_id = A.page_id
GROUP BY A.page_id, B.social, B.search;

Could anyone suggest a solution? 有人可以提出解决方案吗? Table 2 is HUGE so I wanted to avoid doing CASE there. 表2是巨大的,所以我想避免在那里做CASE

Your query is over-complicated. 您的查询过于复杂。 This should do what you want: 这应该做你想要的:

SELECT A.page_id, A.views,
       SUM(CASE WHEN referrer_url LIKE '%%facebook%%' OR
                     referrer_url LIKE '%%instagram%%' OR
                     referrer_url LIKE '%%twitter%%' 
                THEN 1 ELSE 0
           END) AS social,
       SUM(CASE WHEN referrer_url LIKE '%%google%%' OR
                     referrer_url LIKE '%%bing%%' OR
                     referrer_url LIKE '%%yahoo%%' 
                THEN 1 ELSE 0
           END) AS search
FROM table1 A LEFT JOIN
     table2 B
     ON B.page_id = A.page_id   
WHERE B.dt BETWEEN '20190401' AND '20190430'
GROUP BY A.page_id, A.views;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表列到另一表的SQL Update记录 - SQL Update records from one table column using another table SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records 从一个表访问另一表访问特定字段的SQL选择计数 - Access SQL select count number of specific field from one table join another table SQL用另一个表中的记录更新一个表的记录? - SQL to Update records of one table with records from another table? SQL从一个表中选择记录并将其用作另一个表的输入以删除记录 - SQL selecting records from one table and using this as input to another table to delete records 将记录从一个表插入/更新到另一表,没有明确的联接 - insert / update records from one table to another table, no clear join SQL从一个表中选择从另一个表开始的行,内部联接和计数 - SQL select from one table counting rows from another, inner join & count 如何在JOIN表中使用SQL CASE和COUNT - How use SQL CASE & COUNT in JOIN Table SQL JOIN:从另一个具有匹配ID的表中选择记录 - SQL JOIN: Select Records from Another Table With Matching IDs sql 连接未从另一个表中获取所有记录 - sql join not taking all records from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM