SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count


Count

Author
Message
vblllove
vblllove
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 209
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
jacob sebastian
jacob sebastian
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1494 Visits: 2523
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

.
vblllove
vblllove
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 209
I don't know How to Thank You. That Did the Trick. Thanks a lot and I wish you a happy new Year
jacob sebastian
jacob sebastian
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1494 Visits: 2523
Happy new year!

.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search