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.
| Author |
Topic |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-02-15 : 04:56:22
|
I've got this statementSELECT * 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 statementIf P.source = 'WEB' ThenJoin to table dbPubMatrix..tblWebMatrixFiguresElseJoin to table dbPubMatrix..tblNewMatrixFiguresCould 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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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 allSelect * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key and t1.source <> 'WEB' Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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. |
 |
|
|
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 allSelect * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key and t.source <> 'WEB'
EqualsSelect * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.keywhere t.source is not null E 12°55'05.25"N 56°04'39.16" |
 |
|
|
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 QWHERE q.MinCH = 1ORDER BY q.ch ASC[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
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 allSelect * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.key and t.source <> 'WEB'
EqualsSelect * from Tbl1 t1 join tblNewMatrixFigures t2 on t1.key = t2.keywhere 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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-02-15 : 05:16:05
|
| Thanks for your help, I think i'm close with thisI've got these two statementsSELECT * 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 ASCSELECT * 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 ASCBoth 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 |
 |
|
|
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 = 1UNION ALLSELECT * 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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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" |
 |
|
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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" |
 |
|
|
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 |
 |
|
|
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" |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-15 : 07:23:03
|
Interesting. How to you cope with theNMF.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" |
 |
|
|
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.sourceFROM ( 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 QWHERE q.MinCH = 1order by case when q.source = 'web' then q.ch2 else q.ch1 end E 12°55'05.25"N 56°04'39.16" |
 |
|
|
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.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
|
|
|
|
|