Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 group by

Author  Topic 

rjackman1959
Yak Posting Veteran

60 Posts

Posted - 2009-02-25 : 15:01:27
I am trying to run an aging report by customer, but I only want a summary by each customer. When I put in a condition using a date field (glpostdt it makes me use that field in the group by. How can I select these records, but not show it in the summary as separate records? Thanks in advance and hopefully this is an easy question.

ALTER proc [dbo].[my_agging]
as
select i.custnmbr,
sum(i.ortrxamt) as Origanal,
(select case
when rmdtypal = 7 or rmdtypal = 9 then sum(-i.curtrxam)
else sum(i.curtrxam) end) as CurrentBalance,
(select case
when datediff(day, i.glpostdt, getdate()) between 31 and 45
then sum(i.ortrxamt)
else 0 end) as '31-45'
(select case
when datediff(day, i.glpostdt, getdate()) between 46 and 60
then sum(i.ortrxamt)
else 0 end) as '46-60'
(select case
when datediff(day, i.glpostdt, getdate()) between 61 and 65
then sum(i.ortrxamt)
else 0 end) as '61-65'
(select case
when datediff(day, i.glpostdt, getdate()) > 90
then sum(i.ortrxamt)
else 0 end) as ‘Over 90’
from MyTable1 as i inner join MyTable2 as c on i.custnmbr = c.custnmbr
where curtrxam > 0
group by i.custnmbr, c.custname, rmdtypal, i.glpostdt
order by i.custnmbr

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-25 : 15:13:54
may this:

ALTER proc [dbo].[my_agging]
as
Select i.custnmbr,
SUM( Case
when rmdtypal = 7 or rmdtypal = 9 then -1*i.curtrxam
else i.curtrxam end) as CurrentBalance,
SUM(case
when datediff(day, i.glpostdt, getdate()) between 31 and 45
then i.ortrxamt
else 0 end) as [31-45],
SUM(case
when datediff(day, i.glpostdt, getdate()) between 46 and 60
then i.ortrxamt
else 0 end) as [46-60],
SUM(case
when datediff(day, i.glpostdt, getdate()) between 61 and 65
then i.ortrxamt
else 0 end) as [61-65],
SUM(case
when datediff(day, i.glpostdt, getdate()) > 90
then i.ortrxamt
else 0 end) as [Over-90]
from MyTable1 as i inner join MyTable2 as c on i.custnmbr = c.custnmbr
where curtrxam > 0
group by i.custnmbr
order by i.custnmbr
Go to Top of Page

rjackman1959
Yak Posting Veteran

60 Posts

Posted - 2009-02-25 : 15:30:36
Perfect Thank You sodeep
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-25 : 15:31:20
quote:
Originally posted by rjackman1959

Perfect Thank You sodeep



You are Welcome.
Go to Top of Page
   

- Advertisement -