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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:34:08+00:00 2026-06-18T12:34:08+00:00

here is an example of my two table. PROBLEM: How can I create SQL

  • 0

here is an example of my two table.

enter image description here

PROBLEM:
How can I create SQL Query using left join?

HERE IS THE SCENARIO

As I’ve said earlier, I have two table (TABLE1 and TABLE2),
I tried to use left join so that i can combine both UserID
in one table

so here is the code

select * from table1 a left join table2 on a.userid = b.userid

so two tables are now combined.

what i need to do is this:
if the status is all complete then ‘complete’
then if status contains complete and incomplete then ‘incomplete’
else ‘no status’

it should be look like this.

enter image description here

NOTE:
since UserID = 1 (table1) contains complete and incomplete status (table2)
then it display ‘incomplete’ (new column)

since UserID = 4 (table1) contains all complete status (table 2)
then it display ‘completed’ (new column)

———————————–

WHAT IF I CHANGE THE STATUS TO INTEGER?

enter image description here

same procedure. thanks

  • 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-18T12:34:09+00:00Added an answer on June 18, 2026 at 12:34 pm
    SELECT  a.*, 
            CASE WHEN b.totalCount = 1 AND b.totalINC = 0 THEN 'Complete'
                 WHEN totalCount IS NULL THEN ''
                 ELSE 'Incomplete'
            END STatus
    FROM    table1 a
            LEFT JOIN
            (
                SELECT  UserID, 
                        COUNT(DISTINCT STATUS) totalCount,
                        SUM(CASE WHEN status = 'Incomplete' THEN 1 ELSE 0 END) totalINC
                FROM table2
                GROUP BY UserID
            ) b ON a.UserID = b.UserID
    
    • SQLFiddle Demo

    UPDATE 1

    the only thing you’ll change is the CASE

    SELECT  a.*, 
            CASE WHEN b.totalCount = 1 AND b.totalINC = 0 THEN 'Complete'
                 WHEN totalCount IS NULL THEN ''
                 ELSE 'Incomplete'
            END STatus
    FROM    table1 a
            LEFT JOIN
            (
                SELECT  UserID, 
                        COUNT(DISTINCT STATUS) totalCount,
                        SUM(CASE WHEN status <> 100 THEN 1 ELSE 0 END) totalINC
                FROM table2
                GROUP BY UserID
            ) b ON a.UserID = b.UserID;
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the example from the wiki has phantom.exit() in two places here. Why can't
Here's my problem : my site has users, which can create projects, and access
I have problem about django template-db query relationship. For example, I created two tables
Here is a simple example illustrating my problem: Say I have two models Car
I have two tables, images and image_data and here is an example of my
Here's a contrived example: I want to divide two numbers and I need to
Example: here is the string: blablabla123:550:404:487blablabla500:488:474:401blablablabla here is what I'm using: string reg =
Problem I'm trying to build a minimal example where I create a many-to-many relationship
Here is the most relevant part of my database schema: create table TEST (
I'm having a problem with performance with the entity framework. Here's the scenario. I

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.