Author |
Topic |
P.Mahendarajah
Starting Member
3 Posts |
Posted - 2013-10-18 : 04:09:35
|
I have a problem where I essentially want to find all the B_ID numbers that do not have any ones or twos next to them in the whole table (see table below).I come up with this query, however it is wrong, because certain B_ID do not have a 1 or 2 on some lines, but then they do on others :(.SELECT B_IDFROM NUMBER WHERE NUM != 1 AND NUM != 2;SELECT * FROM NUMBER;+------+------+------+| A_ID | B_ID | NUM |+------+------+------+| 101 | 101 | 1 || 101 | 102 | 2 || 101 | 201 | 3 || 101 | 301 | 4 || 102 | 201 | 2 || 103 | 102 | 3 || 201 | 101 | 1 || 301 | 301 | 2 || 401 | 102 | 7 || 501 | 102 | 1 || 501 | 301 | 3 |+------+------+------+11 rows in set (0.00 sec) |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-10-18 : 05:05:45
|
SELECT B_IDFROM NUMBER WHERE NUM NOT IN (1 ,2);--Chandu |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-10-18 : 05:13:01
|
Try these..DECLARE @emp TABLE ( A_ID INT, B_ID int, NUM INT)insert @Emp select 101, 101 , 1 union allselect 101, 102 , 2 union allselect 101, 201 , 3 union allselect 101, 301 , 4 union allselect 102, 201 , 2 union allselect 103, 102 , 3 union allselect 201, 101 , 1 union allselect 301, 301 , 2 union allselect 401, 102 , 7 union allselect 501, 102, 1 union allselect 501, 301, 3 SELECT * FROM @EMPEXCEPTSELECT * FROM @EMP WHERE NUM IN ( 1, 2)SELECT * FROM @EMP WHERE NUM NOT IN ( 1, 2)If the above is not the solution let us know the expected output for the above sample data--Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-18 : 08:39:34
|
I think this is what you're asking forDECLARE @emp TABLE ( A_ID INT, B_ID int, NUM INT)insert @Emp select 101, 101 , 1 union allselect 101, 102 , 2 union allselect 101, 201 , 3 union allselect 101, 301 , 4 union allselect 102, 201 , 2 union allselect 103, 102 , 3 union allselect 201, 101 , 1 union allselect 301, 301 , 2 union allselect 401, 102 , 7 union allselect 501, 102, 1 union allselect 501, 301, 3 ;With CTEAS(SELECT ROW_NUMBER() OVER (ORDER BY A_ID,B_ID) AS Seq,*FROM @Emp)SELECT c1.*FROM CTE c1LEFT JOIN CTE c2ON c2.Seq = c1.Seq + 1AND c2.NUM IN (1,2)WHERE c2.Seq IS NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
P.Mahendarajah
Starting Member
3 Posts |
Posted - 2013-10-18 : 19:53:56
|
Hey, Thanks for the replies, I ended up finding the solution, it was,select distinct T.B_IDfrom ( select distinct B_ID from NUMBER where NUM != 1and NUM != 2) Tminus select distinct B_ID from NUMBER where NUM < 3; |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-10-19 : 05:59:28
|
Keep it simple-- SwePesoSELECT B_IDFROM @EmpGROUP BY B_IDHAVING SUM(CASE WHEN NUM IN (1, 2) THEN 1 ELSE 0 END) = 0 Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|