0

我需要一点关于 SQL 查询的帮助。我想计算 2 两个表中的条目数。我的第一个名为 tracking_adspace_views 的表从我制作的 Adspace 的每个视图中获取条目。在一天结束时,我想计算我最后一天有多少条目。第二个表tracking_clicks计算特殊广告空间的点击次数。因此,并非每个被查看的广告空间都有点击。所以最后我想要类似的东西

  • Adspace Id 1 有 500 次查看和 3 次点击
  • Adspace Id 2 hast 340 Views and 0 Clicks
  • 等等...

我的桌子看起来像这样

tracking_adspace_views

在此处输入图像描述

tracking_clicks

在此处输入图像描述

我的查询如下所示:

SELECT  '2013-10-31', 
        views.tracking_adspace, 
        COUNT(views.tracking_adspace) AS tracking_adspace_views,                        
        COUNT(clicks.tracking_adspace) AS tracking_adspace_clicks
FROM views
LEFT JOIN (
            SELECT tracking_adspace, 
                   COUNT(sub_c.tracking_adspace) AS tracking_adspace_clicks
            FROM tracking_clicks as sub_c
            GROUP BY sub_c.tracking_adspace
          ) AS clicks 
       ON clicks.tracking_adspace = views.tracking_adspace
WHERE views.tracking_time LIKE  '2013-10-31%'
GROUP BY views.tracking_adspace

这会正确计算观看次数,并且点击次数为 0 的位置也计算正确。但是,如果点击列中有点击,我会从视图中获得价值。谢谢你的帮助:)

问候

4

2 回答 2

0

您似乎正在对将被连接重复的行中的值进行计数。

尝试这样的事情: -

SELECT  '2013-10-31' AS TrackingDate, 
        views.tracking_adspace, 
        COUNT(views.tracking_adspace) AS tracking_adspace_views,                        
        clicks.tracking_adspace_clicks
FROM views
LEFT JOIN 
(
    SELECT tracking_adspace, 
            DATE(tracking_time) AS TrackingDate,
            COUNT(tracking_adspace) AS tracking_adspace_clicks
    FROM tracking_clicks 
    GROUP BY tracking_adspace
) AS clicks 
ON clicks.tracking_adspace = views.tracking_adspace
AND DATE(views.tracking_time) = clicks.TrackingDate
WHERE DATE(views.tracking_time) = '2013-10-31'
GROUP BY TrackingDate, views.tracking_adspace, clicks.tracking_adspace_clicks
于 2013-11-01T12:25:53.620 回答
0

我希望您有一个名为tracking_adspaces或其他名称的第三个表,其主键名为tracking_adspace. 如果你这样做,你可以这样做:

select  '2013-10-13' as TrackingDate,
        A.tracking_adspace
        V.views,
        C.clicks
from tracking_adspaces A
left join (
        select tracking_adspace, count(tracking_id) as views
        from views
        where DATE(tracking_time) = '2013-10-31'
        group by tracking_adspace
) V on A.tracking_adspace = V.tracking_adspace
left join (
        select tracking_adspace, count(tracking_id) as clicks
        from tracking_clicks
        where DATE(tracking_time) = '2013-10-31'
        group by tracking_adspace
) C on A.tracking_adspace = C.tracking_adspace

如果没有,您可以使用

select  '2013-10-13' as TrackingDate,
        distinct(A.tracking_adspace)
        V.views,
        C.clicks
from views A
left join ( 
(etc)
于 2013-11-01T12:52:03.563 回答