1

数据库: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

任何帮助,将不胜感激。

4

1 回答 1

3

你可以使用一个PIVOT. 像这样:

SELECT
    pvt.txt_orditems_pname AS Product,
    pvt.[Monday],
    pvt.[Tuesday],
    pvt.[Wednesday],
    pvt.[Thursday],
    pvt.[Friday],
    pvt.[Saturday],
    pvt.[Sunday],
    (
        pvt.[Monday]+pvt.[Tuesday]+pvt.[Wednesday]+pvt.[Thursday]+pvt.[Friday]+
        pvt.[Saturday]+pvt.[Sunday]
    ) AS Total
FROM
    (
        SELECT
            DATENAME(WEEKDAY,dbo.tbl_orders.dte_order_stamp) AS WeekDayName,
            dbo.tbl_orderitems.mon_orditems_pprice,
            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#">
    ) AS SourceTable
    PIVOT
    (
        SUM(mon_orditems_pprice)
        FOR WeekDayName IN([Monday],[Tuesday],[Wednesday],
                           [Thursday],[Friday],[Saturday],[Sunday])
    )
    AS pvt

或者,如果您不想使用PIVOT. 你可以这样做:

SELECT
    t.txt_orditems_pname AS Product,
    t.[Monday],
    t.[Tuesday],
    t.[Wednesday],
    t.[Thursday],
    t.[Friday],
    t.[Saturday],
    t.[Sunday],
    (
        t.[Monday]+t.[Tuesday]+t.[Wednesday]+t.[Thursday]+t.[Friday]+
        t.[Saturday]+t.[Sunday]
    ) AS Total
FROM
(
    SELECT
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=7 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Sunday],
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=1 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Saturday],
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=2 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Monday],
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=3 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Tuesday],
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=4 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Wednesday],
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=5 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Thursday],
        SUM(CASE WHEN datepart(dw,dbo.tbl_orders.dte_order_stamp)=6 THEN dbo.tbl_orderitems.mon_orditems_pprice ELSE NULL END) AS [Friday],
        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
) AS t
于 2012-05-11T09:34:41.173 回答