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 |
|
rajan.nithin
Starting Member
42 Posts |
Posted - 2011-05-28 : 04:00:20
|
| Hi,I have a table as shown below with three columns EmpNo,ReferenceNo & Status.EmpNo ReferenceNo Status106172 210405 Closed106176 210646 Closed116453 211227 Closed117134 207238 Issued134426 209206 Issued135496 209295 Issued135511 209208 Closed135880 210632 Closed136656 207621 Issued142158 210795 Issued142158 211158 Closed161133 206385 Closed161133 210201 IssuedThe status can be either Issued or Closed in the table. The issue are :1. For an EmpNo if there are more than 1 ReferenceNo's and if for the multiple reference no's if both the status 'Issued' & 'Closed' are present . I should not retrieve data for that particular EmpNo. 2. At the same time If the EmpNo occurs once & his status is 'Issued'. I should not retrieve the info for that EmpNo also.For Example 1. From the above table for EmpNo's 142158 & 161133 have multiple ReferenceNo's & the status's are Isuued & Closed. So I should not retrieve info for these EmpNo's .2. EmpNo's 117134,134426,135496,136656 occurs once & their status is issued, so I should not retrieve data for these EmpNo's The output should be :EmpNo ReferenceNo Status106172 210405 Closed106176 210646 Closed116453 211227 Closed135511 209208 Closed135880 210632 ClosedPlease helpBest Regards,Nithin |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-05-28 : 06:11:29
|
| What if the status select *from tblwhere EmpNo in(select EmpNmfrom tblgroup by EmpNoHaving (count(distinct ReferenceNo) = 1 and (max(status) <> 'Issued' or min(Status) <> 'Issued'))or (count(distinct ReferenceNo) > 1 and (max(status) <> min(Status)))==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
rajan.nithin
Starting Member
42 Posts |
Posted - 2011-05-28 : 07:08:14
|
Thanks a looooooooooooot!!!!!!!!!! for the timely help. You Rock    |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-06-03 : 15:27:38
|
If I got the requirement right, I think it can be simpler:select distinct empNo, referenceNo, 'Closed' as statusfrom tbl awhere a.status = 'Closed' and not exists(select * from tbl b where b.empNo = a.empNo and b.status = 'Issued' ) The question is if it could be employee with muliple rows with status = 'Issued'.MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
|
|
|