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. Two counts of rows with different criteria

Two counts of rows with different criteria

Scheduled Pinned Locked Moved Database
tutorialdatabasecomtoolsregex
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.
  • L Offline
    L Offline
    Luis Alonso Ramos
    wrote on last edited by
    #1

    Hello, I have a child table that contains rows related to a parent table. I would like, in a single SELECT query for the parent table, to have two counts, one for rows in the child table that match a certain criteria, and another that match some other criteria. A simple example, a table of Classrooms, with a child table Students, where each student is either male of female; and the query should return ClassroomID, NumMales, and NumFemales. I can easily get only one count (males for example) using GROUP BY with something like:

    SELECT COUNT(StudentID) AS NumMales FROM Students
        GROUP BY Sex WHERE Sex = 0
    

    but I can't find a way to get two different aggregate functions to work on the same table. In my case it's actually a bit more complicated, because I have three tables related. But a little pointer on how to do it with two tables should help me get started. Any ideas? Thanks! -- LuisR


    Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

    D 1 Reply Last reply
    0
    • L Luis Alonso Ramos

      Hello, I have a child table that contains rows related to a parent table. I would like, in a single SELECT query for the parent table, to have two counts, one for rows in the child table that match a certain criteria, and another that match some other criteria. A simple example, a table of Classrooms, with a child table Students, where each student is either male of female; and the query should return ClassroomID, NumMales, and NumFemales. I can easily get only one count (males for example) using GROUP BY with something like:

      SELECT COUNT(StudentID) AS NumMales FROM Students
          GROUP BY Sex WHERE Sex = 0
      

      but I can't find a way to get two different aggregate functions to work on the same table. In my case it's actually a bit more complicated, because I have three tables related. But a little pointer on how to do it with two tables should help me get started. Any ideas? Thanks! -- LuisR


      Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

      D Offline
      D Offline
      Dan_P
      wrote on last edited by
      #2

      Like this: SELECT COUNT(StudentID) AS Females, (SELECT COUNT(StudentID) AS Males FROM dbo.Students WHERE (Sex = 'M')) AS Males FROM dbo.Students AS Students_1 WHERE (Sex = 'F')

      L 1 Reply Last reply
      0
      • D Dan_P

        Like this: SELECT COUNT(StudentID) AS Females, (SELECT COUNT(StudentID) AS Males FROM dbo.Students WHERE (Sex = 'M')) AS Males FROM dbo.Students AS Students_1 WHERE (Sex = 'F')

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        Got it... thanks!!! :) -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        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