简体   繁体   中英

sql skip to count row number for certain conditions

I wanted to sum all records if column A is Plan1 else skip to count the row. But i don't know how to skip to count the row..Below is my query.

Select case when columnA='Plan1' then count(columnA) else ??(how to skip to count the row) end from tableA

Exp:

    No Column A
    1  Plan1
    2  Plan1
    3  Plan3
    4  Plan1

answer: 3

Simplest solution:

SELECT Count(*)
FROM   tablea
WHERE  columna = 'Plan1'

Alternative solution:

SELECT Count(*) As total
     , Sum(CASE WHEN columna = 'Plan1' THEN 1 ELSE 0 END) As plan1
FROM   tablea

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