Producers and Suppliers

       

Write an SQL query to fetch the pairs (Producer_ID, Supplier_ID) where the producer has collaborated with the supplier at least three times.



Input :-

Business table :-
There is no Primary Key column for the Business table.

Producer_ID

Supplier_ID

4

4

4

1

4

4

1

4

4

2

4

4

4

2


Output :-

Producer_ID

Supplier_ID

4

4



Query :-
select Producer_ID, Supplier_ID
from Business
group by Producer_ID, Supplier_ID
having count(*)>=3;

Explanation : - 

This query is retrieving data from a table called "Business". The goal is to find pairs of Producer_ID and Supplier_ID that collaborated together at least three times in the table.

The data is grouped by the columns ‘Producer_ID’ and ‘Supplier_ID’. It means that rows with the same combination of ‘Producer_ID’ and ‘Supplier_ID’ will be grouped together.

There is a condition that filters the grouped results. The ‘count(*)’ function counts the number of rows in each group. The ‘>=3’ condition ensures that only groups with a count of three or more are included in the final result.

In summary, the query retrieves pairs of ‘Producer_ID’ and ‘Supplier_ID’ that occur together at least three times in the "Business" table. It provides a way to identify relationships between producers and suppliers that have a significant number of interactions or transactions in the business context.






No comments

darkmode