Solved

Query not working

Posted on 2013-06-21
19
444 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 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

17 Experts available now in Live!

Get 1:1 Help Now