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

2つの結合されたテーブル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 に答える