2014-06-24 56 views
0

我需要做一個查詢,顯示每個客戶在顯示每週的第一天每週訂購的每個產品/單元組合的數量。到目前爲止,我有這個,但它沒有顯示的日期爲週一PF每週sql server group by week,得到第一個星期的第一天

select o.customer_name, 
convert(varchar, DATEADD(wk, DATEDIFF(wk,0, min(o.delivery_date)), 0), 101) as first_day_of_week, 
item_code, 
(select i.[desc] from items i where i.item = oi.item_code) as description, 
unit, 
(SUM(oi.price)/SUM(oi.qty)) as unit_price, 
SUM(oi.qty) as total_qty, 
SUM(oi.price) as total_charged 
from order_items oi inner join orders o on localID = local_order_id where o.[status] = 'submitted' and qty > 0 
group by DATEPART(ww, delivery_date), customer_name, item_code, unit 
order by customer_name, first_day_of_week, item_code 

回答

0

試試這個SQL

select o.customer_name, 

dateadd(week, datediff(week, 0, o.delivery_date), 0) as first_day_of_week, 
item_code, 
(select i.[desc] from items i where i.item = oi.item_code) as description, 
unit, 
(SUM(oi.price)/SUM(oi.qty)) as unit_price, 
SUM(oi.qty) as total_qty, 
SUM(oi.price) as total_charged 
from order_items oi inner join orders o on localID = local_order_id where o.[status] = 'submitted' and qty > 0 
group by dateadd(week, datediff(week, 0, o.delivery_date), 0), customer_name, item_code, unit 

order by customer_name, first_day_of_week, item_code 
相關問題