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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:03:46+00:00 2026-05-10T17:03:46+00:00

Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key,

  • 0

Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key, value)

and I want to return a value for all keys. If the key exists in TableC return that value else if the key exists in B return that value else return the value from table A

The best I have come up with so far is

SELECT key,Value FROM TableA WHERE key NOT IN (SELECT key FROM TableB)     AND key NOT IN (SELECT key FROM TableC) UNION SELECT key,Value FROM TableB WHERE key NOT IN (SELECT key FROM TableC) UNION SELECT key,Value FROM TableC 

But this seems pretty brute force. Anyone know a better way?

Edit: Here is a more concrete example. Consider TableA as a standard work schedule where the key is a date and the value is the assigned shift. Table B is a statutory holiday calendar that overrides the standard work week. Table C is an exception schedule that is used to override the other two schedules when someone is asked to come in and work either an extra shift or a different shift.

  • 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. 2026-05-10T17:03:47+00:00Added an answer on May 10, 2026 at 5:03 pm

    OK, using your concrete example as a basis, I came up with a solution different from the others posted (although I think I like your solution better). This was tested on MS SQL Server 2005 – changes may be needed for your SQL dialect.

    First, some DDL to set the stage:

    CREATE TABLE [dbo].[StandardSchedule](     [scheduledate] [datetime] NOT NULL,     [shift] [varchar](25) NOT NULL,  CONSTRAINT [PK_StandardSchedule] PRIMARY KEY CLUSTERED  ( [scheduledate] ASC ));  CREATE TABLE [dbo].[HolidaySchedule](     [holidaydate] [datetime] NOT NULL,     [shift] [varchar](25) NOT NULL,  CONSTRAINT [PK_HolidaySchedule] PRIMARY KEY CLUSTERED  ( [holidaydate] ASC ));  CREATE TABLE [dbo].[ExceptionSchedule](     [exceptiondate] [datetime] NOT NULL,     [shift] [varchar](25) NOT NULL,  CONSTRAINT [PK_ExceptionDate] PRIMARY KEY CLUSTERED  ( [exceptiondate] ASC ));  INSERT INTO ExceptionSchedule VALUES ('2008.01.06', 'ExceptionShift1'); INSERT INTO ExceptionSchedule VALUES ('2008.01.08', 'ExceptionShift2'); INSERT INTO ExceptionSchedule VALUES ('2008.01.10', 'ExceptionShift3'); INSERT INTO HolidaySchedule VALUES ('2008.01.01', 'HolidayShift1'); INSERT INTO HolidaySchedule VALUES ('2008.01.06', 'HolidayShift2'); INSERT INTO HolidaySchedule VALUES ('2008.01.09', 'HolidayShift3'); INSERT INTO StandardSchedule VALUES ('2008.01.01', 'RegularShift1'); INSERT INTO StandardSchedule VALUES ('2008.01.02', 'RegularShift2'); INSERT INTO StandardSchedule VALUES ('2008.01.03', 'RegularShift3'); INSERT INTO StandardSchedule VALUES ('2008.01.04', 'RegularShift4'); INSERT INTO StandardSchedule VALUES ('2008.01.05', 'RegularShift5'); INSERT INTO StandardSchedule VALUES ('2008.01.07', 'RegularShift6'); INSERT INTO StandardSchedule VALUES ('2008.01.09', 'RegularShift7'); INSERT INTO StandardSchedule VALUES ('2008.01.10', 'RegularShift8'); 

    Using these tables/rows as a basis, this SELECT statement retrieves the desired data:

    SELECT DISTINCT     COALESCE(e2.exceptiondate, e.exceptiondate, holidaydate, scheduledate) AS ShiftDate,     COALESCE(e2.shift, e.shift, h.shift, s.shift) AS Shift FROM standardschedule s FULL OUTER JOIN holidayschedule h ON s.scheduledate = h.holidaydate FULL OUTER JOIN exceptionschedule e ON h.holidaydate = e.exceptiondate FULL OUTER JOIN exceptionschedule e2 ON s.scheduledate = e2.exceptiondate ORDER BY shiftdate 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you publish your web app or compile your site… May 11, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer The spec for the ADC should identify how 5V is… May 11, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer The problem is the helper method is just trying it's… May 11, 2026 at 5:50 pm

Related Questions

Are there any known hash algorithms which input a vector of int's and output
I need a table to store the state of a financial transaction. The state
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
So if I have to choose between a hash table or a prefix tree

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.