Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query - If condition

SQL Query - If condition

Scheduled Pinned Locked Moved Database
databasehelpcsssql-serversysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aaraaayen
    wrote on last edited by
    #1

    Hi Friends, I am using SQL Server 2005. I am facing the following problem. I need to know how to apply if condition in query. I am having one table called Test1 with two fields c_name, average. CC 7 CC 6 CC 8 IGC 2 IGC 4 IGC 6 HC 4 HC 5 NM 9 NM 6 NC 2 NC 5 NC 9 JC 4 JC 3 JC 1 The above are the values in that table. Now I am using the following query to display the c_name, count(college) with average >5 group by c_name select c_name,count(c_name) as Total from test1 where average>5 group by c_name Result : CC 3 IGC 1 NC 1 NM 2 Based on my query I am getting the above result. In the above result the colleges HC and JC are not coming because it has average only less than 5. But for my case, I want to display the college name with the total as 0 if that college doesnt have average >5. I need to modify the above query to get the result like below. CC 3 IGC 1 NC 1 NM 2 HC 0 JC 0 Please help for this. Thanks in Advance, Regards,

    A 1 Reply Last reply
    0
    • A aaraaayen

      Hi Friends, I am using SQL Server 2005. I am facing the following problem. I need to know how to apply if condition in query. I am having one table called Test1 with two fields c_name, average. CC 7 CC 6 CC 8 IGC 2 IGC 4 IGC 6 HC 4 HC 5 NM 9 NM 6 NC 2 NC 5 NC 9 JC 4 JC 3 JC 1 The above are the values in that table. Now I am using the following query to display the c_name, count(college) with average >5 group by c_name select c_name,count(c_name) as Total from test1 where average>5 group by c_name Result : CC 3 IGC 1 NC 1 NM 2 Based on my query I am getting the above result. In the above result the colleges HC and JC are not coming because it has average only less than 5. But for my case, I want to display the college name with the total as 0 if that college doesnt have average >5. I need to modify the above query to get the result like below. CC 3 IGC 1 NC 1 NM 2 HC 0 JC 0 Please help for this. Thanks in Advance, Regards,

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      Try this:

      select c_name, sum(case when average > 5 then 1 else 0 end) as Total
      from Test1
      group by c_name

      Bob Ashfield Consultants Ltd

      A 1 Reply Last reply
      0
      • A Ashfield

        Try this:

        select c_name, sum(case when average > 5 then 1 else 0 end) as Total
        from Test1
        group by c_name

        Bob Ashfield Consultants Ltd

        A Offline
        A Offline
        aaraaayen
        wrote on last edited by
        #3

        Hi Bob, Thanks lot for ur help. Its working well. Regards,

        A 1 Reply Last reply
        0
        • A aaraaayen

          Hi Bob, Thanks lot for ur help. Its working well. Regards,

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          No problem

          Bob Ashfield Consultants Ltd

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups