• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

Query not working

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
zolf
Asked:
zolf
  • 10
  • 9
1 Solution
 
PortletPaulfreelancerCommented:
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
 
zolfAuthor Commented:
thanks for your feedback, but it is still giving me that same error
0
 
PortletPaulfreelancerCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
PortletPaulfreelancerCommented:
>>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
 
zolfAuthor Commented:
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
>>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
 
zolfAuthor Commented:
that select query of yours returned 0 rows. I have attached the district and city table to get an idea
1.gif
0
 
zolfAuthor Commented:
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
 
PortletPaulfreelancerCommented:
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
 
zolfAuthor Commented:
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
 
zolfAuthor Commented:
when I run that query of yours I get this error

 Incorrect syntax near the keyword 'WHERE'.
0
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
there were some edits above, please ensure you use the latest
0
 
zolfAuthor Commented:
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
 
zolfAuthor Commented:
after using your latest edited query I get this error

The multi-part identifier "d.ID" could not be bound.
0
 
zolfAuthor Commented:
thanks a lot it worked,perfect. I replaced the d with district.ID and it worked.
0
 
zolfAuthor Commented:
cheers mate
0
 
PortletPaulfreelancerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now