Alternative to COUNTIF in SQL Server

b09cbbtk  于 2023-03-11  发布在  SQL Server
关注(0)|答案(1)|浏览(180)

I used COUNTIF on SQL Server to COUNT a particular value with condition but it brought out an error.

SELECT 
    total rounds, total member, total customer,
    ROUND(total rounds/total member, 2) * 100 AS memberper
FROM 
    (SELECT 
         COUNT(rideid) total rounds
         COUNTIF(member_customer = 'member') AS total member,
         COUNTIF(member_customer = 'customer') AS total customer
     FROM 
         Data)
1zmg4dgp

1zmg4dgp1#

add comma

COUNT(rideid) as  total rounds,    << Comma , as

Example COUNTIF in SQL

Excel: =COUNTIF(Ax:Ay, 42)
SQL: COUNT(CASE WHEN A = 42 THEN 1 END)

Recheck column name total rounds, total member, total customer

SELECT 
    total rounds, total member, total customer,  << Wrong name [total_member, total_customer]?
    ROUND(total rounds/total member, 2) * 100 AS memberper
FROM 

AND

COUNT(rideid) total rounds
COUNTIF(member_customer = 'member') AS total member,
COUNTIF(member_customer = 'customer') AS total customer

You need to double-check all of the SQL queries again.

相关问题