473,498 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

There has gotta be a better way

I ended up writing the folling code to get a query to work:

create table #PI (prot_inv_id int, inv_type int, protocol_ID int)
insert into #PI (prot_inv_id, inv_type, protocol_id)
(select Prot_inv_ID, inv_type, protocol_ID
from Protocol_investigator where inv_type in (1,2))

SELECT distinct Protocols.Protocol_ID, Protocols.HICNumber,
Protocols.title,
(select Prot_inv_ID from #PI where Inv_Type = 1
and #PI.Protocol_ID = Protocols.Protocol_ID) as Investigator,
(select Prot_inv_ID from #PI Where Inv_Type = 2
and #PI.Protocol_ID = Protocols.Protocol_ID) as Coordinator,
Protocols.Active
FROM dbo.Protocols
LEFT OUTER JOIN #PI
ON dbo.Protocols.protocol_id = #PI.protocol_id

But I'm sure there is a way to do this without first building a temp
table. When I tried to put a where clause in the main body of the SQL
I would, despite the outer join, end up getting only those records
from both tables that matched. That is to say when I moved the
select Prot_inv_ID, inv_type, protocol_ID
from Protocol_investigator where inv_type in (1,2))
as a where clause I came up short.

Does anyone know how to essentially include a subselect to an outer
join?

Thanks,
Tom

Jul 20 '05 #1
2 1777
On Fri, 16 Jan 2004 11:10:43 -0500, Tom Loach <tc***@virginia.edu> wrote:
I ended up writing the folling code to get a query to work:

create table #PI (prot_inv_id int, inv_type int, protocol_ID int)
insert into #PI (prot_inv_id, inv_type, protocol_id)
(select Prot_inv_ID, inv_type, protocol_ID
from Protocol_investigator where inv_type in (1,2))

SELECT distinct Protocols.Protocol_ID, Protocols.HICNumber,
Protocols.title,
(select Prot_inv_ID from #PI where Inv_Type = 1
and #PI.Protocol_ID = Protocols.Protocol_ID) as Investigator,
(select Prot_inv_ID from #PI Where Inv_Type = 2
and #PI.Protocol_ID = Protocols.Protocol_ID) as Coordinator,
Protocols.Active
FROM dbo.Protocols
LEFT OUTER JOIN #PI
ON dbo.Protocols.protocol_id = #PI.protocol_id

But I'm sure there is a way to do this without first building a temp
table. When I tried to put a where clause in the main body of the SQL
I would, despite the outer join, end up getting only those records
from both tables that matched. That is to say when I moved the
select Prot_inv_ID, inv_type, protocol_ID
from Protocol_investigator where inv_type in (1,2))
as a where clause I came up short.

Does anyone know how to essentially include a subselect to an outer
join?

Thanks,
Tom


What you need to do is include the filter criteria in the join ON expression.
Jul 20 '05 #2
Hi

Why not (untested) :

SELECT distinct P.Protocol_ID, P.HICNumber,
P.title,
CASE when PI.Inv_Type = 1 THEN PI.Prot_inv_ID END as Investigator,
CASE when PI.Inv_Type = 1 THEN PI.Prot_inv_ID END as Coordinator,
P.Active
FROM dbo.Protocols P
LEFT OUTER JOIN Protocol_investigator PI ON P.protocol_id = PI.protocol_id
AND PI.inv_type in (1,2)

John

"Tom Loach" <tc***@virginia.edu> wrote in message
news:mv********************************@4ax.com...
I ended up writing the folling code to get a query to work:

create table #PI (prot_inv_id int, inv_type int, protocol_ID int)
insert into #PI (prot_inv_id, inv_type, protocol_id)
(select Prot_inv_ID, inv_type, protocol_ID
from Protocol_investigator where inv_type in (1,2))

SELECT distinct Protocols.Protocol_ID, Protocols.HICNumber,
Protocols.title,
(select Prot_inv_ID from #PI where Inv_Type = 1
and #PI.Protocol_ID = Protocols.Protocol_ID) as Investigator,
(select Prot_inv_ID from #PI Where Inv_Type = 2
and #PI.Protocol_ID = Protocols.Protocol_ID) as Coordinator,
Protocols.Active
FROM dbo.Protocols
LEFT OUTER JOIN #PI
ON dbo.Protocols.protocol_id = #PI.protocol_id

But I'm sure there is a way to do this without first building a temp
table. When I tried to put a where clause in the main body of the SQL
I would, despite the outer join, end up getting only those records
from both tables that matched. That is to say when I moved the
select Prot_inv_ID, inv_type, protocol_ID
from Protocol_investigator where inv_type in (1,2))
as a where clause I came up short.

Does anyone know how to essentially include a subselect to an outer
join?

Thanks,
Tom

Jul 20 '05 #3

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

Similar topics

42
2540
by: Irmen de Jong | last post by:
Pickle and marshal are not safe. They can do harmful things if fed maliciously constructed data. That is a pity, because marshal is fast. I need a fast and safe (secure) marshaler. Is xdrlib the...
1
4137
by: Norman Ackroyd | last post by:
Ok, so I'm working on a Perl proggy to convert Net.Data to JSP (gotta love Perl.) I've got a Module that has a Class to store all relevant information about a Net.Data user defined function that is...
11
19715
by: Matt Slay | last post by:
In what application does one write all these ASP programs? NOTEPAD? All the examples of code I see are all just text. Is there not an IDE interface, such as what VB or Visual Foxpro has for...
43
42733
by: Dimitri Debruyne | last post by:
Hi group I am in the process of developing a website in XHTML Strict and CSS. Is there a way to open a link in a new window without the use of frames or Javascript or something ? I didn't find a...
22
2800
by: David Sterling | last post by:
After much futzing about with the XML/XSD for the Search.Response, I had to resort to this... PLEASE Tell me there is a better way! private void ExecuteAQuery(string strLookingForWhat) {...
6
2640
by: joebob | last post by:
I've got two forms, Form1 and Form2. Form1 opens invisibly when the database opens. From Form1 (or from a regular module or class module accessed by Form1), is there a way to detect when Form2...
8
1576
by: Hasani | last post by:
The reason I ask this is because I'm having problems accessing an array of type TrackSelection in a VBS script I'm using to test my code. item.AdditionalInformation returns an array of type...
5
1388
by: MC felon | last post by:
Hi, im MC felon.. tell me how to do this... how do i access ALT, CTRL or SHIFT without needing to press another key with it? like, i want comp to understand (or rather, recognize) just CTRL...
2
1454
by: Ron | last post by:
Hi All, Is there a way to print out a list of all controls within tables listing their attributes? I need to know how I've got all the controls set up, type of control, whether they're...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
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,...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.