?
Solved

I want to create a SQL query  to report monthly sales

Posted on 2014-09-23
2
Medium Priority
?
170 Views
Last Modified: 2014-09-23
I want to loop through each months data and return a data set in a single query.

The SQL below only for a single month, and doesn't not include the month column.

DECLARE       
@VSTARTDATE   DATETIME      =      '2014-09-01',
@VENDDATE     DATETIME      =      '2014-09-23';

SELECT AVG(TOT_AMT) AS [Average Basket Total] , COUNT(TRAN_ID) AS [Transaction Count]

FROM TRANS_HEADER TH

WHERE  
CONVERT( DATE, TH.TRAN_STRT_TS)   BETWEEN       @VSTARTDATE   AND @VENDDATE
AND TH.TRAN_CD = 1                -- TRAN CODE = 1, SALES ONLY
AND TH.VOID_CD = 0                -- NO VOIDS
AND TH.TRNING_MDE_FG = 0   -- NOT TRAINING MODE

Open in new window


Expected Result
Month     Average Basket Total        Transaction Count
7                      19.8216                               2482734
8                      21.076                              2904966
9                      21.2056                              2475887
0
Comment
Question by:Sleepydog
[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 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 2000 total points
ID: 40340552
Try this
DECLARE       
@VSTARTDATE   DATETIME      =      '2014-09-01',
@VENDDATE     DATETIME      =      '2014-09-23';

SELECT month(TH.TRAN_STRT_TS) as month, AVG(TOT_AMT) AS [Average Basket Total] , COUNT(TRAN_ID) AS [Transaction Count]

FROM TRANS_HEADER TH

WHERE  
CONVERT( DATE, TH.TRAN_STRT_TS)   BETWEEN       @VSTARTDATE   AND @VENDDATE
AND TH.TRAN_CD = 1                -- TRAN CODE = 1, SALES ONLY
AND TH.VOID_CD = 0                -- NO VOIDS
AND TH.TRNING_MDE_FG = 0   -- NOT TRAINING MODE
group by month(TH.TRAN_STRT_TS) 
order by 1

Open in new window

0
 
LVL 1

Author Closing Comment

by:Sleepydog
ID: 40340555
Works perfect
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

762 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