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-13 : 14:58:56
|
Hi I have dataset mentioned below. Operator represent for condition like for ID 2 Operator 2 means (OR condition) and ID 2 Operator (1, 2, 3) check with AND Condition so my expected result for this data set is ID 1 and 2.ID Operator Result1 0 TRUE2 1 TRUE2 2 TRUE2 2 FALSE2 3 TRUE2 3 FALSE3 1 FALSE3 2 TRUE3 2 FALSE3 3 TRUE3 3 FALSEThanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-13 : 15:04:10
|
sorry you rule is not clear. can you elaborate on how you will get 1 and 2 in output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-08-13 : 15:17:27
|
Ok I can try to explain more.For ID 1 we have Operator 0 and Result is "True" so this is Qulify for output.For ID 2 Operator 1 is TrueFor ID 2 Operator 2 is True and False so when we have same operator ID then we can use (OR) so (True or False) = TrueFor ID 2 Operator 3 is also True and False so again (True or False) = Trueso now we have ID 2 Operator 1 is True and Operator 2 is True and Operator 3 is true so (True AND True AND True) = True so ID 2 is qualify for output. Now we can check same thing for ID 3.ID 3 Operator 1 is FalseID 3 Operator 2 is (True OR False) = TrueID 3 Operator 3 is (True OR False) = Truebut when now we can check combine 1, 2 and 3 so its (False AND True AND True) so its False so 3 will not qualify for output.Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-13 : 15:22:52
|
ok. here you goSELECT IDINTO #TempFROM(SELECT ID,Operator,SUM(CASE WHEN Result='TRUE' THEN 1 ELSE -1 END) AS TotalFROM tableGROUP BY ID,Operator)tGROUP BY IDHAVING SUM(CASE WHEN Total<0 THEN 1 ELSE 0 END) =0SELECT t.*FROM YourTable tINNER JOIN #Temp tmpON tmp.ID = t.ID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-08-13 : 16:58:43
|
Thanks |
 |
|
IK1972
56 Posts |
Posted - 2012-08-15 : 13:43:55
|
One Issue with this query. with below data set my expected result is 1,2 and 4 but its only return 1 and 2.ID Operator Result1 0 TRUE2 1 TRUE2 2 TRUE2 2 FALSE2 3 TRUE2 3 FALSE3 1 FALSE3 2 TRUE3 2 FALSE3 3 TRUE3 3 FALSE4 1 True4 2 True4 2 False4 2 FalseThanks |
 |
|
|
|
|