Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Queries for geting common values and uncommon values Expand / Collapse
Author
Message
Posted Friday, March 15, 2013 7:23 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 17, 2013 10:27 PM
Points: 35, 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

Post #1431524
Posted Friday, March 15, 2013 8:14 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's 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)
Post #1431557
Posted Friday, March 15, 2013 12:37 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:02 PM
Points: 500, Visits: 2,291
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



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1431677
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse