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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:52:24+00:00 2026-05-31T03:52:24+00:00

I have a bit of a complicated sql query I need to do, and

  • 0

I have a bit of a complicated sql query I need to do, and I’m a bit stuck. I’m using SQLite if that changes anything.

I have the following table structure:

Table G
---------
G_id (primary key) | Other cols ...
====================================
        21
        22
        23
        24
        25
        26
        27 (no g_to_s_map)
        28 

.

Table S
---------
S_id (primary key) |  S_num  | Other cols.....
====================================
        1              1101
        2              1102
        3              1103
        4              1104
        5              1105
        6              1106
        7              1107      (no g_to_s_map, no s_to_t_map)
        8              1108      (no g_to_s_map, there IS an s_to_t_map)
        9              1109      (there is an g_to_s_map, but no s_to_t map)

.

Table T
---------
T_id (primary key) | Other cols...
==================================
        1
        2

Then I also have two mapping tables:

Table G_to_S_Map (1:1 mapping, unique values of both g_id and s_id)
----------
G_id (foreign key ref g)| S_id (foreign key ref s)
===================================================
           21                        1  
           22                        2  
           23                        3  
           24                        4  
           25                        5  
           26                        6  
           28                        9

.

Table S_to_T_Map (many:1 mapping, many unique s_id to a t_id)
----------
S_id (foreign key ref s) | T_id (foreign key ref s)
===================================================
           1                         1    
           2                         1    
           3                         1    
           4                         2    
           5                         2    
           6                         2 
           8                         2

Given only a T_id and a G_id, I need to be able to update the G_to_S_Map with the first S_id corresponding to the specified T_id (in the S_to_T_Map) that is NOT in the G_to_S_Map

The first thing I was thinking of was just getting any S_id‘s that corresponded to the T_id in the S_to_T_Map:

SELECT S_id FROM S_to_T_Map where T_id = GIVEN_T_ID;

Then presumably I would join those values somehow with the G_to_S_Map using a left/right join maybe, and then look for the first value which doesn’t exist on one of the sides? Then I’d need to do an insert into the G_to_S_Map based on that S_id and the GIVEN_G_ID value or something.

Any suggestions on how to go about this? Thanks!


Edit: Added some dummy data:

  • 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-31T03:52:26+00:00Added an answer on May 31, 2026 at 3:52 am

    I believe this should work:

    INSERT INTO G_To_S_Map (G_id, S_id) 
              (SELECT :inputGId, a.S_id
               FROM S_To_T_Map as a
               LEFT JOIN G_To_S_Map as b
               ON b.S_id = a.S_id
               AND b.G_id = :inputGId
               WHERE a.T_id = :inputTId
               AND b.G_id IS NULL
               ORDER BY a.S_id
               LIMIT 1);
    

    EDIT:

    If you’re wanting to do the order by a different table, use this version:

    INSERT INTO G_To_S_Map (G_id, S_id) 
              (SELECT :inputGId, a.S_id
               FROM S_To_T_Map as a
               JOIN S as b
               ON b.S_id = a.S_id
               LEFT JOIN G_To_S_Map as c
               ON c.S_id = a.S_id
               AND c.G_id = :inputGId
               WHERE a.T_id = :inputTId
               AND c.G_id IS NULL
               ORDER BY b.S_num
               LIMIT 1);
    

    (As an aside, I really hope your tables aren’t actually named like this, because that’s a terrible thing to do. The use of Map, especially, should probably be avoided)


    EDIT:

    Here’s some example test data. Have I missed something? Did I conceptualize the relationships incorrectly?

    S_To_T_Map
    ================
    S_ID    T_ID    
       1       1    
       2       1    
       3       1    
       1       2    
       1       3    
       3       3 
    
    G_To_S_Map
    ==================   
    G_ID    S_ID  
       1       1  
       3       1  
       2       1  
       3       2  
       2       3  
       3       3  
    

    Resulting joined data:
    (CTEs used to generate cross-join test data)

    Results:
    =============================
    G_TEST    T_TEST    S_ID 
         1         1       3 
         2         1       2 
         1         3       3 
    

    EDIT:

    Ah, okay, now I get the problem. My issue was that I was assuming there was some sort of many-one relationship between S and G. As this is not the case, use this amended statement:

    INSERT INTO G_To_S_Map (G_id, S_id)  
          (SELECT :inputGId, a.S_id 
           FROM S_To_T_Map as a 
           JOIN S as b 
           ON b.S_id = a.S_id 
           LEFT JOIN G_To_S_Map as c 
           ON c.S_id = a.S_id 
           OR c.G_id = :inputGId
           WHERE a.T_id = :inputTId 
           AND c.G_id IS NULL 
           ORDER BY b.S_num 
           LIMIT 1); 
    

    Specficially, the line checking G_To_S_Map for a row containing the G_Id needed to be switched from using an AND to an OR – the key requirement which had not been specified previously was the fact that both G_Id and S_Id were unique in G_To_S_Map.
    This statement will not insert a line if either the provided G_Id has been mapped previously, or if all S_Ids mapped to the given T_Id have been mapped.

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

Sidebar

Related Questions

I have a little bit complicated query. I have table tbl_Categories {CategoryID, Name, CategoryId_fk}
I have a bit of a complicated structure that I have listed below. Basically
I have the following query that I need to convert to rails like syntax.
I have a Sql Server 2005 table with following data: idHearing is the primary
Bit of a complicated SQL question here. I currently have a SELECT statement which
I have some old C 32 Bit DLLs that are using Oracle's Pro C
We currently have a big project that consists of the following layers: MS SQL
The following SQL query that displays products sold sorted by cost and number of
I have some things to do, but is bit complicated for me.I need some
I have a MS SQL Query that is pulling data via from a remote

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.