SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to collapse rows but take a specific value?


How to collapse rows but take a specific value?

Author
Message
SQL_beginner1
SQL_beginner1
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 48
Hi,

I'm struggling as to how to collapse rows in a view based on different values in a column, but take one specific value. Here's the sample data with four columns:

AccountNumber ------MaturityDate ------ResetFreq ------ Amount
123 ------ 20140331 ------ 1D ------- 100.00
123 ------ 20140331 ------ 1M ------- 2400.00
123 ------ 20140331 ------ '' ------- 8700.00
123 ------ 20140331 ------ 1Y ------- -99.00
123 ------ 20140331 ------ 1M ------- 299.00

I want to collapse these four rows into one row:
AccountNumber ------MaturityDate ------ResetFreq ------ Sum(Amount)
123 ------ 20140331 ------ 1Y ------- 11400.00

For ResetFrequency, I need to take the highest value among the distinct values listed for that account.
The hierarchy is:
'' (blank) < 1D (1 day) < 1M (1 month) < 3M (3 months) < 1Y (1 year) < 3Y < 5Y < 10Y

So, in this case, the distinct values are: '', 1D, 1M, 1Y -- so I take 1Y as it is the highest.

Any help will be appreciated.

Amit
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1024 Visits: 979
Here you go:
create table ResetData (accountNumber int, MaturityDate datetime, ResetFreq char(3), Amount decimal(6,2))

insert into ResetData
values (123 , '20140331' , '1D' ,100.00)
,(123 , '20140331' , '1M' ,2400.00)
,(123 , '20140331' , '' ,8700.00)
,(123 , '20140331' , '1Y' ,-99.00)
,(123 , '20140331' , '1M' ,299.00)


select
AccountNumber
, MaturityDate
, max(
case
when ResetFreq = '' then 1
when ResetFreq = '1D' then 2
when ResetFreq = '1M' then 3
when ResetFreq = '3M' then 4
when ResetFreq = '1Y' then 5
when ResetFreq = '3Y' then 6
when ResetFreq = '5Y' then 7
when ResetFreq = '10Y' then 8
end)
, sum(Amount)
from ResetData
group by AccountNumber, MaturityDate



For the ResetFreq you will get a number that you can case back to the naming you want i.e. case when 5 then '1Y'



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1024 Visits: 979
Next time you post a question like yours please try to include a sample create table statement along with some test data like my post back to you. This will help us answer your question quicker and makes it easier too.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
SQL_beginner1
SQL_beginner1
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 48
Keith,

Thanks for your response and suggestion (duly noted).
Your solution has given me the idea. I need to tinker with a little bit - add a new table with Resetfrequency values and a column with corresponding hierarchy value - and then take the Resetfrequency value of the max(hierarchy value). A lookup table will take care of that.

Thanks again.

Amit
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7289 Visits: 6431
Certainly no way around the CASE but I might do it this way so as to only have to CASE it once.


WITH SampleData (accountNumber, MaturityDate, ResetFreq, Amount) AS
(
SELECT 123 , '20140331' , '1D' ,100.00
UNION ALL SELECT 123 , '20140331' , '1M' ,2400.00
UNION ALL SELECT 123 , '20140331' , '' ,8700.00
UNION ALL SELECT 123 , '20140331' , '1Y' ,-99.00
UNION ALL SELECT 123 , '20140331' , '1M' ,299.00
)
SELECT accountNumber, MaturityDate
,ResetFreq=MAX(CASE rn WHEN 1 THEN ResetFreq END)
,Amount=SUM(Amount)
FROM
(
SELECT *
,rn=ROW_NUMBER() OVER (PARTITION BY accountNumber, MaturityDate
ORDER BY CASE ResetFreq
WHEN '' THEN 1
WHEN '1D' THEN 2
WHEN '1M' THEN 3
WHEN '3M' THEN 4
WHEN '1Y' THEN 5
WHEN '3Y' THEN 6
WHEN '5Y' THEN 7
WHEN '10Y' THEN 8
END DESC)
FROM SampleData
) a
GROUP BY accountNumber, MaturityDate;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search