We are given tables Vendors(VendorId, VendorName, ..) , Invoices(InvoiceId, VendorId…)
I am going over a book exercise that asks for a T-SQL statement to find
all vendors with at least one invoice. But I am confused, since this can be perfectly done with standard SQL:
SELECT VendorName FROM Vendors V join Invoices I on V.VendorId=I.VendorID
GROUP BY VendorName having Count(InvoiceId) > = 1
So, what could they be asking? Should I come up with a procedure to select all vendors with more than n invoices; n= 0,1,2 ?
If so, why is this not working for the general case of n invoices:
CREATE Procedure InvoiceCount @NumberofInvoices Int
AS
BEGIN
SELECT VendorName FROM Vendors V join Invoices I on V.VendorId=I.VendorID
WHERE Count(InvoiceId) > = @Number of Invoices
EXEC InvoiceCount @Number of Invoices = n
Why doesn't this work?
Thank you.
Best Answer
You don't need the WHERE clause because you are doing an inner join, which ensures you will only get vendors that have an invoice:
OR