Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2013, 04:36 PM
rg20 rg20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 rg20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 57 sec
Reputation Power: 0
Oracle query

ok I am through pulling my hair out and decided to let you guys help me .

I have 2 tables
Table 1
ID - Container - internalid
100 A01001 1
101 A01002 3
102 A01003 2
103 A01001 4
104 A01003 5

Table 2
ID Container ShortName
1 A01001 A01
2 A01002 A01
3 A01003 A01

My problem is this
1 - I start with the internal id of 1
2 - I need ot find the only container in table 1 with 1 record

Expected output = 102

My attempts have been
select
id, container
from
table 1
where
substr(container,1,9) in
(
select substr(container,1,9)
from table 1
where ID in (1)
) and
having count(container) = 1
group by id, container -- I really don't want to group here, but have to and this screws up the query
order by container


Thanks in advance

Reply With Quote
  #2  
Old February 8th, 2013, 04:55 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,685 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 44 m 12 sec
Reputation Power: 284
I'm not sure why you need table2 at all, so maybe I didn't understand your question completely.

But this returns those rows where there is only one container:
Code:
select id, 
       container, 
       internalid
from (
   select id, 
          container,
          internalid,
          count(*) over (partition by container) as cnt
   from table_1
) 
where cnt = 1
__________________
I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
http://forums.devshed.com/misc.php?do=bbcode#code

Tips on how to ask better questions:
http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
http://wiki.postgresql.org/wiki/SlowQueryQuestions
http://catb.org/esr/faqs/smart-questions.html

Reply With Quote
  #3  
Old February 9th, 2013, 09:42 AM
rg20 rg20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 rg20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 57 sec
Reputation Power: 0
shammat,

Thanks for the response. I did not give a good sample dataset for the question.

Table 1
ID - Container - internalid
100 A01001 1
101 A01002 3
102 A01003 2
103 A01001 4
104 A01003 5
105 A01004 6
106 A02001 7
107 A02001 8
108 A02002 9
109 A02003 10

Now the query that you gave will return
A01002, A01004 A02002 and A02003. I need to return only one value per substr(container,1,3).
so we could get either A01002 or A01004. (it doesn't matter which one.)

Since table 2 has the A01, A02,A03 values, I thought it would be eaiser to use that table aid in the joins.

If I try to do a filter on substr(container,1,3) I end up with what I need, however once I group on the container, it reverts back to the all the values per container.

Reply With Quote
  #4  
Old February 16th, 2013, 10:53 AM
soniya_dawle soniya_dawle is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 soniya_dawle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 25 sec
Reputation Power: 0
Hi,

I created a table (table_1) with the following data as posted in your first question:
100 A01001 1
101 A01002 3
102 A01003 2
103 A01001 4
104 A01003 5

If I have understood your question correctly then you wish to find the container having a unique value in table_1 , i.e no other row has that container value. So as per the data above, that row should be the one where internal id is 3. Because the container value of A01002 is not present in other rows. Please see my query below:

select id , container,internalid from table_1 where container in (
select t1.container
from table_1 t1
group by t1.container
having count(t1.container) <= 1);

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Oracle query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap