473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with complex query, any gurus or geniuses around?

I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated

this is the table, with sample data

------------------
| x_id | y_id |
------------------
| 3 | 9 |
------------------
| 1 | 2 |
------------------
| 7 | 1 |
------------------
| 1 | 4 |
------------------
| 3 | 7 |
------------------
| 4 | 9 |
------------------
| 1 | 9 |
------------------


The query must find a value, let's say 9 in column y_id
it should return
------------------
| 3 | 9 |
------------------
| 4 | 9 |
------------------
| 1 | 9 |
------------------
however there is a catch, the query must then return all y_id's of the x_id's it found in the first query but ignoring the 9, it would return
------------------
| 1 | 2 |
------------------
| 1 | 4 |
------------------
| 3 | 7 |
------------------
because the x_ids of 1 and 3 were found earlier

then to make it even more complicated, it must find only certain values from y_id, lets say 2 is ok but 4 and 7 are not. so in the end we have
------------------
| 1 | 2 |
------------------

if you understand what I posted, can you provide the query code?
I'm completely lost!

much appreciated and thanks for your time
Sep 30 '09 #1

✓ answered by code green

The simpler way to go seems to be nested sub-queries.
Is it just the final result you are after?

Looks to me like you are using algebra to read a graph.
As with some algebraic problems you need to work backwards then forwards

Try something like
Expand|Select|Wrap|Line Numbers
  1. /*it must find only certain values from y_id, lets say 2 is ok but 4 and 7 are not.*/
  2. SELECT x_id y_id FROM graph
  3. WHERE y_id IN (2) AND y_id IN 
  4. /*the query must then return all y_id's of the x_id's it found in the first query but ignoring the 9*/
  5. (SELECT y_id FROM graph WHERE y_id IN 
  6. (SELECT y_id FROM graph WHERE y_id NOT IN (9)
  7. AND y_id IN (SELECT y_id FROM grapg WHERE x_id = 9))
Don't expect this to work first time.
Test the sub queries seperatately then two together then three together.
Hope I have at least helped you on your way

3 2183
code green
1,726 Expert 1GB
The simpler way to go seems to be nested sub-queries.
Is it just the final result you are after?

Looks to me like you are using algebra to read a graph.
As with some algebraic problems you need to work backwards then forwards

Try something like
Expand|Select|Wrap|Line Numbers
  1. /*it must find only certain values from y_id, lets say 2 is ok but 4 and 7 are not.*/
  2. SELECT x_id y_id FROM graph
  3. WHERE y_id IN (2) AND y_id IN 
  4. /*the query must then return all y_id's of the x_id's it found in the first query but ignoring the 9*/
  5. (SELECT y_id FROM graph WHERE y_id IN 
  6. (SELECT y_id FROM graph WHERE y_id NOT IN (9)
  7. AND y_id IN (SELECT y_id FROM grapg WHERE x_id = 9))
Don't expect this to work first time.
Test the sub queries seperatately then two together then three together.
Hope I have at least helped you on your way
Oct 1 '09 #2
thank you very much for the reply and your time
I will investigate your solution

much appreciated
Oct 1 '09 #3
code green

I wanted to thank you for your reply and example
it worked exactly as I needed

very much appreciated!
Oct 4 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
7
by: JN | last post by:
Hello. Sorry about the length of this post. I am trying to implement a hash table for my chess program, and decided to try using the VC++ . NET (Dinkumware) version of hash_map. The role of the...
1
by: steven.fafel | last post by:
I have been working with SQL for a while...but I am stumped. I can not seem to get my arms around this query....can anyone help... Here it is: Table = 12 rows, 4 columns (id, name, amount,...
5
by: MGFoster | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've converted an ACC97 .mdb file to an ACC2K2 .adp. A report that worked in ACC97 doesn't work in ACC2K2. Report setup: ACC97 ...
4
by: Mark | last post by:
the Following bit of code doesn't work. It seems to respond to the second, starting with 'add iif statement for Good Practice', but not to the first, starting 'add iif statement for archived' ...
2
by: NigelMThomas | last post by:
I have an especially challenging problem. I know there are a few geniuses in this group so perhaps; you can advise me whether or not this can be done as an update query in Access. Thanks. I am...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
24
by: cameltoer | last post by:
Need a good tutorial on Classes, OOP, Template systems, etc, because absolutely nothing, and I mean nothing looks familiar to my procedural based programming that has served me for years. Should I...
5
by: binky | last post by:
Question for all you SQL gurus out there. I have a (seemingly) complex query to write, and since I'm just learning SQL server, am not sure how to go about it. Tables: t_trans t_account All...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.