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. Need help with this query so it returns only a single record per user

Need help with this query so it returns only a single record per user

Scheduled Pinned Locked Moved Database
helpdatabasequestion
2 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.
  • M Offline
    M Offline
    Michael J Eber
    wrote on last edited by
    #1

    We have a group of users who can access various locations within the datacenter. For the largest group of users, if they can access one location they can access all of the locations. For a very small population, the user can only access the office and not the data center locations. These users are causing me a problem. The query is aimed at basically giving me the count of active locations. But in the mixed case I get BOTH a record with a null count AND a record with access count. In otherwords: Amy can access only the office and cannot access dc1 or dc2. The query returns Amy's record as Amy, null and Amy, 1. I want it to return ONLY Amy, 1. Any suggestions on the changes I need to accomplish that? Here is the current query:

    SELECT dbo.ContactMaster.BmcCustKey,
    (SELECT COUNT(dbo.LocationAccessGrant.CanAccessLocation)
    WHERE (dbo.LocationAccessGrant.CanAccessLocation = 1)) AS AccessCount
    FROM dbo.ContactMaster LEFT OUTER JOIN
    dbo.LocationAccessGrant ON dbo.ContactMaster.CompanyID = dbo.LocationAccessGrant.CompanyID AND
    dbo.ContactMaster.ContactIdentity = dbo.LocationAccessGrant.ContactIdentity
    GROUP BY dbo.ContactMaster.BmcCustKey, dbo.LocationAccessGrant.CanAccessLocation
    ORDER BY dbo.ContactMaster.BmcCustKey

    J 1 Reply Last reply
    0
    • M Michael J Eber

      We have a group of users who can access various locations within the datacenter. For the largest group of users, if they can access one location they can access all of the locations. For a very small population, the user can only access the office and not the data center locations. These users are causing me a problem. The query is aimed at basically giving me the count of active locations. But in the mixed case I get BOTH a record with a null count AND a record with access count. In otherwords: Amy can access only the office and cannot access dc1 or dc2. The query returns Amy's record as Amy, null and Amy, 1. I want it to return ONLY Amy, 1. Any suggestions on the changes I need to accomplish that? Here is the current query:

      SELECT dbo.ContactMaster.BmcCustKey,
      (SELECT COUNT(dbo.LocationAccessGrant.CanAccessLocation)
      WHERE (dbo.LocationAccessGrant.CanAccessLocation = 1)) AS AccessCount
      FROM dbo.ContactMaster LEFT OUTER JOIN
      dbo.LocationAccessGrant ON dbo.ContactMaster.CompanyID = dbo.LocationAccessGrant.CompanyID AND
      dbo.ContactMaster.ContactIdentity = dbo.LocationAccessGrant.ContactIdentity
      GROUP BY dbo.ContactMaster.BmcCustKey, dbo.LocationAccessGrant.CanAccessLocation
      ORDER BY dbo.ContactMaster.BmcCustKey

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Not sure I've understood you properly, but your subquery doesn't look correct. So try something like this:

      SELECT dbo.ContactMaster.BmcCustKey
      ,Sum(CASE WHEN dbo.LocationAccessGrant.CanAccessLocation = 1
      THEN 1
      ELSE 0
      END) accesscount
      FROM dbo.ContactMaster
      LEFT OUTER JOIN dbo.LocationAccessGrant
      ON dbo.ContactMaster.CompanyID = dbo.LocationAccessGrant.CompanyID
      AND dbo.ContactMaster.ContactIdentity = dbo.LocationAccessGrant.ContactIdentity
      GROUP BY dbo.ContactMaster.BmcCustKey
      ,dbo.LocationAccessGrant.CanAccessLocation
      ORDER BY dbo.ContactMaster.BmcCustKey

      It should take care of your null values

      "When did ignorance become a point of view" - Dilbert

      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