Author |
Topic |
Blip
Starting Member
5 Posts |
Posted - 2012-03-14 : 05:02:00
|
Hi All,I have a nested query that needs to reference a field from the outer query. (SLCustomerAccount.CustomerNumber - Highlighted in red below) Can anyone give me an idea how to do this?Here's my SQLSELECT SLCustomerAccount.CustomerAccountNumber, SLCustomerAccount.CustomerAccountName, SYSTraderAnalysisValue.Name as BusinessType, (SELECT SYSTraderAnalysisValue.NameFROM SLCustomerAnalysisHeadValue INNER JOIN SLCustomerAccount ON SLCustomerAnalysisHeadValue.SLCustomerAccountID = SLCustomerAccount.SLCustomerAccountID INNER JOIN SYSTraderAnalysisValue ON SLCustomerAnalysisHeadValue.SYSTraderAnalysisValueID = SYSTraderAnalysisValue.SYSTraderAnalysisValueIDWHERE (SLCustomerAnalysisHeadValue.SYSTraderAnalysisHeadingID = 1062 AND (SLCustomerAccount.CustomerAccountNumber= '00000003'))) as RepresentativeFROM SLCustomerAnalysisHeadValue INNER JOIN SLCustomerAccount ON SLCustomerAnalysisHeadValue.SLCustomerAccountID = SLCustomerAccount.SLCustomerAccountID INNER JOIN SYSTraderAnalysisValue ON SLCustomerAnalysisHeadValue.SYSTraderAnalysisValueID = SYSTraderAnalysisValue.SYSTraderAnalysisValueIDWHERE (SLCustomerAnalysisHeadValue.SYSTraderAnalysisHeadingID = 1385)The '00000003' needs to reference the result of SLCustomerAccount.CustomerAccountNumberThanks for any help. |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-14 : 05:35:16
|
use different alias names for your tables No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Blip
Starting Member
5 Posts |
Posted - 2012-03-14 : 05:39:14
|
They are differentas BusinessTypeas Representative? |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-14 : 05:48:03
|
use different alias names for your tables not columns No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Blip
Starting Member
5 Posts |
Posted - 2012-03-14 : 06:34:22
|
Resolved Many thanks for your help |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-03-14 : 06:44:21
|
Here is the easy rewriteSELECT ca.CustomerAccountNumber, ca.CustomerAccountName, av.Name AS BusinessType, ( SELECT z.Name FROM dbo.SLCustomerAnalysisHeadValue AS x INNER JOIN dbo.SLCustomerAccount AS y ON y.SLCustomerAccountID = x.SLCustomerAccountID INNER JOIN dbo.SYSTraderAnalysisValue AS z ON z.SYSTraderAnalysisValueID = x.SYSTraderAnalysisValueID WHERE x.SYSTraderAnalysisHeadingID = 1062 AND ca.CustomerAccountNumber = '00000003' ) AS RepresentativeFROM dbo.SLCustomerAnalysisHeadValue AS hvINNER JOIN dbo.SLCustomerAccount AS ca ON ca.SLCustomerAccountID = hv.SLCustomerAccountIDINNER JOIN dbo.SYSTraderAnalysisValue AS av ON av.SYSTraderAnalysisValueID = hv.SYSTraderAnalysisValueIDWHERE hv.SYSTraderAnalysisHeadingID = 1385 N 56°04'39.26"E 12°55'05.63" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-03-14 : 06:48:12
|
And here is one for performanceSELECT MAX(CASE WHEN hv.SYSTraderAnalysisHeadingID = 1385 THEN ca.CustomerAccountNumber ELSE NULL END) AS CustomerAccountNumber, MAX(CASE WHEN hv.SYSTraderAnalysisHeadingID = 1385 THEN ca.CustomerAccountName ELSE NULL END) AS CustomerAccountName, MAX(CASE WHEN hv.SYSTraderAnalysisHeadingID = 1385 THEN av.Name ELSE NULL END) AS BusinessType, MAX(CASE WHEN hv.SYSTraderAnalysisHeadingID = 1062 AND ca.CustomerAccountNumber = '00000003' THEN av.Name ELSE NULL END) AS RepresentativeFROM dbo.SLCustomerAnalysisHeadValue AS hvINNER JOIN dbo.SLCustomerAccount AS ca ON ca.SLCustomerAccountID = hv.SLCustomerAccountIDINNER JOIN dbo.SYSTraderAnalysisValue AS av ON av.SYSTraderAnalysisValueID = hv.SYSTraderAnalysisValueIDWHERE hv.SYSTraderAnalysisHeadingID IN (1062, 1385) N 56°04'39.26"E 12°55'05.63" |
 |
|
Blip
Starting Member
5 Posts |
Posted - 2012-03-14 : 08:56:50
|
Thanks SwePeso |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-03-14 : 11:19:22
|
Did you notice a performance gain? N 56°04'39.26"E 12°55'05.63" |
 |
|
Blip
Starting Member
5 Posts |
Posted - 2012-03-15 : 05:18:27
|
Yes considerable Many thanks for the help |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-03-15 : 05:20:36
|
Any numbers to share to us? N 56°04'39.26"E 12°55'05.63" |
 |
|
|