Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query not working

Posted on 2013-06-21
19
Medium Priority
?
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
19 Comments
 
LVL 49

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 49

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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 49

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 49

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 49

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
 
LVL 49

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 49

Accepted Solution

by:
PortletPaul earned 2000 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 49

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 49

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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

721 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