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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:13:16+00:00 2026-05-23T01:13:16+00:00

Possible Duplicate: Howto build a SQL statement with using IDs that might not be

  • 0

Possible Duplicate:
Howto build a SQL statement with using IDs that might not be available in the table?

Hello together, I try to build a SQL statement with some unknown variables.
I would like to select the province, district, commune and village out of table dbo.pmd.
As there are only the IDs stored, I need to read the name out of the value table.
This is no problem for province and district as they will always be filled in table dbo.pmd. Anyhow the commune and village is not mandatory and might therefor be not filled.
This is where it comes to a problem if I compare the IDs between dbo.pmd and dbo.vhcs (communes) and dbo.pmd and dbo.vhv (villages) if there is no value stored in the dbo.pmd table.

The tables look like this:

table: dbo.pmd
fields: province, district, commune, village (stored value is the ID number)

table: dbo.vhp
fields: country_id, province_id, province_en (name of province)

table: dbo.vhd
fields: province_id, district_id, district_en (name of district)

table: dbo.vhc2
fields: district_id, commune_id, commune_en (name of commune)

table: dbo.vhv
fields: commune_id, village_id, village_en (name of commune)

My current statement looks like this:

SELECT pmd.patient_code, pmd.last_name, pmd.first_name, pmd.age, pmd.sex, pmd.province, pmd.district, pmd.commune, pmd.village 
FROM dbo.pmd AS pmd
INNER JOIN dbo.vhp AS vhp 
ON pmd.province = vhp.province_id
INNER JOIN dbo.vhd AS vhd
ON pmd.district = vhd.district_id 
INNER JOIN dbo.vhc2 AS vhc2
ON (pmd.commune is NULL OR (pmd.commune = vhc2.commune_id)) 
INNER JOIN dbo.vhv AS vhv
ON (pmd.village is NULL OR (pmd.village = vhv.village_id))
ORDER BY pmd.last_name

I have two entries in my database. One entry with commune_id and village_id = NULL and one with commune_id and village_id = some ID number.

I have stopped the execution of this statement after ~2minutes and there are: 8273612 rows all with the same values, showing only the entry with commund_id and village_id equals NULL.

I am using Microsoft SQL Server 2008.

  • 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-23T01:13:17+00:00Added an answer on May 23, 2026 at 1:13 am
        INNER JOIN dbo.vhc2 AS vhc2
    ON (pmd.commune is NULL OR (pmd.commune = vhc2.commune_id)) 
    

    You are inadvertently doing something like a cartesian join here. On a row where pmd.commune is NULL, pmd.commune IS NULL evaluates as true for every row in vhc2, causing every single row in vhc2 to be joined to that pmd row.

    Your scenario this time around is a little bit different from what you described in your previous question, which is why the solution offered there is not helping you in your current situation.

    Some variation on an outer join is what you want for vhc2 & vhv, e.g.:

    LEFT OUTER JOIN dbo.vhc2 AS vhc2
    ON  pmd.commune = vhc2.commune_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: How to Build a custom simple DNS server in C/C++ As the
Possible Duplicate: How to open-source an application that uses API keys I have an
This might be a duplicate question as I have found a few that are
Possible Duplicate: Convert HTML to PDF in .NET I am using VSTS 2008 +
Possible Duplicate: 'Contains()' workaround using Linq to Entities? I am using the System.Linq.Dynamic library
Possible Duplicate: How to build multi oop functions in PHP5 Hey, I've seen this
Possible Duplicate: DateTimePicker: pick both date and time I'm using a WinForm DateTime picker
Possible Duplicate: howto return a array in a c++ method? How can an array

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.