Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Conditional table names is SELECT statement

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2008-02-15 : 04:56:22
I've got this statement


SELECT * FROM
(SELECT P.vehicleref,P.manufacturer,P.model,P.derivative,P.additionalfreetext,P.isLimited,
P.isPriceReduction,P.isSpecial, P.created, P.updated, NMF.term, NMF.milespa,
NMF.maintained, NMF.ch,p.source,
ROW_NUMBER() OVER ( PARTITION BY P.VehicleRef ORDER BY NMF.ch ) AS MinCH
FROM ( SELECT * FROM vwAllMatrixWithLombardSimple ) P
INNER JOIN dbPubMatrix..tblNewMatrixFigures NMF ON P.VehicleRef = NMF. [VehicleRef]
WHERE type='car' AND LOCatCode IN (1,2,3,4,5,6)) Q WHERE MinCH = 1
ORDER BY ch ASC


The figures table (dbPubMatrix..tblNewMatrixFigures) is different depending on the source field in vwAllMatrixWithLombardSimple.

I would like to implement this logic into the statement

If P.source = 'WEB' Then
Join to table dbPubMatrix..tblWebMatrixFigures
Else
Join to table dbPubMatrix..tblNewMatrixFigures

Could anyone suggest if this is possible.

Thanks


(EDITED BY JEFF TO ADD LINE BREAKS TO THE CODE TAG)


madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-15 : 05:01:47
Why dont you write two seperate select statements based on the condition?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-15 : 05:02:20
Time to rethink about the table design when you are forced to join to different tables based on some condition.

You can try this:

Select * from Tbl1 t1 join tblWebMatrixFigures t2 on t1.key = t2.key and t1.source = 'WEB'
union all
Select * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key and t1.source <> 'WEB'


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-15 : 05:02:56
or you need to use dynamic sql with table name being passed from value of a variable.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 05:10:36
quote:
Originally posted by harsh_athalye

Select * from Tbl1 t1 join tblWebMatrixFigures t2 on t1.key = t2.key and t.source = 'WEB'
union all
Select * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key and t.source <> 'WEB'

Equals

Select * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key
where t.source is not null



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 05:14:29
[code]SELECT *
FROM (
SELECT P.vehicleref,
P.manufacturer,
P.model,
P.derivative,
P.additionalfreetext,
P.isLimited,
P.isPriceReduction,
P.isSpecial,
P.created,
P.updated,
NMF.term,
NMF.milespa,
NMF.maintained,
NMF.ch,
p.source,
ROW_NUMBER() OVER (PARTITION BY P.VehicleRef ORDER BY NMF.ch) AS MinCH
FROM vwAllMatrixWithLombardSimple AS P
INNER JOIN dbPubMatrix..tblNewMatrixFigures AS NMF ON NMF.VehicleRef = P.VehicleRef
WHERE {table alias here}.type = 'car'
AND {table alias here}.LOCatCode IN (1, 2, 3, 4, 5, 6)
) AS Q
WHERE q.MinCH = 1
ORDER BY q.ch ASC[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-15 : 05:15:06
quote:
Originally posted by Peso

quote:
Originally posted by harsh_athalye

Select * from Tbl1 t1 join tblWebMatrixFigures t2 on t1.key = t2.key and t.source = 'WEB'
union all
Select * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key and t.source <> 'WEB'

Equals

Select * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key
where t.source is not null



E 12°55'05.25"
N 56°04'39.16"




How come? There is no LEFT JOIN involved.
And where is tblWebMatrixFigures table?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2008-02-15 : 05:16:05
Thanks for your help, I think i'm close with this

I've got these two statements

SELECT * FROM (SELECT P.vehicleref,P.manufacturer,P.model,P.derivative,P.additionalfreetext,P.isLimited,P.isPriceReduction,P.isSpecial, P.created, P.updated, NMF.term, NMF.milespa, NMF.maintained, NMF.ch,p.source, ROW_NUMBER() OVER ( PARTITION BY P.VehicleRef ORDER BY NMF.ch ) AS MinCH FROM ( SELECT * FROM vwAllMatrixWithLombardSimple WHERE source <> 'web') P INNER JOIN dbPubMatrix..tblNewMatrixFigures NMF ON P.VehicleRef = NMF. [VehicleRef] WHERE type='car' AND LOCatCode IN (1,2,3,4,5,6)) Q WHERE MinCH = 1 ORDER BY ch ASC

SELECT * FROM (SELECT P.vehicleref,P.manufacturer,P.model,P.derivative,P.additionalfreetext,P.isLimited,P.isPriceReduction,P.isSpecial, P.created, P.updated, NMF.term, NMF.milespa, NMF.maintained, NMF.ch,p.source, ROW_NUMBER() OVER ( PARTITION BY P.VehicleRef ORDER BY NMF.ch ) AS MinCH FROM ( SELECT * FROM vwAllMatrixWithLombardSimple WHERE source = 'web') P INNER JOIN dbPubMatrix..tblWebMatrixFigures NMF ON P.VehicleRef = NMF. [VehicleRef] WHERE type='car' AND LOCatCode IN (1,2,3,4,5,6)) Q WHERE MinCH = 1 ORDER BY ch ASC

Both the queries work fine on their own and return the correct results. However when I add UNION ALL between them I get incorrect syntax near the word UNION.

What have I done wrong?

Thanks


Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-15 : 05:21:52
[code]SELECT * FROM
(
SELECT
P.vehicleref,P.manufacturer,P.model,P.derivative,P.additionalfreetext,P.isLimited,P.isPriceReduction,P.isSpecial,
P.created, P.updated, NMF.term, NMF.milespa, NMF.maintained, NMF.ch,p.source,
ROW_NUMBER() OVER ( PARTITION BY P.VehicleRef ORDER BY NMF.ch ) AS MinCH
FROM
(
SELECT * FROM vwAllMatrixWithLombardSimple
WHERE source <> 'web'
) P INNER JOIN dbPubMatrix..tblNewMatrixFigures NMF ON P.VehicleRef = NMF. [VehicleRef]
WHERE type='car' AND LOCatCode IN (1,2,3,4,5,6)
) Q WHERE MinCH = 1
UNION ALL
SELECT * FROM
(
SELECT
P.vehicleref,P.manufacturer,P.model,P.derivative,P.additionalfreetext,P.isLimited,P.isPriceReduction,P.isSpecial,
P.created, P.updated, NMF.term, NMF.milespa, NMF.maintained, NMF.ch,p.source,
ROW_NUMBER() OVER ( PARTITION BY P.VehicleRef ORDER BY NMF.ch ) AS MinCH
FROM
(
SELECT * FROM vwAllMatrixWithLombardSimple
WHERE source = 'web'
) P INNER JOIN dbPubMatrix..tblWebMatrixFigures NMF ON P.VehicleRef = NMF. [VehicleRef]
WHERE type='car' AND LOCatCode IN (1,2,3,4,5,6)
) Q WHERE MinCH = 1
ORDER BY ch ASC[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 05:24:28
quote:
Originally posted by harsh_athalye

How come? There is no LEFT JOIN involved.
And where is tblWebMatrixFigures table?
Think about it.
You are SELECTing the exact same records regardingn to JOINing.
The only thing that is different is the t.Source filter.

For first SELECT you want all records where Source IS EQUAL TO 'Web'.
In second SELECT you want all records where Source IS NOT EQUAL TO 'Web'.

Which mean you got all records not equal to NULL.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-15 : 05:31:13
quote:
Originally posted by Peso

quote:
Originally posted by harsh_athalye

How come? There is no LEFT JOIN involved.
And where is tblWebMatrixFigures table?
Think about it.
You are SELECTing the exact same records regardingn to JOINing.
The only thing that is different is the t.Source filter.

For first SELECT you want all records where Source IS EQUAL TO 'Web'.
In second SELECT you want all records where Source IS NOT EQUAL TO 'Web'.

Which mean you got all records not equal to NULL.



E 12°55'05.25"
N 56°04'39.16"




Yes that's right. But there are three tables which needs to be joined based on filter.
In your example, there was only tblNewMatrixFigures table, but what about other one?


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 05:33:59
Ahh.. Now I see the difference in view names.
Good spot!



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2008-02-15 : 05:38:20
Thanks a lot guys, got it working by changing the table aliases to P2 and NMF2 in the second statement. Works great. Thanks again
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 05:49:17
Really? That was the only thing you changed?
You didn't remove the space here "NMF. [VehicleRef]" ?


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-15 : 07:11:59
Simply use two left outer joins. see:

http://weblogs.sqlteam.com/jeffs/archive/2007/04/03/Conditional-Joins.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 07:23:03
Interesting. How to you cope with the
NMF.term,
NMF.milespa,
NMF.maintained,
NMF.ch
part of the SELECT query? If you use 2 LEFT JOIN, will they not become 8 columns, not 4?




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-15 : 07:28:10
Jeff, do you mean something like this?
SELECT		q.vehicleref,
q.manufacturer,
q.model,
q.derivative,
q.additionalfreetext,
q.isLimited,
q.isPriceReduction,
q.isSpecial,
q.created,
q.updated,
case when q.source = 'web' then q.term2 else q.term1 end AS term
case when q.source = 'web' then q.milespa2 else q.milespa1 end AS milespa
case when q.source = 'web' then q.maintained2 else q.maintained1 end AS maintained
case when q.source = 'web' then q.ch2 else q.ch1 end AS ch,
q.source
FROM (
SELECT P.vehicleref,
P.manufacturer,
P.model,
P.derivative,
P.additionalfreetext,
P.isLimited,
P.isPriceReduction,
P.isSpecial,
P.created,
P.updated,
NMF1.term1,
NMF1.milespa1,
NMF1.maintained1,
NMF1.ch1,
p.source,
NMF2.term2,
NMF2.milespa2,
NMF2.maintained2,
NMF2.ch2,
ROW_NUMBER() OVER (PARTITION BY case when p.source = 'web' then 0 else 1 end, P.VehicleRef ORDER BY NMF.ch) AS MinCH
FROM vwAllMatrixWithLombardSimple AS p
LEFT JOIN dbPubMatrix..tblNewMatrixFigures as NMF1 ON NMF1.[VehicleRef] = P.VehicleRef
AND p.source <> 'web'
LEFT JOIN dbPubMatrix..tblWebMatrixFigures AS NMF2 ON NMF2.[VehicleRef] = P.VehicleRef
AND p.source = 'web'
WHERE p.type = 'car'
AND p.LOCatCode IN (1, 2, 3, 4, 5, 6)
) as Q
WHERE q.MinCH = 1
order by case when q.source = 'web' then q.ch2 else q.ch1 end



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-15 : 09:59:38
Yep, more or less. Without the schema or any idea exactly what this should be returning or how it should be working, hard to me to guess, but that's the overall approach, as mentioned in my blog post.

JOINS should be constants, you rarely, if ever, have an OR or a condition on the JOIN itself. If you need to join to different things on different conditions, then simply do multiple LEFT OUTER JOINS and pick which columns you need from those joins based on that condition.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -