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 |
SQLIsTheDevil
Posting Yak Master
177 Posts |
Posted - 2009-08-10 : 11:43:58
|
I really don't know how to phrase this problem in a few short words, which is why the Subject line is ambiguous. I have a table, right. It has ColumnNum and ColumnType, amongst other fields. ColumnType may have a value of A or B. So, it'd look something like this:ColumnNum ColumnType5 A50 B100 A100 B101 A101 B450 A459 A459 B... What I want to do is select all ColumnNums that have a value of A but do NOT have a value of A and B! So, it'd look something like this:ColumnNum ColumnType5 A450 A... That is, any ColumnNum that has records with ColumnType of A and B are filtered out. I'm having difficulty formulating the query, because it's not a matter of checking if ColumnType has A or B in one record but multiple records for the same ColumnNum. I would appreciate any help. Thank you in advance. |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-10 : 11:49:37
|
try this oneselect t.columnnum,t.columntype from tablename t left join tablename s on s.columnnum = t.columnnum and s.columntype ='b'where t.columntype = 'a' and s.columnnum is null |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-08-10 : 11:52:45
|
maybe this...select * from <table> twhere not exists(select * from <table> t1 where t1.ColumnNum = t.ColumnNum and t1.ColumnType = 'B') |
|
|
SQLIsTheDevil
Posting Yak Master
177 Posts |
Posted - 2009-08-10 : 12:15:35
|
Hey, thank you both for responding.I tried the second one and it didn't work. I tried the first suggestion, as well. I think it's correct! I'm pretty sure I got the results I wanted, but it looks to be correct. I have to analyze the query, but I'd surely appreciate your throught process, as to how you came up with that query!!! In any event, I am grateful for the help! |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-08-10 : 12:32:39
|
If you have an index on (ColumnNum, ColumnType) then also try the following:SELECT ColumnNum, 'A' AS ColumnTypeFROM YourTableGROUP BY ColumnNumHAVING COUNT(CASE WHEN ColumnType = 'A' THEN '1' END) > 0 AND COUNT(CASE WHEN ColumnType = 'B' THEN '1' END) = 0 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-11 : 00:39:25
|
quote: Originally posted by SQLIsTheDevil Hey, thank you both for responding.I tried the second one and it didn't work. I tried the first suggestion, as well. I think it's correct! I'm pretty sure I got the results I wanted, but it looks to be correct. I have to analyze the query, but I'd surely appreciate your throught process, as to how you came up with that query!!! In any event, I am grateful for the help!
welcomei am getting the values of the a from t and b from s and comparing the columnnum with t and s tables (used left join and is null in where condition) which gives the unequal values |
|
|
|
|
|
|
|