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. Getting Count(*) ,SQL Server query for below requirement..

Getting Count(*) ,SQL Server query for below requirement..

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorial
3 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.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, I have table with data in my database as follows table name :sample (col 'ps' is primary key) village ps val 1 1 56 1 2 67 1 3 65 1 4 70 2 5 74 2 6 64 2 7 32 3 8 46 3 9 56 4 10 64 My requirement is i have to get the Count of 'village' whose val > 60 and val < 60 for that i tried in the following way..(for finding avg 'val' of villages) select sum(val)/count(ps) assessment from sample group by village output: assessment 66 56 59 54 But i need the count of above assessment values > 60 and < 60 i.e count of assessment > 60 , < 60 For that i tried in following way(i know i wrote wrong...but it gives you the idea what i am trying for) select count(*) sample where (select sum(val)/count(ps) assessment from sample group by village) > 60 Please suggest me how to do that... thanks in advance.

    C 1 Reply Last reply
    0
    • K K V Sekhar

      Hi all, I have table with data in my database as follows table name :sample (col 'ps' is primary key) village ps val 1 1 56 1 2 67 1 3 65 1 4 70 2 5 74 2 6 64 2 7 32 3 8 46 3 9 56 4 10 64 My requirement is i have to get the Count of 'village' whose val > 60 and val < 60 for that i tried in the following way..(for finding avg 'val' of villages) select sum(val)/count(ps) assessment from sample group by village output: assessment 66 56 59 54 But i need the count of above assessment values > 60 and < 60 i.e count of assessment > 60 , < 60 For that i tried in following way(i know i wrote wrong...but it gives you the idea what i am trying for) select count(*) sample where (select sum(val)/count(ps) assessment from sample group by village) > 60 Please suggest me how to do that... thanks in advance.

      C Offline
      C Offline
      Caio Kinzel Filho
      wrote on last edited by
      #2

      you need to use HAVING... something like: select sum(val)/count(ps) assessment from sample group by village HAVING sum(val)/count(ps) > 60 let me know if it helps

      Intelligence is almost useless for those who have nothing else! Email: caiokf@gmail.com

      K 1 Reply Last reply
      0
      • C Caio Kinzel Filho

        you need to use HAVING... something like: select sum(val)/count(ps) assessment from sample group by village HAVING sum(val)/count(ps) > 60 let me know if it helps

        Intelligence is almost useless for those who have nothing else! Email: caiokf@gmail.com

        K Offline
        K Offline
        K V Sekhar
        wrote on last edited by
        #3

        thank you for your suggestion. I got it. Its similar approach as you suggested. select count(*) from sample group by village having avg(value) > 60 select count(*) from sample group by village having avg(value) < 60

        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