If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > SQL Query !

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-14-11, 13:53
Ju12Ju Ju12Ju is offline
Registered User
 
Join Date: Dec 2011
Posts: 7
Post SQL Query !

Hi there.. I am new to this SQL query writing.

I have a table ROADS in which i need to filter out some records. The table SS is attached with this post.

As you can see, there are several entries having same S_GISID but different H_surveyed_length. I need to filter out unique S_GISID records which has the higher H_surveyed_length.

Please guide. And suggest me some good tutorials to study these.

Thanks !
Attached Thumbnails
SQL Query !-table.png  
Reply With Quote
  #2 (permalink)  
Old 12-14-11, 13:59
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
>As you can see,
no, not really.

too bad COPY & PASTE are broken for you.
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #3 (permalink)  
Old 12-14-11, 14:08
Ju12Ju Ju12Ju is offline
Registered User
 
Join Date: Dec 2011
Posts: 7
Cant see?.. The attachment is right there.. My bad !

Anyways, here ImageShack® - Online Photo and Video Hosting
Reply With Quote
  #4 (permalink)  
Old 12-14-11, 14:15
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Please post your table definition as CREATE TABLE statements.

And ideally some sample data (as INSERT INTO) and the expected output that you want to see.
Reply With Quote
  #5 (permalink)  
Old 12-14-11, 14:30
Ju12Ju Ju12Ju is offline
Registered User
 
Join Date: Dec 2011
Posts: 7
Alright !

create table roads (ID nvarchar2(255), Length number(38,8));
insert into roads values (22,34.45);
insert into roads values (22,56.75);
insert into roads values (22,23.32);
insert into roads values (22,12.98);

ID Length
22 34.45
22 56.75
31 23.32
31 12.98

The expected output from this table must have one record for each unique ID with has bigger value in length field.

ID Length
22 56.75
31 23.32

????

Last edited by Ju12Ju; 12-14-11 at 14:39.
Reply With Quote
  #6 (permalink)  
Old 12-14-11, 14:40
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Code:
SELECT r1.id, r1.length
FROM roads r1
WHERE r1.length = (SELECT max(r2.length) 
                   FROM roads r2
                   WHERE r2.id = r1.id)
Reply With Quote
  #7 (permalink)  
Old 12-14-11, 14:46
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
It is bad idea to have column name the same as Oracle defined function "LENGTH".
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #8 (permalink)  
Old 12-14-11, 15:55
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Another option:
Code:
select r.id, max(r.length) max_len
from roads r
group by r.id;
Reply With Quote
  #9 (permalink)  
Old 12-15-11, 02:38
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by Littlefoot View Post
Another option:
Code:
select r.id, max(r.length) max_len
from roads r
group by r.id;
And much better than my solution
Reply With Quote
  #10 (permalink)  
Old 12-22-11, 13:40
Ju12Ju Ju12Ju is offline
Registered User
 
Join Date: Dec 2011
Posts: 7
Thanks a lot guys I am slowly learning these things !.. Can you please tell me whats wrong with the following simple query?

update ww.pr14postcodes a set a.cwregioncode='EN' where b.regioncode='EN' from BND.cwbWorkRegion b and SDO_INSIDE(a.shape,b.shape)='TRUE';

I just do not know where to give the "from BND.cwbWorkRegion b".. It keeps throwing me errors as "SQL command not properly ended"..

pls !?
Reply With Quote
  #11 (permalink)  
Old 12-22-11, 13:42
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
UPDATE statement never contains "FROM"
only SELECT contains FROM clause
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #12 (permalink)  
Old 12-22-11, 13:43
Ju12Ju Ju12Ju is offline
Registered User
 
Join Date: Dec 2011
Posts: 7
Yeah.. Okay.. how shall i workout this scenario then?

I need to give the second table name & its alias name somwhere..
Reply With Quote
  #13 (permalink)  
Old 12-22-11, 13:46
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
Code:
UPDATE ww.pr14postcodes a
SET    a.cwregioncode = 'EN'
WHERE  b.regioncode = 'EN'
       AND bnd.cwbworkregion != b
       AND Sdo_inside(a.shape, b.shape) = 'TRUE';
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #14 (permalink)  
Old 12-22-11, 13:50
Ju12Ju Ju12Ju is offline
Registered User
 
Join Date: Dec 2011
Posts: 7
Nope.. It throws the following..

Code:
ORA-00904: "B"."SHAPE": invalid identifier
But the following select command works well and gives output !
Code:
select a.postcode from ww.pr14postcodes a, BND.cwbWorkRegion b 
where b.regioncode='EN' 
and 
SDO_INSIDE(A.shape,B.shape) = 'TRUE'
Reply With Quote
  #15 (permalink)  
Old 12-22-11, 21:30
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
Code:
UPDATE ww.pr14postcodes a
SET    a.cwregioncode = 'EN'
WHERE  EXISTS (SELECT a.postcode
               FROM   ww.pr14postcodes a,
                      bnd.cwbworkregion b
               WHERE  b.regioncode = 'EN'
                      AND Sdo_inside(a.shape, b.shape) = 'TRUE')
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On