簡體   English   中英

sql server group by week,獲取一周的第一天

[英]sql server group by week, get first day of 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

試試這個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM