Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Left join with parameter option

 
   Database Help (Home) -> Programming RSS
Next:  integrity check large db  
Author Message
danbridgland

External


Since: Feb 05, 2009
Posts: 4



(Msg. 1) Posted: Tue Feb 17, 2009 7:51 am
Post subject: Left join with parameter option
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi, I'm stuck with a fairly simple query (at least I think it might
be).

I need to retreive a selection of case_numbers that match the criteria
set by a parameter value, but also I want to be able to bypass that
part of the criteria if the parameter is '0' (ideally) or Null

The problem is that the table is a Right join.

Is this possible?



Here is my code

DECLARE @DESIG TABLE (state_id varchar(4))
--INSERT INTO @DESIG (state_id)
--SELECT 'PL'
INSERT INTO @DESIG (state_id)
SELECT 'ES'
--INSERT INTO @DESIG (state_id)
--SELECT '0'
--INSERT INTO @DESIG (state_id)
--SELECT 'NULL'


SELECT PAT_REPORT_TABLE.CASE_NUMBER,
DESIGNATED_STATES.STATE_ID,
(Select [dbo].[wr_designations_list] (PAT_CASE.CASE_ID)) as
DESIGNATED_COUNTRYS_LIST
FROM PAT_CASE
INNER JOIN PAT_REPORT_TABLE
ON PAT_CASE.CASE_ID = PAT_REPORT_TABLE.CASE_ID
INNER JOIN STATE_NAME
ON PAT_CASE.STATE_ID = STATE_NAME.STATE_ID
AND STATE_NAME.LANGUAGE_ID = 3
RIGHT OUTER JOIN DESIGNATED_STATES
ON DESIGNATED_STATES.CASE_ID = PAT_CASE.CASE_ID
AND DESIGNATED_STATES.STATE_ID IN (SELECT * FROM @DESIG) ---///
** THIS IS WHERE I WANT TO ADD THE PARAMETER OPTION

WHERE (PAT_CASE.STATE_ID IN ('EP'))
ORDER BY PAT_REPORT_TABLE.CASE_NUMBER




Thanks for any assistance

 >> Stay informed about: Left join with parameter option 
Back to top
Login to vote
danbridgland

External


Since: Feb 05, 2009
Posts: 4



(Msg. 2) Posted: Tue Feb 17, 2009 7:57 am
Post subject: Re: Left join with parameter option [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Oops, Sorry, Please ignore the obvious mistake in the subject, It
should have been 'Right join with parameter option'

Regards
Dan

 >> Stay informed about: Left join with parameter option 
Back to top
Login to vote
Russell Fields

External


Since: Jan 10, 2008
Posts: 608



(Msg. 3) Posted: Tue Feb 17, 2009 11:21 am
Post subject: Re: Left join with parameter option [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dan,

If I understand correctly, if the parameter = 0 then you want all states to
be included. Perhaps something like the following

....
RIGHT OUTER JOIN DESIGNATED_STATES
ON DESIGNATED_STATES.CASE_ID = PAT_CASE.CASE_ID
AND (DESIGNATED_STATES.STATE_ID IN (SELECT * FROM @DESIG)
OR EXISTS (SELECT * FROM @DESIG WHERE state_id = 0))
....


RLF


wrote in message

> Hi, I'm stuck with a fairly simple query (at least I think it might
> be).
>
> I need to retreive a selection of case_numbers that match the criteria
> set by a parameter value, but also I want to be able to bypass that
> part of the criteria if the parameter is '0' (ideally) or Null
>
> The problem is that the table is a Right join.
>
> Is this possible?
>
>
>
> Here is my code
>
> DECLARE @DESIG TABLE (state_id varchar(4))
> --INSERT INTO @DESIG (state_id)
> --SELECT 'PL'
> INSERT INTO @DESIG (state_id)
> SELECT 'ES'
> --INSERT INTO @DESIG (state_id)
> --SELECT '0'
> --INSERT INTO @DESIG (state_id)
> --SELECT 'NULL'
>
>
> SELECT PAT_REPORT_TABLE.CASE_NUMBER,
> DESIGNATED_STATES.STATE_ID,
> (Select [dbo].[wr_designations_list] (PAT_CASE.CASE_ID)) as
> DESIGNATED_COUNTRYS_LIST
> FROM PAT_CASE
> INNER JOIN PAT_REPORT_TABLE
> ON PAT_CASE.CASE_ID = PAT_REPORT_TABLE.CASE_ID
> INNER JOIN STATE_NAME
> ON PAT_CASE.STATE_ID = STATE_NAME.STATE_ID
> AND STATE_NAME.LANGUAGE_ID = 3
> RIGHT OUTER JOIN DESIGNATED_STATES
> ON DESIGNATED_STATES.CASE_ID = PAT_CASE.CASE_ID
> AND DESIGNATED_STATES.STATE_ID IN (SELECT * FROM @DESIG) ---///
> ** THIS IS WHERE I WANT TO ADD THE PARAMETER OPTION
>
> WHERE (PAT_CASE.STATE_ID IN ('EP'))
> ORDER BY PAT_REPORT_TABLE.CASE_NUMBER
>
>
>
>
> Thanks for any assistance
 >> Stay informed about: Left join with parameter option 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 4) Posted: Tue Feb 17, 2009 11:23 am
Post subject: Re: Left join with parameter option [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Not very clear on your requirements, but seems something like this
should do what you need:

....
AND (DESIGNATED_STATES.STATE_ID IN (SELECT state_id FROM @DESIG)
OR EXISTS(SELECT *
FROM @DESIG
WHERE state_id = '0'))
....

--
Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Left join with parameter option 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
left join/inner join/outer join with repeat values - hi david, thanks for the reply. basically what i need this for is: there are two tables: table1 is a master table, table2 is a detail table. table1's primary_id is present in details_id of table2 table1 has only 1 record table2 could have many..

Left join is equal to inner join? - Hi All I try to calculate the result by using left join but it still returns the output same as using inner join. ************************************ Table1(T1) Status_id Score_id SumOfAmount Chief Excellent 1 Chief Fair 1 Chief ....

Left join - On a Sql Server 2005, suppose the query SELECT ...... FROM A LEFT JOIN B ON B.Col1 = A.Col2 LEFT JOIN C ON (C.Col3 = A.Col4 OR C.Col5 = B.Col6) If the left join on B results in 1 or more rows, then I get all the rows from C: the rows that apply to the....

LEFT JOIN question - Hi, I have table A and I create a left join to table B: SELECT * from tableA a LEFT JOIN tableB b ON a.id=b.id... and I get all the records from table A, that is 100 records for example. If I create join to table C: SELECT * from tableA a LEFT JOIN....

use "left join" or "not exists" ? - Using SQL Server 2000, Example : == create table jtest1 (id int primary key, col1 varchar(10)) create table jtest2 (id2 int primary key, col2 varchar(10)) insert jtest1 values (1, 'hello') insert jtest1 values (2, 'two') insert jtest2 values (1,..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]