数据库:SQL Server 2008
我需要制作一份销售报告,将销售数据分组到一周中的几天,例如;
Product Mon Tues Wed Thurs Friday Sat Sunday Total
Product 1 5 2 0 4 3 2 1 17
Product 2 2 1 4 3 1 1 1 13
我有两个连接表 tbl_orders(主表保存订单号、订单状态等),tbl_orderitems(表保存项目信息、数量、价格、产品等)。
日期字段是 dbo.tbl_orders.dte_order_stamp
连接表的结构;
SELECT
dbo.tbl_orders.uid_orders,
dbo.tbl_orders.dte_order_stamp,
dbo.tbl_orders.txt_order_ref,
dbo.tbl_orders.uid_order_custid,
dbo.tbl_orders.uid_order_webid,
dbo.tbl_orders.txt_order_status,
dbo.tbl_orders.uid_order_addid,
dbo.tbl_orders.mon_order_tax,
dbo.tbl_orders.mon_order_grandtotal,
dbo.tbl_orders.mon_order_discount,
dbo.tbl_orders.bit_order_preorder,
dbo.tbl_orders.int_order_deposit_percent,
dbo.tbl_orders.mon_order_delivery,
dbo.tbl_orders.txt_order_deltype,
dbo.tbl_orders.txt_order_process,
dbo.tbl_orders.txt_voucher_code,
dbo.tbl_orders.txt_order_terms,
dbo.tbl_orders.dte_order_paydate,
dbo.tbl_orders.int_order_taxrate,
dbo.tbl_orders.txt_order_googleid,
dbo.tbl_orders.bit_order_archive,
dbo.tbl_orderitems.uid_orderitems,
dbo.tbl_orderitems.uid_orditems_orderid,
dbo.tbl_orderitems.txt_orditems_pname,
dbo.tbl_orderitems.uid_orditems_pcatid,
dbo.tbl_orderitems.uid_orditems_psubcatid,
dbo.tbl_orderitems.mon_orditems_pprice,
dbo.tbl_orderitems.int_orderitems_qty,
dbo.tbl_orderitems.txt_orditems_stype,
dbo.tbl_orderitems.txt_orditems_pref,
dbo.tbl_orderitems.uid_orditems_prodid
FROM
dbo.tbl_orders
INNER JOIN dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
我正在使用以下语句来获取总体销售数据,我可以将其调整为按星期几分组吗?不太确定从哪里开始,我已经阅读了一些内容,datepart
但不太确定如何实现它,或者我会更好地包装 select 语句?
SELECT
SUM(dbo.tbl_orderitems.mon_orditems_pprice) AS prodTotal,
AVG(dbo.tbl_orderitems.mon_orditems_pprice) AS avgPrice,
count(dbo.tbl_orderitems.uid_orditems_prodid) AS prodQty,
dbo.tbl_orderitems.txt_orditems_pname
FROM dbo.tbl_orderitems
INNER JOIN dbo.tbl_orders ON (dbo.tbl_orderitems.uid_orditems_orderid = dbo.tbl_orders.uid_orders)
WHERE dbo.tbl_orders.txt_order_status = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.sale_status#">
GROUP BY
dbo.tbl_orderitems.txt_orditems_pname
ORDER BY dbo.tbl_orderitems.txt_orditems_pname ASC
任何帮助,将不胜感激。