I have four tables in my SQL database i.e MenuItems
, Categories
, Invoices
and InvoiceDetails
. Now what I want is to show the menu performance report for a certain date i.e total Qty and total Amount for
each menu item for a specific date. It shows the desired result without the date in the where clause but excludes menu items with null values.
Here is my stored procedure:
CREATE PROCEDURE spGetMenuPerformanceByDay
@Date date,
@Terminal int
AS
BEGIN
SELECT
M.Name,
ISNULL(SUM(D.Amount), 0) AS Amount,
ISNULL(SUM(D.Qty), 0) AS Qty
FROM
MenuItems AS M
JOIN
Categories AS C ON C.Id = M.CategoryId
LEFT JOIN
InvoiceDetails AS D ON M.Id = D.ItemId
LEFT JOIN
Invoices I ON I.Id = d.InvoiceId
WHERE
@Terminal IN (I.TerminalId, C.TerminalId)
AND CONVERT(date, I.Time) = @Date
OR NULL IN (Amount, Qty)
GROUP BY
M.Name, M.Id, D.ItemId
ORDER BY
(Qty) DESC
END
The result this stored procedure returns on adding Date in where clause:
Item | Amount | Qty |
---|---|---|
KOFTA ANDA | 1950 | 3 |
HOT N SOUR SOUP | 550 | 1 |
CHICKEN CHOWMEIN | 250 | 1 |
CHICKEN KORMA | 850 | 1 |
And the result I want is but don't get it on adding Date in where clause :
Item | Amount | Qty |
---|---|---|
KOFTA ANDA | 1950 | 3 |
HOT N SOUR SOUP | 550 | 1 |
CHICKEN CHOWMEIN | 250 | 1 |
CHICKEN KORMA | 850 | 1 |
CRISPY CHICKEN | 0 | 0 |
MEXICAN BURGER | 0 | 0 |