March 15, 2013 at 7:23 am
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
March 15, 2013 at 8:14 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 15, 2013 at 12:37 pm
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
-- Itzik Ben-Gan 2001
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy