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 |
dforshee
Starting Member
4 Posts |
Posted - 2014-04-28 : 09:29:23
|
Hi guys I have an sql statement I am trying to run however I have a slight issue I was hoping to get help with.The problem is I am getting results in multiple rows when I only want a single response. My code is SELECT BE010130.EMPID_I, case when BE010130.BENEFIT IN ('HSA', 'HSA2P', 'HSAF', 'HSAS', 'HSAW', 'MA2P', 'MA2WP', 'MAFP', 'MAFWP', 'MASP', 'MASWP', 'MB2P', 'MB2WP', 'MBFP', 'MBFWP', 'MBSP', 'MBSWP', 'MC2P', 'MC2PP', 'MC2WP', 'MCFP', 'MCFWP', 'MCSP', 'MCSWP') then 'yes' else 'no' end FROM BE010130 where INACTIVE = 0Currently the results I get are EMPID_1 Answer010003 no010003 no010003 no010003 no010003 yes010004 no010004 no010004 no010004 no010004 no010004 noThe result I am looking for is EMPID_1 Answer010003 yes010004 no |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-04-28 : 09:48:06
|
What if multiple rows in BE01030 have values in the Benefit column that match the values in the list? Which one should the query return? |
|
|
dforshee
Starting Member
4 Posts |
Posted - 2014-04-28 : 09:53:46
|
If multiple rows in BE01030 associated with a single employee id (EMPID_1) match any of the listed values than it should return as true or 'yes' otherwise it should return a false response or 'no' |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-04-28 : 12:46:38
|
quote: Originally posted by dforshee If multiple rows in BE01030 associated with a single employee id (EMPID_1) match any of the listed values than it should return as true or 'yes' otherwise it should return a false response or 'no'
Ok, then something likeSELECT DISTINCT EMPID_I , CASE WHEN exists ( SELECT 1 FROM BE01030 b WHERE BENEFIT in ('HSA', 'HSB') AND b.EMPID_I = a.EMPID_I AND INACTIVE = 0) THEN 'yes' ELSE 'no' ENDFROM BE010130 aWHERE INACTIVE = 0 |
|
|
|
|
|
|
|