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 |
IK1972
56 Posts |
Posted - 2012-08-22 : 11:11:16
|
I have dataset like thisCaseID, PIDA1, 1A1, 2A1, 3A1, 4A1, 5A2, 3A2, 4A2, 5A2, 6A2, 7A3, 1A3, 3A3, 4A3, 5Then I have PIDs like 1,4,5 and I need to get all CaseID there this PID not exists. If any one PID exists for Case Number then I don't need that Case ID if any PID not exists then I want that CaseID.Thanks |
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2012-08-22 : 11:29:50
|
You haven't exactly made yourself clear, but my best guess is that you want CaseID's where there are no occurrences of a specific PID.try this:declare @pid intset @pid = 2select CaseIDfrom YourTable ytwhere not exists (select CaseID from YourTable ne where PID = @PID and ne.caseID=yt.CaseID)GROUP BY CaseID ---------------------------------------------------------------------------------http://www.mannyroadend.co.uk A Bury FC supporters website and forum |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-22 : 12:15:33
|
quote: Originally posted by inayat1972 I have dataset like thisCaseID, PIDA1, 1A1, 2A1, 3A1, 4A1, 5A2, 3A2, 4A2, 5A2, 6A2, 7A3, 1A3, 3A3, 4A3, 5Then I have PIDs like 1,4,5 and I need to get all CaseID there this PID not exists. If any one PID exists for Case Number then I don't need that Case ID if any PID not exists then I want that CaseID.Thanks
SELECT CaseIDFROM tableGROUP BY CaseIDHAVING SUM(CASE WHEN PID IN (1,4,5) THEN 1 ELSE 0 END)=0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-08-23 : 11:47:10
|
[visakh16] My PID is basically 5 number integer like 25489, 254795, 45856 and I have very large data set.Is this work with large data and with PID i mentioned above. If its work then I like this its more simple and clean as compare to check with Exists or not Exists other options I check.Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-23 : 12:06:59
|
quote: Originally posted by inayat1972 [visakh16] My PID is basically 5 number integer like 25489, 254795, 45856 and I have very large data set.Is this work with large data and with PID i mentioned above. If its work then I like this its more simple and clean as compare to check with Exists or not Exists other options I check.Thanks
it should still work finewhy not test it?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|