Find common values (Intersection) from one column of a table depending on the given values of second column of same table

Saurabh Palatkar

I've following table which specifies the areas where suppliers deliver their products, with three columns as

ID   Supp_ID   Area_ID
1      a         P
2      a         R
3      a         T
4      a         s
.      .         .
.      .         .
5      b         R
6      b         T
7      b         V
.      .         .
.      .         .
8      c         Z
9      c         R
10     c         P
11     c         T
.      .         .
.      .         .
.      .         .

Now I want such a stored procedure such that if I pass Supp_IDs a,b,c to the SP, it should return me the Area_IDs R,T which are common in all the 3 suppliers. In short I've to perform intersection of Area_IDs for given Supp_IDs.

Currently what I am trying is as:

select Area_ID from Table_name where Supp_ID=a
INTERSECT
select Area_ID from Table_name where Supp_ID=b
INTERSECT
select Area_ID from Table_name where Supp_ID=c

The above code is good when I know there is exact three Supp_IDs But I am not able to find how to use above logic at run when there will be different numbers of Supp_IDs.

Now I am not able to find how should I write above SP.

Thanks in advance.

Roman Pekar
select Area_ID
from Table1
where Supp_ID in ('a', 'b', 'c')
group by Area_ID
having count(distinct Supp_ID) = 3

Or, to clarify where 3 comes from:

declare @Filter table(ID nchar(1) primary key)

insert into @Filter values ('a'), ('b'), ('c')

select a.Area_ID
from Table1 as a
where a.Supp_ID in (select t.ID from @Filter as t)
group by a.Area_ID
having count(distinct Supp_ID) = (select count(*) from @Filter)

sql fiddle demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find common values (Intersection) from one column of a table depending on the given values of second column of same table

From Dev

How to update a column in one table based on values from a second table?

From Dev

Fetching several values from the same column in a table

From Dev

Fetching several values from the same column in a table

From Dev

Fetch values from one column of a table with PDO

From Dev

How to find and replace values of one column within the same column (Wordpress wp_postmeta table)

From Dev

mysql update table column with multiple values from same table

From Dev

mysql insert unique values from one column to a column of another table

From Dev

Inputing column values in one table from another related table

From Dev

Inputing column values in one table from another related table

From Dev

How to subtract one table column values to the other table column values

From Dev

Find common elements in a given column from two files and output the column values from each file

From Dev

Comapare values in same column same table

From Dev

Update column of one table from same table

From Dev

Use id values from one query, to corresponding column with same id in another table

From Dev

Join multiple values from one column, selected from another table

From Dev

Update table column with another column values of the same table using updateAll()

From Dev

Select values from one table depending on referenced value in another table

From Dev

SQL query to find count of multiple occurrences of column values from a table?

From Dev

insert statement one column from another table rest of the columns is values

From Dev

Using values from a field in one table as column name for field in another

From Dev

Insert INTO from one table to another and change column values

From Dev

copy certain column values from one table to anther

From Dev

Cross validating one values from one column to second column

From Dev

MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

From Dev

Update value of one table with other values of same column with different condition in same table

From Dev

Update value of one table with other values of same column with different condition in same table

From Dev

Get rows with matching values in column in same table

From Dev

Why does a column of table has same values?

Related Related

  1. 1

    Find common values (Intersection) from one column of a table depending on the given values of second column of same table

  2. 2

    How to update a column in one table based on values from a second table?

  3. 3

    Fetching several values from the same column in a table

  4. 4

    Fetching several values from the same column in a table

  5. 5

    Fetch values from one column of a table with PDO

  6. 6

    How to find and replace values of one column within the same column (Wordpress wp_postmeta table)

  7. 7

    mysql update table column with multiple values from same table

  8. 8

    mysql insert unique values from one column to a column of another table

  9. 9

    Inputing column values in one table from another related table

  10. 10

    Inputing column values in one table from another related table

  11. 11

    How to subtract one table column values to the other table column values

  12. 12

    Find common elements in a given column from two files and output the column values from each file

  13. 13

    Comapare values in same column same table

  14. 14

    Update column of one table from same table

  15. 15

    Use id values from one query, to corresponding column with same id in another table

  16. 16

    Join multiple values from one column, selected from another table

  17. 17

    Update table column with another column values of the same table using updateAll()

  18. 18

    Select values from one table depending on referenced value in another table

  19. 19

    SQL query to find count of multiple occurrences of column values from a table?

  20. 20

    insert statement one column from another table rest of the columns is values

  21. 21

    Using values from a field in one table as column name for field in another

  22. 22

    Insert INTO from one table to another and change column values

  23. 23

    copy certain column values from one table to anther

  24. 24

    Cross validating one values from one column to second column

  25. 25

    MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

  26. 26

    Update value of one table with other values of same column with different condition in same table

  27. 27

    Update value of one table with other values of same column with different condition in same table

  28. 28

    Get rows with matching values in column in same table

  29. 29

    Why does a column of table has same values?

HotTag

Archive