Solved

query not working as expected

Posted on 2013-06-19
5
196 Views
Last Modified: 2013-06-21
Hello there,

I have this query

UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county
INNER JOIN county on county.province = district.province;

Open in new window


which should compare 2 cols in county table and district table and if they are same then get the id of the county table and insert in the district countyid col. but it is not working. it is only checking for the province name and inserting the id.
0
Comment
Question by:zolf
5 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39258936
not sure about this, could you try this as a select first

--UPDATE district
--SET district.countyID = c.ID

select c.id, d.*

FROM district as d
INNER JOIN county AS c  ON district.county = c.county
AND district.province = c.province
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 39259179
UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county
INNER JOIN county on county.province = district.province;




which should compare 2 cols in county table and district table and if they are same then get the id of the county table and insert in the district countyid col. but it is not working. it is only checking for the province name and inserting the id.


hi,

as per my understanding,
you are joining two tables with country and province.

so common row only will update, why you joining twice?
UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county
AND c.province = district.province;

Open in new window


if you want to update based on country then
UPDATE district
SET district.countyID = county.ID
FROM county as c INNER JOIN district ON c.county = district.county

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39259797
Can you provide some sample data for the two tables and your expected results after the update?
0
 
LVL 5

Accepted Solution

by:
DOSLover earned 500 total points
ID: 39260350
Hi Zolf,
Based on what you are saying, it looks like ID is the Primary/Unique key on County table. The District table has two columns (among many) -  County and Province - using which you can uniquely get a County record. The District table also has column named CountyId which for some reason is not correct (or may be a new column you have added). If that is true, the following statement should help to set the correct CountyId.

UPDATE district d
       SET countyID = 
       (Select c.ID
         FROM county c 
        where c.county = d.county
            and c.province = d.province)

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 39267531
cheers
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question