Solved

SQL SELECT Statement: weekly sales report

Posted on 2006-11-06
5
2,071 Views
Last Modified: 2008-01-09
I am using SQL Server 2000 with all of the latest service packs on Windows Server 2003 with all of the latest service packs.

I have a table called "sales" which looks like:
<pre>
| product_name | amount_sold | sale_date  |
--------------------------------------------
| Product_a    | 2           | 2006/10/22 |
| Product_b    | 12          | 2006/10/24 |
| Product_a    | 10          | 2006/10/27 |
| Product_c    | 5           | 2006/10/26 |
| Product_c    | 1           | 2006/10/23 |
... and so on
</pre>
I want to generate a report that lists the total number of each product sold on each day of the week over the course of a single week.  An example of the table I hope to generate is:
<pre>
| Product Name | S | M | Tu | W | Th | F | Sa |
-----------------------------------------------
| Product_a    | 2 | 2 | 2  | 3 | 10 | 10| 10 |
| Product_b    | 4 | 0 | 12 | 8 | 14 | 0 | 9  |
| Product_c    | 1 | 1 | 2  | 6 | 5  | 7 | 8  |
</pre>
The SQL statement I have come up with gives me:  
<pre>
| Product Name | S | M | Tu | W | Th | F | Sa |
-----------------------------------------------
| Product_a    |   |   |    |   |    |   | 39 |
| Product_b    |   |   |    |   |    |   | 47 |
| Product_b    |   |   |    |   |    |   | 30 |
</pre>
Here it is the SQL statement:

SELECT sales.product_name,
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 1 THEN SUM(sales.amount_sold) END AS [S],
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 2 THEN SUM(sales.amount_sold) END AS [M],
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 3 THEN SUM(sales.amount_sold) END AS [Tu],
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 4 THEN SUM(sales.amount_sold) END AS [W],
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 5 THEN SUM(sales.amount_sold) END AS [Th],
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 6 THEN SUM(sales.amount_sold) END AS [F],
CASE DATEPART(weekday,MAX(sales.sale_date)) WHEN 0 THEN SUM(sales.amount_sold) END AS [Sa]
FROM sales
WHERE sales.sale_date >= @dateLow AND sales.sale_date <= @dateHigh
GROUP BY sales.product_name
ORDER BY sales.product_name

Any ideas on how to modify my statement?
0
Comment
Question by:camelo1972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 11

Expert Comment

by:rw3admin
ID: 17882831
Try this
Select      Distinct
      sales.product_name,
      Case DW when 1 then amount_sold end S,
      Case DW when 2 then amount_sold end M,
      Case DW when 3 then amount_sold end Tu,
      Case DW when 4 then amount_sold end W,
      Case DW when 5 then amount_sold end Th,
      Case DW when 6 then amount_sold end F,
      Case DW when 7 then amount_sold end Sa
From      
(      SELECT       sales.product_name,
            DatePart(dw,sales.sale_date) DW,
            Sum(sales.amount_sold) amount_sold
      FROM Sales
      WHERE sales.sale_date >= @dateLow AND sales.sale_date <= @dateHigh
      GROUP BY sales.product_name,DatePart(dw,sales.sale_date)
      ) As sales
ORDER BY sales.product_name
0
 
LVL 11

Accepted Solution

by:
rw3admin earned 63 total points
ID: 17883002
sorry I just tested my last solution was not correct one

try this
Select      Distinct
      sales.Product_Name,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=1 ) S,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=2 ) M,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=3 ) Tu,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=4 ) W,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=5 ) Th,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=6 ) F,
      (      Select       sum(amount_sold) from Sales A
            where       A.Product_Name=sales.Product_Name and
                  A.sale_date Between dateLow and dateHigh and
                  datepart(dw,A.sale_date)=7 ) Sa
From      Sales
Inner Join
      (Select @dateLow as dateLow, @dateHigh as dateHigh ) as Dates
ON      sales.sale_date Between dateLow and dateHigh
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 62 total points
ID: 17883507
SELECT sales.product_name,
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 1 THEN sales.amount_sold ELSE 0 END) AS [S],
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 2 THEN sales.amount_sold ELSE 0 END) AS [M],
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 3 THEN sales.amount_sold ELSE 0 END) AS [Tu],
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 4 THEN sales.amount_sold ELSE 0 END) AS [W],
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 5 THEN sales.amount_sold ELSE 0 END) AS [Th],
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 6 THEN sales.amount_sold ELSE 0 END) AS [F],
    SUM(CASE WHEN DATEPART(weekday, sales.sale_date) = 7 THEN sales.amount_sold ELSE 0 END) AS [Sa]
FROM sales
WHERE sales.sale_date >= @dateLow AND sales.sale_date <= @dateHigh
GROUP BY sales.product_name
ORDER BY sales.product_name
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question