简体   繁体   中英

Select count of same column with different value

I have a select like this:

SELECT 
    [Status] AS Requested
    ,[Status] AS [Sent]
    ,[Status] AS Finished
     FROM Store.[Order] 

Status is an int value and I want Count all Status

  1. Count value = 1 and display as a Requested.
  2. Count value = 2 and display as Sent.
  3. Count value = 3 and display as Finished.

How can I achieve it? Regards

Use case expression to count status based on value ( 1,2,3 )

SELECT 
       SUM(CASE WHEN [Status] = 1 THEN 1 ELSE 0 END) AS Requested,
       SUM(CASE WHEN [Status] = 2 THEN 1 ELSE 0 END) AS Sent,
       SUM(CASE WHEN [Status] = 3 THEN 1 ELSE 0 END) AS Finished
FROM Store.[Order] 

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