Solved

I want to create a SQL query  to report monthly sales

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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