Solved

Query not working

Posted on 2013-06-21
19
449 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

820 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