I have created a view using this code, my intent is to return a line for each day for each product, with ISNULL causing zero values for days on which that product had no sales. Instead it omits product/day combinations that had zero sales. It is important to have the zero value lines because this is being dropped into an Excel report and the same number of lines need to always be present. Can anyone explain how I am mis understanding the use of ISNULL?:
ALTER VIEW [dbo].[vwLicense] AS
/*The Nested CAST functions round the time part of the orderdate
to zero effectivly preserving the date without the time information*/
SELECT (CAST(
FLOOR(CAST(order_date AS FLOAT))
AS DATETIME
)) AS 'Order_Date',
CASE
WHEN od.product_id = 1
THEN 'Prod 1'
WHEN od.product_id = 2
THEN 'Prod 2'
WHEN od.product_id = 3
THEN 'Prod 3'
WHEN od.product_id = 4
THEN 'Prod 4'
WHEN od.product_id = 5
THEN 'Prod 5'
ELSE 'Unkown Product'
END
AS 'Product',
ISNULL(SUM(od.o_quantity),0) AS 'Licenses',
ISNULL(COUNT(DISTINCT o.order_id),0) AS 'Orders',
ISNULL(SUM(od.o_quantity * od.o_price),0) AS 'Total'
FROM orders AS o
JOIN orderdetail AS od
ON o.order_id = od.order_id
WHERE od.product_id BETWEEN 1 AND 5
AND o.status = 0
AND o.order_total <> 0
GROUP BY CAST(
FLOOR(CAST(order_date AS FLOAT))
AS DATETIME
),
CASE
WHEN od.product_id = 1
THEN 'Prod 1'
WHEN od.product_id = 2
THEN 'Prod 2'
WHEN od.product_id = 3
THEN 'Prod 3'
WHEN od.product_id = 4
THEN 'Prod 4'
WHEN od.product_id = 5
THEN 'Prod 5'
ELSE 'Unkown Product'
END
GO
ALTER VIEW [dbo].[vwLicense] AS
/*The Nested CAST functions round the time part of the orderdate
to zero effectivly preserving the date without the time information*/
SELECT (CAST(
FLOOR(CAST(order_date AS FLOAT))
AS DATETIME
)) AS 'Order_Date',
CASE
WHEN od.product_id = 1
THEN 'Prod 1'
WHEN od.product_id = 2
THEN 'Prod 2'
WHEN od.product_id = 3
THEN 'Prod 3'
WHEN od.product_id = 4
THEN 'Prod 4'
WHEN od.product_id = 5
THEN 'Prod 5'
ELSE 'Unkown Product'
END
AS 'Product',
ISNULL(SUM(od.o_quantity),0) AS 'Licenses',
ISNULL(COUNT(DISTINCT o.order_id),0) AS 'Orders',
ISNULL(SUM(od.o_quantity * od.o_price),0) AS 'Total'
FROM orders AS o
JOIN orderdetail AS od
ON o.order_id = od.order_id
WHERE od.product_id BETWEEN 1 AND 5
AND o.status = 0
AND o.order_total <> 0
GROUP BY CAST(
FLOOR(CAST(order_date AS FLOAT))
AS DATETIME
),
CASE
WHEN od.product_id = 1
THEN 'Prod 1'
WHEN od.product_id = 2
THEN 'Prod 2'
WHEN od.product_id = 3
THEN 'Prod 3'
WHEN od.product_id = 4
THEN 'Prod 4'
WHEN od.product_id = 5
THEN 'Prod 5'
ELSE 'Unkown Product'
END
GO