IIF query alternate in sql server

  • hello am using the follwing

    SELECT

    Sum(IIf(attendance.present='P',1,0))  AS TotalPresent  from Attendance...

    it gives me error tht incorrect sytex near=..

     

    if i replace my iif with CASE such as

    SELECT

    case Present

    when 'P'

    then 1

    else 0

    end as TotalPresen from Attendance

     

    then how can i apply SUM function

    Thnx in advance

     

     

  • Just like you did with the immediate IF, you wrap the Sum around it.

    SELECT

    Sum(case Present

    when 'P'

    then 1

    else 0

    end) as TotalPresent from Attendance

    If that really is the extent of your resultset, you could also just do a SELECT COUNT... with the Present = 'P' in the WHERE clause.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply