Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:54:34+00:00 2026-05-20T10:54:34+00:00

Goal: write a query that can bring back all the users of a company,

  • 0

Goal: write a query that can bring back all the users of a company, and the company’s primary user.

I have funky situation where there is login structure that my company has set in place. The tables look like so:

User

  int        int          bit
UserId | CompanyId | IsPrimaryUser |

UserLoginBridge

  int      int
UserId | LoginId

Login

 int
LoginId | CompanyId | ...

Obviously the relationships here are much less than optimal. But this is what I have to work with. My issue is that users can be assigned to many Companies .. so there is a 1:M relationship between User and Login that is expressed by UserLoginBridge.

There is also a Primary User … indicated by the present of a 1 in the IsPrimaryUser col. In addition, the primary user is never placed into the UserLoginBridge but he has a Login entry …. So the catch is .. I cannot join my way to a primary user.

Any thoughts?

Ideal DataSet

UserId | IsPrimaryUser | CompanyId | LoginId
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:54:34+00:00Added an answer on May 20, 2026 at 10:54 am

    I’ll try to explain the method to my madness.

    The subquery “q” in the first SELECT finds minimum login ID for each company. Those should be the logins for the primary user, one per company. I can then join primary users to that subquery on CompanyId.

    The second select for regular users should need no explanation.

    This could be written without the UNION using COALESCE for CompanyId and LoginId, but I think this version is a little easier to read and understand.

    /* Primary User */
    SELECT u.UserId, u.IsPrimaryUser, q.CompanyId, q.LoginId
        FROM User u
            INNER JOIN (SELECT l.CompanyId, MIN(l.LoginId)
                            FROM login l
                            GROUP BY l.CompanyId) q
                ON u.CompanyId = q.CompanyId
        WHERE u.IsPrimaryUser = 1
    UNION ALL
    /* Regular Users */
    SELECT u.UserId, u.IsPrimaryUser, l.CompanyId, l.LoginId
        FROM User u
            INNER JOIN UserLoginBridge ulb
                ON u.UserId = ulb.UserId
            INNER JOIN Login l
                ON ulb.LoginId = l.LoginId
        WHERE u.IsPrimaryUser = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sqlite query that I'm trying to write. I have two tables:
My friend is trying to write a Windows application that can query an iPhone
I have a query that i am trying to write to return the amount
My final goal is to write the program which can run on the Unix
I apologize for the vague title. I am attempting to write a query that
Lets imagine that I have videos and each video can have few tags (maximal
The goal of this SQL query is to return the user with the most
I want to write a query parsing method that would belong to the location
I have some VBA in Access 2010 that runs a query and exports the
My goal is to get a query written. I have three tables, A, B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.