使用sql按星期几分组销售数量和销售数字

时间:2012-05-11 09:12:42

标签: sql-server sql-server-2008 pivot

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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