Solved

Query not working

Posted on 2013-06-21
19
445 Views
Last Modified: 2016-02-11
Hello there,

I have created this table from an excel sheet.
I imported the excel sheet in my db using SSIS which is fine

CREATE TABLE
    sheet
    (
        id BIGINT NOT NULL IDENTITY,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        District NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        City NVARCHAR(255) COLLATE Latin1_General_CI_AS,
    );


I need to create 4 tables from the above table.

Province
County(FK is Provicne table id)
District(FK is County table id)
City (FK is District table id)


I managed to create the first 3 tables,now when I am trying to link the district table to the city table with the districtID as FK in the city table I get this error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

UPDATE city
       SET districtID = 
       (Select d.ID
         FROM district as d 
        where d.districtname = city.district
            and city.county = d.county
            and d.province = city.province)

Open in new window


my city and district table is as follows

CREATE TABLE
    city
    (
        id BIGINT NOT NULL IDENTITY,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        District NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        cityname NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        districtID BIGINT
    );

Open in new window



CREATE TABLE
    district
    (
        id BIGINT NOT NULL IDENTITY,
        County NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        Province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        districtname NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        countyID BIGINT
    );

Open in new window

0
Comment
Question by:zolf
  • 10
  • 9
19 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267555
try either 'distinct' in that subquery

UPDATE city
       SET districtID =
       (Select DISTINCT d.ID
         FROM district as d
        where d.districtname = city.district
            and city.county = d.county
            and d.province = city.province)
0
 

Author Comment

by:zolf
ID: 39267556
thanks for your feedback, but it is still giving me that same error
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267559
not sure how you are going about normalizing that excel import - but this blog entry might give you some ideas. It uses cross apply and dense_rank() as way to normalize the data - which appears to be quite suitable here.

{+ edit sorry} and the url is
http://www.experts-exchange.com/blogs/PortletPaul/B_7511-Unpivotting-by-CROSS-APPLY-and-VALUES.html
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267561
>>but it is still giving me that same error
can I see some sample data perhaps?

you may also want to try this:

Select
  d.id
, d.districtname
, d.county
, d.province
, count(*)
FROM district as d
group by
  d.id
, d.districtname
, d.county
, d.province
having count(*) > 1
0
 

Author Comment

by:zolf
ID: 39267563
I am comparing the names of those two tables and if they are equal then I want to get the district id in tho the city table as fk
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267564
or this

Select
   d.districtname
, d.county
, d.province
, min(d.id)
, max(d.id)
, count(*)
FROM district as d
group by
   d.districtname
, d.county
, d.province
having count(*) > 1
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267566
>>I am comparing the names of those two tables and if they are equal then I want to get the district id in tho the city table as fk
yes, I understand what you are wanting

that subquery however is supplying more than one d.id
for the same   ( d.districtname , d.county , d.province ) combinations

which may indicate you have deeper problems in the method being used to normalize the data
0
 

Author Comment

by:zolf
ID: 39267569
that select query of yours returned 0 rows. I have attached the district and city table to get an idea
1.gif
0
 

Author Comment

by:zolf
ID: 39267571
by the way I have not assigned any constraints to the 4 tables. first I wanted to get the data in the tables and then apply the FK contraints to those tables and link them.
also I will delete the repeated col names in each tables later when I get the links
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267578
regrettably I can't read that script (arabic?) although I think I see repetition (eg rows 14-16
UPDATE city
SET districtID = (
        SELECT ID
        FROM (
                (
                SELECT MIN(d.ID) AS ID
                FROM district
                GROUP BY d.districtname
                    , d.county
                    , d.province
                )
         WHERE d.districtname = city.district
                AND city.county = d.county
                AND d.province = city.province
            )
        )

Open in new window

can you supply some source data - not by image (something I used in a db)
maybe a portion of the source Excel?
are you familiar with sqlfiddle?
http://sqlfiddle.com/
(it only permits a total of 8000 chars in DDL ~= 4000 of actual data)
0
 

Author Comment

by:zolf
ID: 39267592
I have zipped and exported the district,city table also the table called country which I imported from excel with SSIS. I have also attached the original excel. please help me to sort this issue.  I managed to get the 3 tables only stuck with this city table to link it to district
Desktop.zip
0
 

Author Comment

by:zolf
ID: 39267597
when I run that query of yours I get this error

 Incorrect syntax near the keyword 'WHERE'.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39267604
UPDATE city
SET districtID = (
        SELECT ID
        FROM (
                SELECT MIN(d.ID) AS ID, districtname , county, province
                FROM district
                GROUP BY districtname
                    , county
                    , province
                ) as d
         WHERE d.districtname = city.district
                AND city.county = d.county
                AND d.province = city.province
            )

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267606
there were some edits above, please ensure you use the latest
0
 

Author Comment

by:zolf
ID: 39267610
now I get this error

 The multi-part identifier "d.ID" could not be bound. 2) [Error Code: 207, SQL State: 42S22]  Invalid column name 'districtname'. 3) [Error Code: 207, SQL State: 42S22]  Invalid column name 'county'. 4) [Error Code: 207, SQL State: 42S22]  Invalid column name 'province'.
0
 

Author Comment

by:zolf
ID: 39267611
after using your latest edited query I get this error

The multi-part identifier "d.ID" could not be bound.
0
 

Author Comment

by:zolf
ID: 39267616
thanks a lot it worked,perfect. I replaced the d with district.ID and it worked.
0
 

Author Closing Comment

by:zolf
ID: 39267617
cheers mate
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267622
excellent!

sorry for the false starts

when you come to simplify those tables don't forget you have used min(id) in that update

I really would like to use that cross apply/dense_rank() approach for normalizing this data but maybe it's probably too late
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now