SQL:对主人和主人进行分组和统计细节

时间:2013-07-10 13:36:06

标签: sql tsql

我想计算发送的邮件(主表:ex_deliverylog)&他们的收件人(详细信息表:ex_deliverylog)来自日志。以下查询返回[session]和[recipients]的相同值。总之,我无法组织和计数[会话]。

    Select 
        deliveryaccount,
        DATEDIFF(d,deliverytime, getdate()) AS ago
        ,COUNT(ex_deliverylog.deliveryid) as session
        ,COUNT(ex_deliverylog_recipients.deliveryid) as recipients

        --,( select count(*) from ex_deliverylog_recipients where ex_deliverylog.deliveryid = ex_deliverylog_recipients.deliveryid )  

    from ex_deliverylog

    left join ex_deliverylog_recipients 
           on ex_deliverylog_recipients.deliveryid = ex_deliverylog.deliveryid


    group by
        deliveryaccount,
        DATEDIFF(d,deliverytime, getdate())


    order by ago, session desc

查询&结果:

enter image description here

表&字段:

enter image description here

我如何计算两个会话和&他们的总收件人?

4 个答案:

答案 0 :(得分:1)

现在你得到两个相同的值,因为你的查询为每个GROUP BY返回了一定数量的行,并且对于每个COUNT()语句,所有这些行都被填充,所以你在这些字段中计算的结果中收到相同的值。您需要计算这些ID的唯一值。所以改变

Select 
    deliveryaccount,
    DATEDIFF(d,deliverytime, getdate()) AS ago
    ,COUNT(ex_deliverylog.deliveryid) as session
    ,COUNT(ex_deliverylog_recipients.deliveryid) as recipients

Select 
    deliveryaccount,
    DATEDIFF(d,deliverytime, getdate()) AS ago
    ,COUNT(distinct ex_deliverylog.deliveryid) as session
    ,COUNT(distinct ex_deliverylog_recipients.deliveryid) as recipients

如果这不能满足您的需求,我建议您将会话数和收件人数分成您认为可以控制的单独查询。

答案 1 :(得分:1)

如果我了解您要执行的操作,我认为您需要在会话数上使用COUNT DISTINCT而不是COUNT,默认为COUNT ALL

SELECT 
        deliveryaccount,
        DATEDIFF(d,deliverytime, getdate()) AS ago
        ,COUNT(DISTINCT ex_deliverylog.deliveryid) as session
        ,COUNT(ex_deliverylog_recipients.deliveryid) as recipients
FROM ex_deliverylog
LEFT JOIN ex_deliverylog_recipients 
       ON ex_deliverylog_recipients.deliveryid = ex_deliverylog.deliveryid
GROUP BY
    deliveryaccount,
    DATEDIFF(d,deliverytime, getdate())
ORDER BY ago, session desc

这样,会话计数将反映不同会话的数量,收件人计数将反映不同收件人的数量。如果既未指定ALL也未指定DISTINCT,则COUNT默认为ALL,您将获得您遇到的行为(即两者的计数相同)。

答案 2 :(得分:1)

我如何计算两个会话和&他们的总收件人?
需要计算ex_deliverylog_recipients.deliveryrecipientid

SELECT 
        deliveryaccount
       ,DATEDIFF(d,deliverytime, getdate()) AS ago
       ,COUNT(DISTINCT ex_deliverylog.deliveryid) as session
       ,COUNT(DISTINCT ex_deliverylog_recipients.deliveryrecipientid) as recipients
FROM ex_deliverylog
LEFT JOIN ex_deliverylog_recipients 
       ON ex_deliverylog_recipients.deliveryid = ex_deliverylog.deliveryid
GROUP BY
    deliveryaccount,
    DATEDIFF(d,deliverytime, getdate())
ORDER BY ago, session desc

一个人可以多次收到同一封电子邮件 例如。发送到两个小组,每个小组都有人 如果您需要一个唯一收件人的计数,那么deliveryrecipientaddress(不是deliveryrecipientid):

SELECT 
        deliveryaccount
       ,DATEDIFF(d,deliverytime, getdate()) AS ago
       ,COUNT(DISTINCT ex_deliverylog.deliveryid) as session
       ,COUNT(DISTINCT ex_deliverylog_recipients.deliveryrecipientaddress) as recipients
FROM ex_deliverylog
LEFT JOIN ex_deliverylog_recipients 
       ON ex_deliverylog_recipients.deliveryid = ex_deliverylog.deliveryid
GROUP BY
    deliveryaccount,
    DATEDIFF(d,deliverytime, getdate())
ORDER BY ago, session desc

答案 3 :(得分:0)

SQL Server有一个很好的SQL扩展来执行此操作。

添加" WITH ROLLUP"在你的GROUP BY子句之后,它将在每个GROUP BY级别产生小计。