I have a table with orders (for each order (transaction_id) there are client_id and item_id and quantity)
I need to find completely identical orders for a set of goods and their quantity in the order
CREATE TABLE IF NOT EXISTS orders (client_id varchar(10),
item_id varchar(10), quantity int, transaction_id varchar(10));
INSERT INTO orders values
('CL1111','111',1, '1001'),
('CL1111','222',2,'1001'),
('CL1111','333',1,'1001'),
('CL2222','111',2,'1002'),
('CL2222','222',1,'1002'),
('CL2222','333',1,'1002'),
('CL3333','111',1,'1003'),
('CL3333','222',2,'1003'),
('CL3333','333',1,'1003'),
('CL3333','444',1,'1003'),
('CL4444','111',1,'1004'),
('CL4444','222',2,'1004'),
('CL4444','333',1,'1004'),
('CL5555','111',1,'1005'),
('CL5555','222',2,'1005'),
('CL6666','111',1,'1006'),
('CL6666','222',2,'1006'),
('CL6666','333',1,'1007')
client_id | item_id | quantity | transaction_id |
---|---|---|---|
CL1111 | 111 | 1 | 1001 |
CL1111 | 222 | 2 | 1001 |
CL1111 | 333 | 1 | 1001 |
CL2222 | 111 | 2 | 1002 |
CL2222 | 222 | 1 | 1002 |
CL2222 | 333 | 1 | 1002 |
CL3333 | 111 | 1 | 1003 |
CL3333 | 222 | 2 | 1003 |
CL3333 | 333 | 1 | 1003 |
CL3333 | 444 | 1 | 1003 |
CL4444 | 111 | 1 | 1004 |
CL4444 | 222 | 2 | 1004 |
CL4444 | 333 | 1 | 1004 |
CL5555 | 111 | 1 | 1005 |
CL5555 | 222 | 2 | 1005 |
CL6666 | 111 | 1 | 1006 |
CL6666 | 222 | 2 | 1006 |
CL6666 | 333 | 1 | 1007 |
Identical orders here are: (1001 and 1004), (1005 and 1006)
And now I don’t know, how to leave orders, which completely identical on item_id, quantity and set of items.
4
Answers
Your example code doesn’t include quantity!
Here is a code that results as many rows as the table contains the same orders:
Please check this query, it returns correct rows in dbfiddle
I am not sure what about clients having several transactions, but in your examples clients CL5555 and CL6666 are not matched even though they have same transactions 1005 and 1006. So here only clients having one distinct transaction are compared.
Try this query. Order clause in aggregation is necessary for reliable result.
Rows with cnt>1 is has identical row (identical items with same quantity).
With test data, find 2 identical groups for transaction_id 1001-1004 and 1005-1006 (not green in your picture)
result is
Test data