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

  • Home
  • SEARCH
  • 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 8965687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:51:51+00:00 2026-06-15T16:51:51+00:00

I’m somewhat of a newbie to SQL queries, especially anything containing logic, and although

  • 0

I’m somewhat of a newbie to SQL queries, especially anything containing logic, and although I’ve searched for hours finding the exact terms to search for is not easy in this case! I have a relatively simple one, I’m sure:

A table has 2 columns, and each row contains data about a function in a program. Some functions have a parent function associated (for grouping). Column A is the unique function ID. Column B indicates, when applicable, the parent function’s ID. All parent function IDs are independent and valid function IDs that exist elsewhere in column A.

For reporting purposes I need to list the functions grouped by their parent ID, listing the parent function with the child functions. I can easily report by parent function ID, but the problem is that a parent function does not know that it is a parent function because its column B is empty!

What I need to do is complete the value in Column B if it is empty and the function is referenced elsewhere as a parent function.

Otherwise stated, for each row that has a null value in Column B:

  1. Take the value from column A
  2. Check for the existence of that value in ANY row on column B
  3. If there is a match, inject the value into column B (so that Column A and B have the same value)

What I have: (Query: SELECT function_id, parent_function FROM functions)

FUNCTION_ID   PARENT_FUNCTION   
4  
13            4  
79  
138           4
195
314           345
345

What I need to have:

FUNCTION_ID   PARENT_FUNCTION
4             4
13            4
79
138           4
195
314           345
345           345

Any Ideas? I can’t wait to get more familiar with SQL! Thanks ahead of time.

  • 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-15T16:51:51+00:00Added an answer on June 15, 2026 at 4:51 pm

    This should work for you:

    UPDATE functions
    SET parent_function = function_id
    WHERE parent_function IS NULL
    AND function_id IN (SELECT parent_function FROM functions)
    

    This will set parent_function equal to function_id where it has not yet been set, and where it appears somewhere in the parent_function column.

    If you don’t actually want to modify the table data but still return values that you need, you can use similar logic like this:

    SELECT f.function_id, COALESCE(f.parent_function, f2.function_id) as parent_function
    FROM functions f
    LEFT JOIN functions f2
      ON f.function_id = f2.function_id
      AND f2.function_id IN (SELECT parent_function FROM functions)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im still somewhat of a newbie on jQuery and the ajax scene, but I
I have a question that may sound odd, but being somewhat of a newbie,
Hello I'm somewhat past the newbie stage of javascript and am looking at how
I'm not afraid to admit that I'm somewhat of a C++ newbie, so this
I have a Django project, and I'm somewhat of a newbie in it. I
Although somewhat related to this question , I have what I think is a
this is somewhat a newbie question probably. I'm generating keypairs with Java: KeyPairGenerator keyGen
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
I'm somewhat of a Rails newbie. I'm writing a couchrest-rails app, so am not
Although somewhat experienced with writing Winforms applications, the... vagueness of WPF still eludes me

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.