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 |
stenhoeve
Starting Member
5 Posts |
Posted - 2014-05-13 : 04:30:12
|
Hello, I have the following question. I have a table with a column that contains registrationnumbers and a column that contains mutation codes. The registrationnumber can be duplicate but can't have the same mutationcode.So registrationnumber 1234 with mutationcode 01 and/or 1234 with 02What I would like to achieve is to get a list of all registrationnumbers that don't have a specific combination e.g. 1234 with 099 in the database. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-05-13 : 05:46:50
|
[code]select *from yourtablewhere registrationnumber <> '1234'and mutationcode <> '099'[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
stenhoeve
Starting Member
5 Posts |
Posted - 2014-05-13 : 06:02:51
|
Thank you for your reaction but I think this statement is not exactly what I am trying to acchieve. This statement wil give me all records without this specific combination. What I mean is that I would like to have all registrationnumbers in combination with mutationcode 01 and/or 02 but this list of registrationnumebers do not have a combination with code 099 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2014-05-13 : 07:40:43
|
You will get better answers if you provide test data and the expected results for the test data.Something like the following should work but, without test data, the details are up to you.SELECT DISTINCT RegistrationNumberFROM YourTable T1WHERE NOT EXISTS( SELECT 1 FROM YourTable T2 WHERE T2.RegistrationNumber = T1.RegistrationNumber AND T2.MutationCode = '099'); |
|
|
stenhoeve
Starting Member
5 Posts |
Posted - 2014-05-13 : 08:24:31
|
Thank you |
|
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2014-05-13 : 12:46:00
|
You can check for specific mutation values that must all match or not match like below. If you need to see all columns from the row, add:select * from yourtable where registrationnumber in (before the query below and)after it.select registrationnumberfrom yourtablegroup by registrationnumberhaving SUM(case when mutationcode = '001' then 1 else 0 end) = 1 and --match SUM(case when mutationcode = '002' then 1 else 0 end) = 1 and --match SUM(case when mutationcode = '099' then 1 else 0 end) = 0 --no match |
|
|
|
|
|