tags:

views:

42

answers:

5
A: 

It's hard to tell given the formatting of your result sets and lack of column headers, but my first guess would be that your GROUP BY needs to eliminate the use of cd.Name1 and use SUM(CASE...) in your columns.

For your column list, you can try this:

CAST(SUM(CASE cd.Name1
             WHEN 'Association' THEN t.Amount - t.AppliedAmount
             ELSE 0
         END) AS DECIMAL(18, 2)) AS [assoc_balance],
CAST(SUM(CASE cd.Name1
             WHEN 'RRFS' THEN t.Amount - t.AppliedAmount
             ELSE 0
         END) AS DECIMAL(18, 2)) AS [rr_balance],

CAST(SUM(CASE cd.Name1
             WHEN 'RRFS' THEN 0
             WHEN 'Association' THEN 0
             ELSE t.Amount - t.AppliedAmount
         END) AS DECIMAL(18, 2)) AS [_balance],

Also, you'll need to remove the name from the ORDER BY as well.

Tom H.
I tried SUM(CASE...) but got the error: Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Blackcoil
I've added code to show how I think the SUM(CASE...) will need to be set up. You should actually be pulling the existing SUM out a level, not just adding a SUM on top of that. Hopefully this is clear with the example now.
Tom H.
+1  A: 

Assuming SQL Server 2005 or better:

I'd use your current query as a CTE then query/group by that. I.e.:

;With CTE AS(


select
      u.AssessorParcelNumber,
      c.CollectionKey AS [r_number],
      c.Closed,
      CASE cd.Name1 WHEN 'Association'
            THEN CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) ELSE 0 END AS [assoc_balance],
      CASE cd.Name1 WHEN 'RRFS' 
            THEN CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) ELSE 0 END AS [rr_balance],
      CASE cd.Name1 WHEN 'RRFS' THEN 0 WHEN 'Association' THEN 0
            ELSE CONVERT(dec(18,2),sum(t.Amount - t.AppliedAmount)) END AS [_balance],
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance]
from
      Unit u with(nolock)
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5
where
      t.Credit = 0 -- is a charge
      and t.Voided = 0 -- is not voided
      -- and u.AssessorParcelNumber = '140-31-715-164'
group by
      u.AssessorParcelNumber, c.CollectionKey, c.closed, cd.Name1
order by
      c.CollectionKey,
      cd.Name1)


SELECT AssessorParcelNumber, 
r_number, 
Closed, 
SUM(Assoc_balance) AS 'Assoc_Balance',
SUM(rr_balance) AS 'rr_balance',
SUM(_balance) AS '_balance',
SUM(balance) AS 'balance'
FROM CTE
GROUP BY AssessorParcelNumber, r_number, Closed
JNK
+1 CTE assumes SQL Server 2005 or later.
Joe Stefanelli
Yeah I am assuming since he listed T-SQL the odds are good.
JNK
+1  A: 

Looks like you'd want to SUM your various balance columns.

SELECT
      t.AssessorParcelNumber,
      t.[r_number],
      t.Closed,
      SUM([assoc_balance]),
      SUM([rr_balance]),
      SUM([_balance]),
      SUM([balance])
    FROM (/* Insert your original query here */) t
    GROUP BY t.AssessorParcelNumber, t.r_number, t.Closed
Joe Stefanelli
+1 Joe - Similar to what I posted just w/out the CTE.
JNK
A: 
select
      u.AssessorParcelNumber,
      c.CollectionKey AS [r_number],
      c.Closed,
      [assoc_balance]=CONVERT(dec(18,2),SUM(CASE WHEN cd.Name1='Association' THEN t.Amount - t.AppliedAmount ELSE 0 END)),
      [rr_balance]=CONVERT(dec(18,2),SUM(CASE WHEN cd.Name1='RRFS' THEN t.Amount - t.AppliedAmount ELSE 0 END)),
      [_balance]=CONVERT(dec(18,2),SUM(CASE WHEN cd.Name1='RRFS' THEN t.Amount - t.AppliedAmount ELSE 0 END)),
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance]
from
      Unit u with(nolock)
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5
where
      t.Credit = 0 -- is a charge
      and t.Voided = 0 -- is not voided
      -- and u.AssessorParcelNumber = '140-31-715-164'
group by
      u.AssessorParcelNumber, c.CollectionKey, c.closed, cd.Name1
order by
      c.CollectionKey,
      cd.Name1;
tricat
A: 

The problem stems from including cd.Name1 in the GROUP BY clause. In the results set you listed (and I am making some assumptions here), you've got a row each for cd.Name = "Association" and "RRFS", and the group by churns that into two rows. Take that column out of the GROUP BY and move it into the case statement, something like:

select 
      u.AssessorParcelNumber, 
      c.CollectionKey AS [r_number], 
      c.Closed, 
      CONVERT(dec(18,2), sum(CASE cd.Name1 WHEN 'Association' THEN t.Amount - t.AppliedAmount ELSE 0 END)) AS [assoc_balance], 
      CONVERT(dec(18,2), sum(CASE cd.Name1 WHEN 'RRFS'        THEN t.Amount - t.AppliedAmount ELSE 0 END)) AS [rr_balance], 
      CONVERT(dec(18,2), sum(CASE cd.Name1 WHEN 'RRFS' THEN 0 WHEN 'Association' THEN 0 ELSE t.Amount - t.AppliedAmount END)) AS [_balance], 
      CONVERT(dec(18,2),SUM(t.amount - t.AppliedAmount)) AS [balance] 
from 
      Unit u with(nolock) 
      left outer join [collection] c with(nolock) on u.UnitKey = c.UnitKey 
      left outer join TransactionDetail t with(nolock) on c.CollectionKey=t.CollectionKey 
      left outer join TypeCode tc with(nolock) on t.PostType = tc.PostType 
      left outer join CodeData cd with(nolock) on tc.Category = cd.Code2 and Code1=5 
where 
      t.Credit = 0 -- is a charge 
      and t.Voided = 0 -- is not voided 
      -- and u.AssessorParcelNumber = '140-31-715-164' 
group by 
      u.AssessorParcelNumber, c.CollectionKey, c.closed
order by 
      c.CollectionKey, 
      cd.Name1;
Philip Kelley