简体   繁体   中英

SQL Server SUM of values in column counted 3 different ways in the same output

I'm trying to find the right way to write a query to obtain the following dataset:

CustName        CityID      TransactionCount    Complete    InProc
Hammertown      10001       200                 50          150
SportsAuth      10002       10                  1           9

"Complete" is a smaller set of TransactionCount, it should be the sum of TransactionCount when another column not shown (Format) is equal to:

having [format]=23
or [format]=25
or [format]=38
or [format]>=400 and [format]<=499
or [format]>=800 and [format]<=899

"InProc" should then be the remainder of the TransactionCount value. So far I've come up with the following:

SELECT  c.CustName,
t.[City],
sum (t.[TransactionCount]) as InProc
FROM [log].[dbo].[TransactionSummary] t
JOIN [log].[dbo].[Customer] c
on t.CustNo = c.CustNo
and t.City = c.City
and t.subno = c.subno
where t.transactiondate between '6/1/16' and '6/22/16'
group by c.CustName,t.City,t.TransactionCount,[format]
having [format]=23
or [format]=25
or [format]=38
or [format]>=400 and [format]<=499
or [format]>=800 and [format]<=899

This currently outputs the following data:

CustName        CityID      InProc
Hammertown      10001       147
Hammertown      10001       1
Hammertown      10001       1
Hammertown      10001       1
SportsAuth      10002       4
SportsAuth      10002       4
SportsAuth      10002       1

So not only am I not getting 1 result back for each customer, but I don't know how I would add the other 2 columns in without breaking this query. Whatever help I can get would be greatly appreciated.

Assuming you can get the two sub-sets by CustName and CityID (your example is not that clear) . A simple join can pull them together. In the example below. Alias A would be the primary summary and Alias B with be the in-process

Select  A.CustName
       ,A.CityID
       ,A.TransactionCount
       ,Complete = A.TransactionCount - isnull(B.InProc,0)
       .InProc = isnull(B.InProc,0)
From (Select CustName,CityID,TransactionCount=sum(Transactions) From SomeTable Group By CustName,CityID) A
Left Join (Select CustName,CityID,InProc=sum(InProc) From SomeOtherTable Group By CustName,CityID) B
Order By A.CustName,,A.CityID
SELECT sq.*, sq.TransactionCountTotal - sq.CompleteTotal as InProcTotal from 
(
    select 
    c.CustName,
    t.[City],
    sum (t.TransactionCount) as TransactionCountTotal
    sum (
        case 
            when (
                [format] in (23,25,38) 
                or [format] between 400 and 499 
                or format between 800 and 899
                )
        then t.TransactionCount
        else 0
        end
    ) as CompleteTotal
    FROM [log].[dbo].[TransactionSummary] t
    INNER JOIN [log].[dbo].[Customer] c
        on t.CustNo = c.CustNo
        and t.City = c.City
        and t.subno = c.subno
    where t.transactiondate between '6/1/16' and '6/22/16'
    group by c.CustName,t.City
) sq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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