views:

39

answers:

1

hEY ALL

i have a table that shows transactions and their status. How can i create a column that shows the total of transactions that are completed,rejected,declined and the grand total of all on a different column

for example i am looking something like this

Completed 100
Rejected 50
Declined 20
Total        170

In 3 different rows..

The table fields are something like this.

transaction_id
status_id,
date
transaction_number
+3  A: 

Assuming the textual status you mention in your desired output corresponds to the status_id column in your table, you could use something like:

SELECT status_id, count(*)
FROM dbo.YourTable
GROUP BY status_id WITH ROLLUP

This works for SQL Server 2000 and newer - not sure if other RDBMS have the exact same syntax... (unfortunately, you didn't mention what system you're using...)

The WITH ROLLUP will cause an extra line to be added to your result with a status_id of NULL - this is the total of all counts combined.

You didn't mention how to "translate" the status_id into a textual description - that could be added if you provide the necessary info....

marc_s
@Vash: added the WITH ROLLUP
marc_s
+1 for rollup then ;-).
Vash
+1, you can add a COALESCE to include the word "total": `SELECT COALESCE(status_id,'Total'), count(*)...`
KM
@KM: ALl I get with that approach is: "Conversion failed when converting the varchar value 'Total' to data type int."
marc_s