Solved

number of transactions per day per shipper

Posted on 2014-02-07
2
282 Views
Last Modified: 2014-02-07
Hi Experts,
I am trying to determine how many transaction records each of our shippers had per day last year. The following query correctly returns the total number of transactions per day.

SELECT CONVERT(CHAR(10), A.DATETIME_ENDED,101), COUNT(*) AS TOTAL_NUM
FROM MYDB.XACSTAT AS A
WHERE
A.CANCELLED = 0 AND A.XACTYPE = 3 AND YEAR(A.DATETIME_ENDED) = 2013
GROUP BY CONVERT(CHAR(10), A.DATETIME_ENDED,101);

I would like the query to return additional columns for the number of transactions for each 0day where A.SHIPPER = 1095 AND A.SHIPPER = 2969. (There are other shippers besides these two.)  

For example, the first few rows of output data might look something like this...
DATE          TOTAL_NUM       NUM_1095      NUM_2969
-------          ------------------      ----------------      ---------------
1/1/2013             20                        5                    10
1/2/2013             25                      25                      0
1/3/2013             30                       0                       5
1/4/2013             40                       0                       0


Again, I would like to add two columns to my existing query - one for the number of transactions per day for shipper 1095 (NUM_1095) and another for the number of transactions per day for shipper 2969 (NUM_2969).

Any help would be appreciated.
0
Comment
Question by:munequito
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
Comment Utility
try this

SELECT CONVERT(CHAR(10), A.DATETIME_ENDED,101), COUNT(*) AS TOTAL_NUM
,COUNT(CASE A.SHIPPER WHEN 1095 THEN 1 ELSE 0 END) AS NUM_1095
,COUNT(CASE A.SHIPPER WHEN 2969 THEN 1 ELSE 0 END) AS NUM_2969
FROM MYDB.XACSTAT AS A 
WHERE 
A.CANCELLED = 0 AND A.XACTYPE = 3 AND YEAR(A.DATETIME_ENDED) = 2013 
GROUP BY CONVERT(CHAR(10), A.DATETIME_ENDED,101);

Open in new window

0
 

Author Closing Comment

by:munequito
Comment Utility
Surendra,
Your query was returning the same value in each field. I simply changed "count" to "sum" - which you probably intended - and it now returns the correct values I was looking for.

Thanks so much for your solution.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now