Reputation: 135
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
Upvotes: 1
Views: 1413
Reputation: 754488
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....
Upvotes: 3