Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

number of transactions per day per shipper

Posted on 2014-02-07
2
Medium Priority
?
318 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
[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 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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