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 8075731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:05:15+00:00 2026-06-05T15:05:15+00:00

I have a configuration table, that holds the values selected by a specific user.

  • 0

I have a configuration table, that holds the values selected by a specific user. If no values where entered, then the default should be returned. But there’s a catch: the user belong to a speciffic group, and each group has it’s own defaul.

What I need is a single query that returns the user’s values, or the default for his/her group if no data found.

The config tables structure is quite simple:

CONFIGURATION: ConfigurationId (PK), GroupId (FK), UserId (FK), ConfigurationDetailId (FK);
CONFIGURATIONDETAIL: ConfigurationDetailId (PK), ConfigValue1, ConfigValue2

A user may belong to more than one group, and have different configurations for each. Those configs are considered different (have their own ConfigurationDetailId) even if their values are the same.
When I look for the data, I always have the GroupId and the UserId

This works, but I wanted something more “elegant”… What I don’t like is the TOP 1 and ORDER BY I need to eliminate the default row when a user specific row exists:

SELECT TOP 1 
  FROM Config C
 WHERE C.GroupId = @GroupId
   AND (C.UserId = @UserId OR C.UserId IS NULL)
 ORDER BY C.UserId DESC

Any thoughts?

  • 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-06-05T15:05:16+00:00Added an answer on June 5, 2026 at 3:05 pm

    Perhaps try a self join, depending on your table structure.

    create table config
     ( GroupID int, 
       UserID int, 
       ConfigId int, 
       ConfigValue varchar(50),
       IsDefault int );
    
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, NULL, 1, 'A', 1);
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, NULL, 2, 'B', 1);
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, NULL, 3, 'C', 1);
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, 1, 1, 'A1', 0);
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, 1, 2, 'B1', 0);
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, 1, 3, 'C1', 0);
    insert into config (groupid, userid, configid, configvalue, isdefault)
    values (1, 2, 1, 'A2', 0);
    
    select 
       COALESCE(c1.configid, c2.configid) as configid,
       COALESCE(c1.configvalue, c2.configvalue) as configvalue
    from config c1 right join config c2
     on (c1.groupid = c2.groupid 
         and c1.configid = c2.configid
         and c1.isdefault = 0 and c1.userid = 2)
    where c2.isdefault = 1 and c2.groupid = 1;
    
    select 
       COALESCE(c1.configid, c2.configid) as configid,
       COALESCE(c1.configvalue, c2.configvalue) as configvalue
    from config c1 right join config c2
     on (c1.groupid = c2.groupid 
         and c1.configid = c2.configid
         and c1.isdefault = 0 and c1.userid = 1)
    where c2.isdefault = 1 and c2.groupid = 1;
    

    Results for userid = 2

    CONFIGID    CONFIGVALUE
    1           A2
    2           B
    3           C
    

    Results for userid = 1

    CONFIGID    CONFIGVALUE
    1           A1
    2           B1
    3           C1
    

    I’ve built a SQLFiddle for this so you can see it for yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that holds rows of configuration data, ie: id item
I have a configuration listener that creates a Hibernate session when a user hits
We have a configuration table as shown below that stores the start time and
I have configuration values saved in an app.config . I want to create a
I have some configuration data in a config file that I read off disk
I have a table that shows a list of events, each event also has
Here's my configuration: I have a re-runnable batch script that I use to update
I have a few classes, such as those that outline database table structure or
I have an admin configuration for a custom module. I have a button that
Say I have a products table that stores off the shelf products that are

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.