Skip to content
Advertisement

Find pair of customers(CustID), who have bought same number of items.(SQL Nested Query)

  • Invoice (InvoiceNo, CustID, OutletID, Date) PK(InvoiceNO)
  • InvoiceItems (InvoiceNo, ItemID, Quantity, Discount) PK(InvoiceNO,ItemID)

PLz tell me solution of Above mention Query using nested Method Using these Two tables structure. I am unable to solve it.

Advertisement

Answer

This should do the trick.

Select i1.CustID, i2.CustID
From
    (Select InvoiceNo,count(*) as nb
         From InvoiceItems
         Group by InvoiceNo) as a,
    (Select InvoiceNo,count(*) as nb
         From InvoiceItems
         Group by InvoiceNo) as b,
    Invoice as i1,
    Invoice as i2
Where a.InvoiceNo = i1.InvoiceNo
    and b.InvoiceNo = i2.InvoiceNo
    and a.InvoiceNo < b.InvoiceNo
    and a.nb = b.nb;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement