473,383 Members | 1,997 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,383 software developers and data experts.

SQL Assistance required

I am trying to obtain some data and I have a way to do it however it
appears really longwinded and I'm sure there must be a better way to
get this using a join of some type without such a complex statement.

tbl1
--------
sid
role_id

tbl2
---------
sid
region_id

in tbl1 I have two types of roles

What I want to do is get all the sid's where role_id=role1 and where
region_id = all regions connected to sid=role2

Here is the rubbish attempt I have so far that works but can surely be
improved:

SELECT tbl2.sid
FROM tbl2
WHERE tbl2.region_id in
(SELECT tbl2.region_id
FROM tbl1
WHERE tbl1.sid = 8)
GROUP BY tbl2.sid

I also want to exclude sid=8 from the results as this is already
prevalent...

Thanks in advance
Apr 2 '08 #1
1 1514
On Apr 3, 1:24*pm, Ed Murphy <emurph...@socal.rr.comwrote:
shannonwhi...@hotmail.com wrote:
I am trying to obtain some data and I have a way to do it however it
appears really longwinded and I'm sure there must be a better way to
get this using a join of some type without such a complex statement.
tbl1
--------
sid
role_id
tbl2
---------
sid
region_id
in tbl1 I have two types of roles
What I want to do is get all the sid's where role_id=role1 and where
region_id = all regions connected to sid=role2

I assume you mean "region_id is one of the regions connected" etc.
Here is the rubbish attempt I have so far that works but can surely be
improved:
SELECT tbl2.sid
FROM tbl2
WHERE tbl2.region_id in
* *(SELECT tbl2.region_id
* *FROM tbl1
* *WHERE tbl1.sid = 8)
GROUP BY tbl2.sid
I also want to exclude sid=8 from the results as this is already
prevalent...

Where are role1 and role2? *Anyway, moving away from hardcoded sid
values that happen to be appropriate in one specific example, and
back to the more general approach that you mentioned earlier:

select *t2.sid
from * *tbl2 t2
* join *tbl1 t1 on t2.sid = t1.sid
where * t1.role_id = 'role1'
* and * t2.region_id in (
* * * * * * * * select *other_t2.region_id
* * * * * * * * from * *tbl2 other_t2
* * * * * * * * * join *tbl1 other_t1 on other_t2.sid = other_t1.sid
* * * * * * * * where * other_t1.role_id = 'role2'
* * * * )- Hide quoted text -

- Show quoted text -
Perfect.
Thanks
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kevin Michael Reed | last post by:
------=_NextPart_000_002B_01C34DF8.C34C94F0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Assistance required. I have 3 tables:
5
by: Bill | last post by:
Good Day; I would appreciate assistance developing a query that I haven't been able to develop without using a second table. I wish to count the number of records that are still open on the...
0
by: Jawahar | last post by:
All I had posted this in the remote assistance group and could not get any replies so I thought that I could try in the developer group Thanks One of the issues we face when helping our remote...
3
by: Kenny G | last post by:
I have the below listed code in a text box: Private Sub TypeofSurgery_BeforeUpdate(Cancel As Integer) Dim strMsg As String Dim strTitle As String Dim intStyle As Integer If...
1
by: Methven | last post by:
Hello Folks, I am having a small problem and I wonder if someone might be able to assist me. It is with the Importing of Text files into MS-Access 2000. It is specifically with the Importing...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
23
by: Bloke at the pennine puddle | last post by:
This query is for me, but I think that it'll be of use to just about everyone who participates in this news group. I learn better if I have a decent example to study, so I'm looking for publicly...
4
by: Sugartonic | last post by:
I have been reading for about three days now on how to use a drop down menu with options to write to my database but still haven't been successful, Basically I made a drop down menu for age. made a...
1
by: cottcody | last post by:
Hello. I'm attempting to write a form for a web page which is using the formmail.pl script. The form works fine and all but it its the script in which i am having troubles. What I would like it to do...
3
by: gaurav92K | last post by:
sir i am working in a company . there are many pc. i want to use remote assistance. i configure all group policy which are related remote assistance.and i enable service through remote in system...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.