SQL选择多个计数(*)输出

时间:2015-12-18 12:00:46

标签: sql

我很难弄清楚如何获取查询,这些查询可以让我获得有多少人在某个广告系列中收到过多少封信的数据。 喜欢 输出:

Number of sent emails received             Number of client ID-s
       0                                 20145
       1                                  1244
       2                                  123
      etc..                                etc..

我编写的当前SQL是为了从获取信息所需的所有表中获取所有数据。

SELECT 
account.EXT_STRING0 as client_ID,
account.EXT_STRING2 as Email,
activity.TEMPLATE_NAME as Template_name,
activity.NAME as campaign_name,
activity.EXECUTION_ID, -- unique campaign sent mail id
activity.SEND_DATE,
Activity.OPEN_DATE,
Activity.CLICK_DATE
FROM  
    crmautomation.oocke1_account account 
Join 
    crmautomation.oocke1_activity activity 
    ON 
     (account.object_id = activity.rep_contact)
left JOIN 
    crmautomation.OBJ_VAL val1
       ON val1.obj_row_id = activity.CAMPAIGN_TYPE AND (val1.OBJ_VAL_ID in(4250950)) 
left JOIN 
    crmautomation.OBJ_VAL val2
       ON val2.obj_row_id = activity.CAMPAIGN_TYPE AND (val2.OBJ_VAL_ID in(4250951))
where activity.TEMPLATE_NAME= ('cabin_surprise_ne')
and activity.SEND_DATE >('2015.12.01')

我认为我可以使用Group by来获取电子邮件数量(*) - s,但我不知道该怎么做。

编辑: 这是这个sql的输出

client_ID   Email              template_name      Execution_ID     Send_date
'343205'  random@gmail.com   Cabin_surprise_ne  0032-1fdsa-31    2015-12-14 23:08    

此邮件尚未打开

2 个答案:

答案 0 :(得分:1)

构建查询的一种方法是:

HTTPoison.get("httpbin.org/cookies", [{"User-agent", @userAgent}], hackney: [
                cookie: "cookie1=1 domain=httpbin.org cookie2=2"]  ) do 

这是一个“直方图直方图”查询,您可以尝试获取计数。

编辑:

仔细检查后,您将无法获得零计数,因为with cte as ( <your query here> ) select cnt, count(*) as numClients, min(client_id), max(client_id) from (select client_id, count(activity.rep_contact) as cnt from cte group by client_id ) x group by cnt order by cnt; 子句撤消where。因此,查询可能如下所示:

left join

我仍然不完全理解查询。对于未使用的表,您有两个SELECT cnt, count(*) FROM (SELECT acc.EXT_STRING0 as client_ID, COUNT(a.rep_code) as cnt FROM crmautomation.oocke1_account acc left Join crmautomation.oocke1_activity a ON acc.object_id = a.rep_contact and a.TEMPLATE_NAME = 'cabin_surprise_ne' a.SEND_DATE > '2015.12.01' left JOIN crmautomation.OBJ_VAL val1 ON val1.obj_row_id = a.CAMPAIGN_TYPE AND (val1.OBJ_VAL_ID in (4250950)) left JOIN crmautomation.OBJ_VAL val2 ON val2.obj_row_id = a.CAMPAIGN_TYPE AND (val2.OBJ_VAL_ID in (4250951)) GROUP BY acc.EXT_STRING0 ) a GROUP BY cnt ORDER BY cnt; 个。这些可能会被删除,但是您没有在问题中提供有关数据结构的足够信息。

答案 1 :(得分:0)

select count(*) count2, sum(a.count)
(
SELECT 
    count(*) as count,
    account.EXT_STRING0 AS client_ID,
    account.EXT_STRING2 AS Email,
    activity.TEMPLATE_NAME AS Template_name,
    activity.NAME AS campaign_name,
    activity.EXECUTION_ID,
    activity.SEND_DATE,
    Activity.OPEN_DATE,
    Activity.CLICK_DATE
FROM
    crmautomation.oocke1_account account
        JOIN
    crmautomation.oocke1_activity activity ON (account.object_id = activity.rep_contact)
        LEFT JOIN
    crmautomation.OBJ_VAL val1 ON val1.obj_row_id = activity.CAMPAIGN_TYPE
        AND (val1.OBJ_VAL_ID IN (4250950))
        LEFT JOIN
    crmautomation.OBJ_VAL val2 ON val2.obj_row_id = activity.CAMPAIGN_TYPE
        AND (val2.OBJ_VAL_ID IN (4250951))
WHERE
    activity.TEMPLATE_NAME = ('cabin_surprise_ne')
        AND activity.SEND_DATE > ('2015.12.01')
    group by account.EXT_STRING0 ) a -- client_ID 
    group by account.EXT_STRING2 -- Email