繁体   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