Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

using IN and EXIST in a complex Oracle SQL Statment

Posted on 2011-09-27
7
Medium Priority
?
380 Views
Last Modified: 2012-05-12
I need to remove records from a child table where the parent table has field called removed set to true.  Normally this be a straight forward change however there is versioning so for I need to leave both the child versions and the parent version of the where removed is set to F (false) in tact.

here is what I have so far but it still pulling all versions (starting with a select statement to see what I pull vs running the delete statment and getting into trouble...if someone could help me with the select statement I greatly apprciate.)


SELECT ANALYSIS, NAME, VERSION  from COMPONENT where EXISTS (SELECT Name from Analysis WHERE  COMPONENT.ANALYSIS = ANALYSIS.NAME  and REMOVED = 'T') ORDER BY ANALYSIS

Open in new window

0
Comment
Question by:gilnari
[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
  • 4
  • 2
7 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36709363
Just to make sure I understand, can you provide some sample data and expected results?

I'm not understanding where removed='F' comes into play since you have "and REMOVED = 'T'"
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36709425
Given component with analysis X

if there is an analysis.removed T and an analysis.removed F  then your exists will return true  for either the false or the true condition.

Try this query to check for that condition


SELECT   c.analysis, c.name, c.version, MIN(a.removed), MAX(a.removed)
    FROM component c, analysis a
   WHERE c.analysis = a.name
ORDER BY analysis

if min and max are the same, then IN/EXISTS will work.  If min and max are different then it won't work because both T and F will find matches
0
 

Author Comment

by:gilnari
ID: 36709676
There are two tables Analysis and Component.  In the Analysis Table we have Name, Version, Removed.  In the Component table (child) we have Analysis, ComponentName, Version).  
Analysis.Name = Component.Analysis
Analysis.Version = Compoenent.Versoin

I need to  remove all Components from the Components table where Version is the Same abetween the tables nd where Removed = 'T' in the Analysis Table.

Example Data.Analysis Table (Analysis Name, Version, Removed)
XYZ , 1, T
XYZ, 2, T
YXZ, 3, F

Component  (analysis, componentname, version)
XYZ, Start, 1
XYZ, Start, 2
XYZ, Start, 3

I want the select statement to return for the Comnpent Table
XYZ, Start, 1
XYZ, STart, 2

Eventually this will become a delete statmenet where only thing left in the component table is XYZ, Start, 3 and in the analysis table XYZ, 3, F
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 74

Expert Comment

by:sdstuber
ID: 36709719
select * from component
where (analysis,version) in (select name,version from analysis where removed='T')
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 36709728
or

select * from component c
where exists
(select null from analysis a
where a.removed='T'
and a.name = c.analysis
and a.version=c.version)
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36709740
or

select c.* from component c, analysis a
where a.removed='T'
and a.name = c.analysis
and a.version=c.version


note this one will work for the data you have above, but is not functionally equivalent  in general to the other two
0
 

Author Closing Comment

by:gilnari
ID: 36709975
perfect...just what I need
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

610 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