SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Age Condition
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

satheesh
Posting Yak Master

United Kingdom
146 Posts

Posted - 03/15/2013 :  13:03:58  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 03/15/2013 :  13:09:54  Show Profile  Reply with Quote

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


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

Go to Top of Page

satheesh
Posting Yak Master

United Kingdom
146 Posts

Posted - 03/18/2013 :  12:46:15  Show Profile  Reply with Quote
Its working.

Many Thanks Visakh.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/18/2013 :  12:49:10  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000