SQL按州的总销售额和联系人数

时间:2015-10-12 13:30:10

标签: sql-server

我正在尝试运行一个查询,该查询将返回联系人总数和每个州的销售总数。这是我能够得到的尽可能接近,但是当只有26个制作时总共有31个销售,而当制作694个时则返回737个。此外,如果可能的话,我希望它为每个州显示一行,其中显示该行的该州的销售和联系人总数。提前谢谢。

SELECT LD.STABBR AS STATE,  
ISNULL (count(case when cl.disposition = '750254B1-7DF4-4677-90D5-F2CF8FB3FAD3' then cl.disposition end),0) AS SALES,
count(cl.disposition) as contacts

FROM CLIENTS.DBO.nptLead_81140201650614 LD
inner JOIN
ASDM.DBO.CALLLOG CL ON LD.ACCTNO = CUSTOMER_ID
WHERE CL.START > '2015-10-08' AND CL.START < '2015-10-09' AND
cl.disposition IN (

            'A582678D-365B-4ADF-9C61-71718872F162',
                  --TURNDOWN
            '8B54C708-320E-403F-B80B-4FFA9AB4F240',
                  --TURNDOWN HNGUP_IMMED
            '375AF837-A09B-4600-A885-ACF2C7ED8FB9',
                  --TURNDOWN SND_FND_ELSW
            'B643C325-F08D-4811-BA57-30841F46D47F',
                  --TURNDOWN GAVE LOCAL
            'C8A236A8-0470-41CC-B54E-6E42778479E8',
                  --TURNDOWN TIMING
            '7877D7E7-719B-42AF-8ED2-9B0042E10BC3',
                  --TURNDOWN ALREADY HELPED
            'DF1CD1EF-3282-44AA-AF9D-FE0367CFA056',
                  --TURNDOWN TOO MANY CALLS
            '6F9CD16A-40DE-4786-841F-36E607FE6184',
                  --TURNDOWN CAN'T AFFORD
            '0EE86647-8AEB-41F1-A810-4077ACB79F44',
                  --TURNDOWN NOT INTERESTED
            'CFAAC025-BD19-4B28-BCBF-4D9EA65874D3',
                  --TURNDOWN NOT CMT OVR PHN       
            'E77FB052-5029-467C-9CE8-19D1D2EEE62C',
                  --TURNDOWN ON DNC LIST
            'D3B62778-B76A-47F2-8E1E-AA982990E5C8',
                  --TURNDOWN OTHER
            'FAB27564-0C34-4D99-897F-BC03C09B9F81',
                  --SALE VERIFIED
            '750254B1-7DF4-4677-90D5-F2CF8FB3FAD3',
                  --SALE UNVERIFIED
            '14BE3DFE-CF5C-4242-B19D-D424AEA0FCC8',
                  --DECEASED
            '0A513C75-2272-40BA-96CA-32305D122E41',
                  --DONATION
            'B2C3EB3F-D961-4D18-954F-BB24DDA597A2',
                  --DONATION UNVERIFIED
            'DDF1DF59-5C7A-4952-A5F4-925C4BCADA10',
                  --ZB
            '1545280C-B33F-4C24-9108-3979F7BD2B32',
                  --DNC
            '92B96771-95F7-46D2-8B42-010EB2D8A329')
                  --AUTO DNC
    GROUP BY LD.STABBR WITH ROLLUP


    ORDER BY LD.STABBR

http://sqlfiddle.com/#!3/d849c/7/0

我希望结果像

一样出现
state   contacts  sales
ga         2       1
fl         1       0
total      3       1

1 个答案:

答案 0 :(得分:0)

我不知道您的全局问题,但您的SqlFiddle问题非常简单,只需替换

WHERE CL.START > '2015-10-08' AND CL.START < '2015-10-09' AND

WHERE CL.START > '2015-10-08' AND CL.START <= '2015-10-09' AND

由于您正在处理Date而不是Datetime,因此9到8之间没有值。

编辑:此外,添加&#34;总计&#34; label,您可以通过替换

来使用分组功能
select stabbr as state

select CASE WHEN GROUPING(stabbr) = 1 THEN 'Total' ELSE stabbr END as state,