Solved

I want to create a SQL query  to report monthly sales

Posted on 2014-09-23
2
168 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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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