SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Queries for geting common values and uncommon values


SQL Queries for geting common values and uncommon values

Author
Message
sankar.nandakumaran
sankar.nandakumaran
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 39
Hi......

I have one table in my database say for example "TableA" and the data is shown below..

ID Value
1 A (common for all ids)
1 B (common for all ids)
2 A (common for all ids)
2 C
2 B (common for all ids)
3 A (common for all ids)
3 B (common for all ids)
3 C
3 D

My problem is i need two sql queries which will give the following two resultant sets as shown below

First sql query result will be like this Common Factors in ID (1,2,3)
A
B

Second query result will be Common Factors in ID (1,2,3)
C (here c is not in id 1 so will be listed here)
D

Can any one please give the two queries please , just i need common factors in one set and uncommon factors in another set ..I need two SQL queries but

Thanks and Regards

Sankar
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62893 Visits: 17959
sankar.nandakumaran (3/15/2013)

Hi......

I have one table in my database say for example "TableA" and the data is shown below..

ID Value
1 A (common for all ids)
1 B (common for all ids)
2 A (common for all ids)
2 C
2 B (common for all ids)
3 A (common for all ids)
3 B (common for all ids)
3 C
3 D

My problem is i need two sql queries which will give the following two resultant sets as shown below

First sql query result will be like this Common Factors in ID (1,2,3)
A
B

Second query result will be Common Factors in ID (1,2,3)
C (here c is not in id 1 so will be listed here)
D

Can any one please give the two queries please , just i need common factors in one set and uncommon factors in another set ..I need two SQL queries but

Thanks and Regards

Sankar



I will help you but first you have to enable me to help. There is no chance anybody can provide you with queries based on the extremely vague description posted. Please provide ddl (create table scripts), sample data (insert statements) and desired output based on your sample data. See the first link in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Alan.B
Alan.B
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13452 Visits: 8002
Based on what I can make of your post I think you are looking for this:


--sample data
DECLARE @x TABLE (id int not null, value char(1) not null, primary key(id,value));
INSERT INTO @x SELECT 1,'A' UNION ALL SELECT 1,'B' UNION ALL SELECT 2,'A' UNION ALL SELECT 2,'C' UNION ALL
SELECT 2,'B' UNION ALL SELECT 3,'A' UNION ALL SELECT 3,'B' UNION ALL SELECT 3,'C' UNION ALL SELECT 3,'D';

-- common factors
SELECT value FROM (
SELECT value, COUNT(value) AS x
FROM @x
GROUP BY value
HAVING COUNT(value)=(SELECT COUNT(DISTINCT id) FROM @x)) AS x

-- uncommon factors
SELECT value FROM (
SELECT value, COUNT(value) AS xx
FROM @x
GROUP BY value
HAVING COUNT(value)<(SELECT COUNT(DISTINCT id) FROM @x)) AS xx



-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search