?
Solved

number of transactions per day per shipper

Posted on 2014-02-07
2
Medium Priority
?
329 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 2000 total points
ID: 39842344
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
ID: 39842361
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

864 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