Solved

I want to create a SQL query  to report monthly sales

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now