Count

  • Hi,

    I m having problem in creating a query for counting Articles of a same FAmily in an Invoice:

    I have two Tables Invoice, InvoiceDetails that are related through InvoiceId.

    In Invoice Table I keep the Salesman Code. In InvoiceDetails I keep the Articles that they Sell ( Marlboro Family). The Marlboro Family is defines as three GRoups (Marlboro Light, Marlboro Ultra Light and Marlboro Full Flavour) . Their Code is like:

    PMM1112, PMM1212, PMM1313 (MArlboro Light)

    PMM2111, PMM2112, PMM2112 (Marlboro Full Flavour)

    PMM3111 (Marlboro Ultra Light)

    Now I have to count the Invoices for Each Salesman that have an Invoice that Include all Three of them in one Invoice

    E.c InvoiceId 00001 (PMM1112, PMM1212, PMM2111, PMM3111).

    Thanks in Advance

  • Something like this?

    ;WITH family1 AS (

    SELECT DISTINCT InvoiceID

    FROM invoiceDetails

    WHERE ItemCode LIKE 'PMM1%'

    ), family2 AS (

    SELECT DISTINCT InvoiceID

    FROM invoiceDetails

    WHERE ItemCode LIKE 'PMM2%'

    ), family3 AS (

    SELECT DISTINCT InvoiceID

    FROM invoiceDetails

    WHERE ItemCode LIKE 'PMM3%'

    )

    SELECT SalesRepID, COUNT(*) FROM Invoices i

    INNER JOIN family1 f1 ON f1.invoiceID = i.InvoiceID

    INNER JOIN family2 f2 ON f2.InvoiceID = i.InvoiceID

    INNER JOIN family3 f3 ON f3.InvoiceID = i.InvoiceID

    GROUP BY SalesRepID

    .

  • I don't know How to Thank You. That Did the Trick. Thanks a lot and I wish you a happy new Year

  • Happy new year!

    .

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply