SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Specific SQL query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stenhoeve
Starting Member

5 Posts

Posted - 05/13/2014 :  04:30:12  Show Profile  Reply with Quote

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 02

What 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)

Singapore
17607 Posts

Posted - 05/13/2014 :  05:46:50  Show Profile  Reply with Quote

select  *
from    yourtable
where   registrationnumber <> '1234'
and     mutationcode       <> '099'



KH
Time is always against us

Go to Top of Page

stenhoeve
Starting Member

5 Posts

Posted - 05/13/2014 :  06:02:51  Show Profile  Reply with Quote
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
Go to Top of Page

Ifor
Aged Yak Warrior

583 Posts

Posted - 05/13/2014 :  07:40:43  Show Profile  Reply with Quote
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 RegistrationNumber
FROM YourTable T1
WHERE NOT EXISTS
(
	SELECT 1
	FROM YourTable T2
	WHERE T2.RegistrationNumber = T1.RegistrationNumber
		AND T2.MutationCode = '099'
);
Go to Top of Page

stenhoeve
Starting Member

5 Posts

Posted - 05/13/2014 :  08:24:31  Show Profile  Reply with Quote
Thank you
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
364 Posts

Posted - 05/13/2014 :  12:46:00  Show Profile  Reply with Quote
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  registrationnumber
from    yourtable
group by registrationnumber
having  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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000