I need help creating columns for the number of days since an order has been place, I cannot figure out how to code the query for the number of days since the order has been place. Ex: I have 37 total orders for a customer that have not shipped, 1 order is 0-1 day old and the rest are +5 days. I need my query to return results like the below example:
bill_to_code bill_to_name 0-1 day 2 day 3 day 4 day +5 day TotalOrder
--------------- --------------------------
---- ------- ----- ----- ----- ------ -----------
100115 INGRAM MICRO CONSIGNED 1 0 0 0 36 37
Below is my query and the data it returns:
SELECT t_ord.bill_to_code,
t_ord.bill_to_name,
COUNT(DISTINCT t_ord.order_number) AS [TotalOrder],
CONVERT(nvarchar(10),t_ord
.order_dat
e,112) AS [OrderDate]
FROM t_order_detail tod
INNER JOIN t_order t_ord ON tod.order_id = t_ord.order_id
INNER JOIN t_item_master tim ON tod.item_master_id = tim.item_master_id
WHERE t_ord.type_id <=5 AND
t_ord.status NOT IN ('S','X')
AND t_ord.bill_to_code = '100115'
GROUP BY t_ord.bill_to_code, t_ord.bill_to_name , CONVERT(nvarchar(10),t_ord
.order_dat
e,112)
ORDER BY bill_to_code
bill_to_code bill_to_name TotalOrder OrderDate
--------------- --------------------------
---- ----------- ----------
100115 INGRAM MICRO CONSIGNED 5 20080516
100115 INGRAM MICRO CONSIGNED 2 20080520
100115 INGRAM MICRO CONSIGNED 1 20080604
100115 INGRAM MICRO CONSIGNED 28 20080605
100115 INGRAM MICRO CONSIGNED 1 20080617
Im sure it is some nested query or union to get the result I want but I just cannot get the syntax correct.
Thank you,
Larry Bye
Start Free Trial