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. Count number of records in related tables

Count number of records in related tables

Scheduled Pinned Locked Moved Database
databasehelpquestion
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.
  • K Offline
    K Offline
    Kevin Brydon
    wrote on last edited by
    #1

    Hi. I have database structure like this: Organizations(OrganizationID, Name) Members(MemberID, OrganizationID, Name) Courses(CourseID, OrganizationID, Name) I need to return the Organization Name, the number of members of that organization and the number of courses in that organization in one sql statement so I'd have a table like [OrganizationName] [Number of Members] [Number of courses] [Org A] [10] [22] [Org B] [92] [11] ... Is it possible without using a stored procedure? Can anyone help me out? Any help would be much appreciated.

    A 1 Reply Last reply
    0
    • K Kevin Brydon

      Hi. I have database structure like this: Organizations(OrganizationID, Name) Members(MemberID, OrganizationID, Name) Courses(CourseID, OrganizationID, Name) I need to return the Organization Name, the number of members of that organization and the number of courses in that organization in one sql statement so I'd have a table like [OrganizationName] [Number of Members] [Number of courses] [Org A] [10] [22] [Org B] [92] [11] ... Is it possible without using a stored procedure? Can anyone help me out? Any help would be much appreciated.

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

      One way: select o.Name, (select count(*) as MembersCount from Members m where m.OrganizationID= o.OrganizationID),(select count(*) as CoursesCount from Courses c where c.OrganizationID = o.OrganizationID) from Organization o There are other alternatives depending on what database you are using

      Bob Ashfield Consultants Ltd

      K 1 Reply Last reply
      0
      • A Ashfield

        One way: select o.Name, (select count(*) as MembersCount from Members m where m.OrganizationID= o.OrganizationID),(select count(*) as CoursesCount from Courses c where c.OrganizationID = o.OrganizationID) from Organization o There are other alternatives depending on what database you are using

        Bob Ashfield Consultants Ltd

        K Offline
        K Offline
        Kevin Brydon
        wrote on last edited by
        #3

        Great. Thanks very much!

        A 1 Reply Last reply
        0
        • K Kevin Brydon

          Great. Thanks very much!

          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