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. Querying for unused parent entries

Querying for unused parent entries

Scheduled Pinned Locked Moved Database
sharepointdatabasecollaborationquestion
8 Posts 4 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.
  • G Offline
    G Offline
    GregStevens
    wrote on last edited by
    #1

    So I have a table of teams, orgTeams, each of which has a primary key, teamID. I have a table of users, orgUsers, each of which has field that describes what team the user is on, userTeamID. If I want to get a list of all of the teams that have at least one person on them, it is easy: SELECT DISTINCT teamID FROM orgTeams,orgUsers WHERE userTeamID=teamID However, with people moving from team to team, sometimes teams end up "empty", i.e. with nobody at all on them. What query can I use to identify such "empty" teams?

    K P 2 Replies Last reply
    0
    • G GregStevens

      So I have a table of teams, orgTeams, each of which has a primary key, teamID. I have a table of users, orgUsers, each of which has field that describes what team the user is on, userTeamID. If I want to get a list of all of the teams that have at least one person on them, it is easy: SELECT DISTINCT teamID FROM orgTeams,orgUsers WHERE userTeamID=teamID However, with people moving from team to team, sometimes teams end up "empty", i.e. with nobody at all on them. What query can I use to identify such "empty" teams?

      K Offline
      K Offline
      Kiefie
      wrote on last edited by
      #2

      To determine if a table has rows in it you can use SELECT COUNT(*) FROM tableName ie. ( I think) SELECT DISTINCT teamID FROM orgTeams,orgUsers WHERE userTeamID=teamID and COUNT(orgTeams.Person) > 0

      Kiefie The man with a plan.

      G K 2 Replies Last reply
      0
      • K Kiefie

        To determine if a table has rows in it you can use SELECT COUNT(*) FROM tableName ie. ( I think) SELECT DISTINCT teamID FROM orgTeams,orgUsers WHERE userTeamID=teamID and COUNT(orgTeams.Person) > 0

        Kiefie The man with a plan.

        G Offline
        G Offline
        GregStevens
        wrote on last edited by
        #3

        I understand the COUNT() function, but I don't understand what orgTeams.Person is supposed to refer to in your example. I don't have a "Person" field in my table orgTeams.

        1 Reply Last reply
        0
        • G GregStevens

          So I have a table of teams, orgTeams, each of which has a primary key, teamID. I have a table of users, orgUsers, each of which has field that describes what team the user is on, userTeamID. If I want to get a list of all of the teams that have at least one person on them, it is easy: SELECT DISTINCT teamID FROM orgTeams,orgUsers WHERE userTeamID=teamID However, with people moving from team to team, sometimes teams end up "empty", i.e. with nobody at all on them. What query can I use to identify such "empty" teams?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You could probably do this with:

          select distinct teamId
          from orgTeams
          left join orgUsers
          on teamID = userTeamId
          group by userTeamID, teamID
          having count(userTeamID) = 0
          

          Deja View - the feeling that you've seen this post before.

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            You could probably do this with:

            select distinct teamId
            from orgTeams
            left join orgUsers
            on teamID = userTeamId
            group by userTeamID, teamID
            having count(userTeamID) = 0
            

            Deja View - the feeling that you've seen this post before.

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #5

            select teamId
            from orgTeams
            left join orgUsers
            on teamID = userTeamId
            where userTeamId IS NULL

            is equivalent and likely to execute quicker.

            Stability. What an interesting concept. -- Chris Maunder

            P G 2 Replies Last reply
            0
            • K Kiefie

              To determine if a table has rows in it you can use SELECT COUNT(*) FROM tableName ie. ( I think) SELECT DISTINCT teamID FROM orgTeams,orgUsers WHERE userTeamID=teamID and COUNT(orgTeams.Person) > 0

              Kiefie The man with a plan.

              K Offline
              K Offline
              Kiefie
              wrote on last edited by
              #6

              I don't know what fields you have in your table so I made it up. Could work with just about any field. I see there are more posts which will probably work better.

              Kiefie The man with a plan.

              1 Reply Last reply
              0
              • M Mike Dimmick

                select teamId
                from orgTeams
                left join orgUsers
                on teamID = userTeamId
                where userTeamId IS NULL

                is equivalent and likely to execute quicker.

                Stability. What an interesting concept. -- Chris Maunder

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                True - this is what happens when you code when tired. You end up overcomplicating things. Thanks for pointing this out to the OP. I'd like to think that I would have spotted this, this morning.

                Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • M Mike Dimmick

                  select teamId
                  from orgTeams
                  left join orgUsers
                  on teamID = userTeamId
                  where userTeamId IS NULL

                  is equivalent and likely to execute quicker.

                  Stability. What an interesting concept. -- Chris Maunder

                  G Offline
                  G Offline
                  GregStevens
                  wrote on last edited by
                  #8

                  Thank you! This worked perfectly.

                  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