简体   繁体   中英

SQL TOTAL AND GRAND TOTAL

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

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM