Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get data required

I have STORE_TABLE

id store_name
1 STORE A
2 STORE B
3 STORE C

and TRANSACTION_TABLE

id tran_id store item qty
1 001 1 ruler 2
2 002 2 book 5
3 003 1 pencil 10
4 004 1 tissue 3
5 005 2 tissue 5
6 006 3 tissue 2
7 007 1 pencil 5

With oracle, what query is to find the product that sold in all store?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a PARTITIONed OUTER JOIN:

SELECT t.item
FROM   store_table s
       LEFT OUTER JOIN transaction_table t
       PARTITION BY (t.item)
       ON (s.id = t.store)
GROUP BY
       t.item
HAVING COUNT(t.store) = COUNT(s.id)

Which, for the sample data:

CREATE TABLE store_table (id, store_name) AS
SELECT 1, 'STORE A' FROM DUAL UNION ALL
SELECT 2, 'STORE B' FROM DUAL UNION ALL
SELECT 3, 'STORE C' FROM DUAL;

CREATE TABLE TRANSACTION_TABLE (id, tran_id, store, item, qty) AS
SELECT 1, '001', 1, 'ruler',   2 FROM DUAL UNION ALL
SELECT 2, '002', 2, 'book',    5 FROM DUAL UNION ALL
SELECT 3, '003', 1, 'pencil', 10 FROM DUAL UNION ALL
SELECT 4, '004', 1, 'tissue',  3 FROM DUAL UNION ALL
SELECT 5, '005', 2, 'tissue',  5 FROM DUAL UNION ALL
SELECT 6, '006', 3, 'tissue',  2 FROM DUAL UNION ALL
SELECT 7, '007', 1, 'pencil',  5 FROM DUAL;

Outputs:

ITEM
tissue

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading