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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:11:19+00:00 2026-06-06T14:11:19+00:00

I am trying to write the query for existing table data. TABLE_A and TABLE_B

  • 0

I am trying to write the query for existing table data. TABLE_A and TABLE_B has one record but TABLE_C has 2 records. One for Home address and other is for Work address.

So the following query returns 2 records. I am trying to get only one record out of 2 records.

If CITY is NULL, state_id is null for address_type = 1(Home) then get Work(address_type = 2) address. If both are null then get ‘Home’ address. What is the best way to achieve this functionality.

Thank you for any suggestion.

select a.A_ID, a.B_ID, a.A_DESC, b.first_name, b.last_name, c.address_type,  c.city, c.state
from table_A a
left join table_B b on b.B_ID = a.B_ID
left join table_C c on c.B_id = b.B_id
where a.A_ID = 10

TABLE_A

A_ID int
B_ID int 
A_Desc  varchar(20)

TABLE_B

B_ID int
first_name  varchar(30)
last_name   varchar(30)

TABLE_C

C_ID    int
B_ID    int 
address_type    int 
city      varchar(50)
state  int

Result:

 A_ID     B_ID   A_DESC   first_name  last_name  address_type   city       state
 --------------------------------------------------------------------------------
 10       200    test_     name1        name_last    1            NULL       NULL 
 10       200    test_     name1        name_last    2           City_test    2

I want this final result

 A_ID     B_ID   A_DESC   first_name  last_name  address_type   city       state
 --------------------------------------------------------------------------------
 10       200    test_     name1        name_last    2           City_test    2
  • 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-06T14:11:21+00:00Added an answer on June 6, 2026 at 2:11 pm
    select a.A_ID, a.B_ID, a.A_DESC, b.first_name, b.last_name,
           coalesce(c1.address_type,c2.address_type) address_type, 
           coalesce(c1.city,c2.city) city, 
           coalesce(c1.state,c2.state) state
    from table_A a
    left join table_B b on b.B_ID = a.B_ID
    left join table_C c1 on c.B_id = b.B_id and c.Address_Type = 1
    left join table_C c2 on c.B_id = b.B_id and c.Address_Type = 2
    
    where a.A_ID = 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a query to find records which don't have a matching record
I am trying to write a query to create a 'table' of data as
I'm trying to write a query that extracts and transforms data from a table
I am trying to write a query that connects 3 tables. The first table
I am trying to write a query expression to parse an XML tree, but
Im trying to write an SQL query that will check in the table ‘persons’
I am trying to write a query for retrieving data from SQLite database. I
I have trying to write a query in Access 2010. I have a table:
I am trying to write a query for SQL Server 2005 but I can't
I am trying write a query that will display if the person has anyone

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.