Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:53:21+00:00 2026-06-03T16:53:21+00:00

Database: SQL Server 2008 I need to produce a sales report which groups sales

  • 0

Database: SQL Server 2008

I need to produce a sales report which groups sales figures into days of the week, e.g.;

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

I have two joined tables tbl_orders (primary table holding order no, order status etc.), tbl_orderitems (table holding item information, qty, price, product etc).

The date field is dbo.tbl_orders.dte_order_stamp

Structure of joined tables;

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)

I am using the following statement to get overall sales figures, can i adapt this to group by days of the week? Not really sure where to start, i have done some reading on datepart but not quite sure how to implement it, or would i be better to wrap select statements?

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

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:53:24+00:00Added an answer on June 3, 2026 at 4:53 pm

    You could use a PIVOT. Like this:

    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
    

    Or if you do not want to use a PIVOT. You can do it like this:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to localize a SQL Server 2008 database. After investigating recommendations, I have
I need to create an entirely new Sql Server 2008 database and want to
I need to localize data in tables in a SQL Server 2008 database. Each
Getting error while inserting values into database (SQL Server 2008) Implicit conversion from data
I've got a decimal value, which is stored in my database (SQL Server 2008)
I need to change the database name in SQL SERVER 2008 and use it
I need to view the transaction logs of a database on SQL Server 2008
I need to save measures coming from devices into a SQL database (SQL Server
I am working on a project which uses a relational database (SQL Server 2008).
I need to know if in a database in Sql Server 2008 (would also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.