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.
| Author |
Topic |
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-09-06 : 07:39:25
|
Hi all,This is my first post here.Probably the Subject isn't making clear what I'm struggling with, but I didn't know how to explain in such few words. Let me clarify.Let's say I have a table with four columns.Code, Major Version, Minor Version, StatusCode.Example of data:
Code Major Minor StatusCodeA 0 0 0 A 0 1 0A 1 1 3A 1 2 6A 2 2 2B 0 1 0B 1 1 1B 1 2 6C 2 2 2C 2 3 6C 3 3 6 I would like to have a query which returns all rows, indicating which row, per Code, has the highest Major-Minor version, and has a status different from 'Cancelled' (StatusCode <> 6).Expected result
Code Major Minor StatusCode <HighestNotCancelled>A 0 0 0A 0 1 0A 1 1 3A 1 2 6A 2 2 2 ThisOneB 0 1 0B 1 1 1 ThisOneB 1 2 6C 2 2 2 ThisOneC 2 3 6C 3 3 6 I tried using RANK() over partition, but the result is not what I need.
select t.Code, t.Major, t.Minor, t.StatusCode, t.Rankingfrom( select i.Code, i.Major, i.Minor, i.StatusCode, RANK() over (partition by i.Code order by i.StatusCode) as ranking from MyTable i) torder by t.Major desc, t.Minor desc Any help would be appreciated.Thanks in advance |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2011-09-06 : 07:52:26
|
| post some sample data and the expected result data.Karthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-09-06 : 07:56:29
|
quote: Originally posted by karthik_padbanaban post some sample data and the expected result data.Karthikhttp://karthik4identity.blogspot.com/
I was Editing the post, adding that.Hope it clarifies what I'm trying to do.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-06 : 07:59:09
|
| [code]select t.Code, t.Major, t.Minor, t.StatusCode, case when t.rn=1 then 'This one'else '' end as [<HighestNotCancelled>]from( select i.Code, i.Major, i.Minor, i.StatusCode, ROW_NUMBER() over (partition by i.Code order by [Major Version] desc,[Minor Version] desc) as rn from MyTable where i.StatusCode<>6) t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-09-06 : 08:16:40
|
quote: Originally posted by visakh16
select t.Code, t.Major, t.Minor, t.StatusCode, case when t.rn=1 then 'This one'else '' end as [<HighestNotCancelled>]from( select i.Code, i.Major, i.Minor, i.StatusCode, ROW_NUMBER() over (partition by i.Code order by [Major Version] desc,[Minor Version] desc) as rn from MyTable where i.StatusCode<>6) t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks visakh16 for the quick reply!It is almost what I need. The query does indeed put the 'ThisOne' on the correct rows, but is skipping the rows where StatusCode = 6.I would like a query which returns all rows.Any ideas? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-06 : 08:28:20
|
| [code]select t.Code, t.Major, t.Minor, t.StatusCode, case when t.rn=1 then 'This one'else '' end as [<HighestNotCancelled>]from( select i.Code, i.Major, i.Minor, i.StatusCode, ROW_NUMBER() over (partition by i.Code order by case when i.StatusCode<>6 then 0 else 1 end,[Major Version] desc,[Minor Version] desc) as rn from MyTable ) t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2011-09-06 : 08:33:11
|
| Visakh16, Thank You!!I didn't know we could put a case structure in the order by. That did the trick.Thanks a ton! |
 |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2011-09-06 : 08:36:31
|
quote: Originally posted by visakh16
select t.Code, t.Major, t.Minor, t.StatusCode, case when t.rn=1 then 'This one'else '' end as [<HighestNotCancelled>]from( select i.Code, i.Major, i.Minor, i.StatusCode, ROW_NUMBER() over (partition by i.Code order by case when i.StatusCode<>6 then 0 else 1 end,[Major Version] desc,[Minor Version] desc) as rn from MyTable ) t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
This is fantastic part of work.. I learnt new too.Karthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-06 : 08:38:34
|
quote: Originally posted by Zifter Visakh16, Thank You!!I didn't know we could put a case structure in the order by. That did the trick.Thanks a ton!
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-06 : 08:39:27
|
quote: Originally posted by karthik_padbanaban
quote: Originally posted by visakh16
select t.Code, t.Major, t.Minor, t.StatusCode, case when t.rn=1 then 'This one'else '' end as [<HighestNotCancelled>]from( select i.Code, i.Major, i.Minor, i.StatusCode, ROW_NUMBER() over (partition by i.Code order by case when i.StatusCode<>6 then 0 else 1 end,[Major Version] desc,[Minor Version] desc) as rn from MyTable ) t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
This is fantastic part of work.. I learnt new too.Karthikhttp://karthik4identity.blogspot.com/
Glad that i added something new to your knowledgebase ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|