Solved

number of transactions per day per shipper

Posted on 2014-02-07
2
297 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
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

685 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