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
 Age Condition

Author  Topic 

satheesh
Posting Yak Master

152 Posts

Posted - 2013-03-15 : 13:03:58
Dear All,

I have a table which has policy id and age of the traveler in separate field.Max 7 traveler

I need to select all the policyid which has only 1 adult traveler (18yrs or above)

for ex

Policyid t1 t2 t3 t4 t5 t6 t7
1123 12 8 5 7 45
2345 56 67
3234 9 35
6785 56 58 13
2456 23 11 14

Result

1123
3234
2456

Any help will be highly appreciated!

Thanks
SG

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-15 : 13:09:54
[code]
SELECT policyid
FROM table t
unpivot (Age FOR traveller IN ([t1],[t2],[t3],[t4],[t5],[t6],[t7]))u
group by policyid
having sum(case when Age > 18 then 1 else 0 end) = 1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

satheesh
Posting Yak Master

152 Posts

Posted - 2013-03-18 : 12:46:15
Its working.

Many Thanks Visakh.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-18 : 12:49:10
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -