简体   繁体   English

如何计算 SQL 中连接表上的行数?

[英]How to count the rows on a joined table in SQL?

I'm trying to do a join on two tables and count the rows of the second one as they are an event themselves.我正在尝试对两个表进行连接并计算第二个表的行数,因为它们本身就是一个事件。 Here is the code, I've tried a subquery, but I'm not sure if the is the right approach:这是代码,我尝试了一个子查询,但我不确定这是否是正确的方法:

SELECT
    a.E_PST_DATE,
    case when unfilled = 'true' then 'unfilled' else a.bidder end  bidder, 
    a.browser,
    a.site_name,
    a.placement,
    a.device_name, 
    b.PAGENUMBER,
    (SUM(a.CPM)/1000) revenue,
    COUNT(a.sessiontoken) impressions,
    COUNT(b.*) page_views
FROM Table1 a
LEFT JOIN Table2 b ON a.SESSIONTOKEN = b.SESSIONTOKEN
    AND a.SESSIONDEPTH = b.SESSIONDEPTH AND b.PST_DATE > current_date - 3 
WHERE a.E_PST_DATE > current_date - 3 AND b.PAGENUMBER IS NOT NULL
GROUP BY 1, 2, 3, 4, 5, 6, 7

Insteas of COUNT(b. ) page_views Please any not null column name. Insteas of COUNT(b. ) page_views 请任何不是 null 的列名。 count( ) might give you wrong answer count( ) 可能会给你错误的答案

count(b.id)计数(b.id)

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

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