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 |
|
mikebird
Aged Yak Warrior
529 Posts |
Posted - 2009-06-09 : 12:20:07
|
| Please remind me the syntax for a join when you need more than one ON criteria?Do you put on x = xAND on y = y? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-09 : 12:23:03
|
yup you can put like this. only single on and others separated by and/oron x=xand y=yor...and... |
 |
|
|
mikebird
Aged Yak Warrior
529 Posts |
Posted - 2009-06-10 : 08:20:51
|
| BUT what IF I did a left join on a table much earlier in a query and now I want to add a different criteria to the join which would be in terms of an inner join, really?Not only this, but the extra additional table I made comes right at the end of the joins, of course. When I want to add another condition to an already-linked table, this is in the middle of the joins, and I assume the parser goes through the joins in order, and when it sees the new fields (added at the end) not recognised, this query is invalid. Tried moving this join to the end, but then its crucial join setup is missing in the middle!!So, the question is: How do I add a new join condition to an existing, already linked table, later. Rewriting everything from scratch may be an option, but still bebuilding everything line-by-line may not work at all, with what I want to do, and is there a way to fiddle a solution with a quick fix?newest join at the end. Want to add conditions to the second left join, and it's there, after the ANDerror: Msg 4104, Level 16, State 1, Line 1The multi-part identifier "p2.pr_usrchar1" could not be bound.This is query:select Centre_Name, centre_no, p2.pr_usrchar1, p2.prcode2, pr_sort_key, stk_sort_key, stk_sort_key3, det_type, det_quantity, det_unit_price, det_year, st_nett, nl_cat_code4, nl_categorynamefrom financetemplate.dbo.sl_pl_nl_detail detinner join financetemplate.dbo.sl_accounts sla on sla.cucode = det.det_account -- taking 1 second, still 53,344 rowsinner join asdandat.dbo.tbl_centre c on sla.cucode = c.centre_noinner join asdandat.dbo.tbl_net_centre nc on c.centre_id = nc.centre_ID -- after these two, 3 seconds, 176,399 rowsleft join financetemplate.dbo.prc_price_recs p on p.prcode = det.det_price_code left join financetemplate.dbo.stk_stock s on s.stkcode = det.det_stock_code and s.stk_sort_key3 = p2.pr_usrchar1inner join financetemplate.dbo.sl_transactions t on det.det_header_ref = t.st_header_refinner join financetemplate.dbo.prc_price_recs2 p2 on p2.prcode2 = p.prcodeinner join financetemplate.dbo.nl_category nlc on right(det.det_analysis, 3) = nlc.nl_cat_code4where c.reg_status = 'R'and (pr_sort_key = 'CANDIDATE FEES'or stk_sort_key in ('STUDENT BOOKS','RESOURCE BOOKLETS') -- These two lines should give Workbooksand det.det_quantity > 5) and ((nl_cat_code4 between '102' and '106' and pr_usrchar1 = 'A')or (nl_cat_code4 between '141' and '146' and pr_usrchar1 = 'B')or (nl_cat_code4 between '201' and '231' and pr_usrchar1 = 'E')or (nl_cat_code4 = '701' and pr_usrchar1 = 'J')or (nl_cat_code4 between '703' and '707' and pr_usrchar1 in ('Y','X','S','W'))--or (nl_cat_code4 in ('302','303') and pr_usrchar1 = 'D')or (nl_cat_code4 between '304' and '311' and pr_usrchar1 in ('F','P','H','I','G','M','L','K')))order by pr_usrchar1, nl_categoryname--and nl_cat_code4 != '™'--and t.st_description = 'CANDIDATE REGISTRATIONS' |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-10 : 08:28:38
|
Post some samples. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-10 : 14:23:37
|
quote: Originally posted by mikebird BUT what IF I did a left join on a table much earlier in a query and now I want to add a different criteria to the join which would be in terms of an inner join, really?Not only this, but the extra additional table I made comes right at the end of the joins, of course. When I want to add another condition to an already-linked table, this is in the middle of the joins, and I assume the parser goes through the joins in order, and when it sees the new fields (added at the end) not recognised, this query is invalid. Tried moving this join to the end, but then its crucial join setup is missing in the middle!!So, the question is: How do I add a new join condition to an existing, already linked table, later. Rewriting everything from scratch may be an option, but still bebuilding everything line-by-line may not work at all, with what I want to do, and is there a way to fiddle a solution with a quick fix?newest join at the end. Want to add conditions to the second left join, and it's there, after the ANDerror: Msg 4104, Level 16, State 1, Line 1The multi-part identifier "p2.pr_usrchar1" could not be bound.This is query:select Centre_Name, centre_no, p2.pr_usrchar1, p2.prcode2, pr_sort_key, stk_sort_key, stk_sort_key3, det_type, det_quantity, det_unit_price, det_year, st_nett, nl_cat_code4, nl_categorynamefrom financetemplate.dbo.sl_pl_nl_detail detinner join financetemplate.dbo.sl_accounts sla on sla.cucode = det.det_account -- taking 1 second, still 53,344 rowsinner join asdandat.dbo.tbl_centre c on sla.cucode = c.centre_noinner join asdandat.dbo.tbl_net_centre nc on c.centre_id = nc.centre_ID -- after these two, 3 seconds, 176,399 rowsleft join financetemplate.dbo.prc_price_recs p on p.prcode = det.det_price_code left join financetemplate.dbo.stk_stock s on s.stkcode = det.det_stock_code and s.stk_sort_key3 = p2.pr_usrchar1inner join financetemplate.dbo.sl_transactions t on det.det_header_ref = t.st_header_refinner join financetemplate.dbo.prc_price_recs2 p2 on p2.prcode2 = p.prcodeinner join financetemplate.dbo.nl_category nlc on right(det.det_analysis, 3) = nlc.nl_cat_code4where c.reg_status = 'R'and (pr_sort_key = 'CANDIDATE FEES'or stk_sort_key in ('STUDENT BOOKS','RESOURCE BOOKLETS') -- These two lines should give Workbooksand det.det_quantity > 5) and ((nl_cat_code4 between '102' and '106' and pr_usrchar1 = 'A')or (nl_cat_code4 between '141' and '146' and pr_usrchar1 = 'B')or (nl_cat_code4 between '201' and '231' and pr_usrchar1 = 'E')or (nl_cat_code4 = '701' and pr_usrchar1 = 'J')or (nl_cat_code4 between '703' and '707' and pr_usrchar1 in ('Y','X','S','W'))--or (nl_cat_code4 in ('302','303') and pr_usrchar1 = 'D')or (nl_cat_code4 between '304' and '311' and pr_usrchar1 in ('F','P','H','I','G','M','L','K')))order by pr_usrchar1, nl_categoryname--and nl_cat_code4 != '™'--and t.st_description = 'CANDIDATE REGISTRATIONS'
here the table refered by p2 is coming after the left join where you refers it. try putting the left join after this join statement where you define p2 |
 |
|
|
|
|
|
|
|