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 |
|
RonanKeating
Starting Member
3 Posts |
Posted - 2011-03-08 : 00:02:57
|
| hi all, Does anybody can help to spot where I'm making the mistake with following code, cause SSMSE 2005 complains about the incorrect syntax near the keyword "AND".UPDATE eSET e.prop_type = a.row_nFROM ( SELECT d.elm_type, d.elm_prop, ROW_NUMBER() OVER(ORDER BY d.elm_type ASC, d.elm_prop ASC) as row_n FROM ( SELECT DISTINCT elm_type, elm_prop FROM [elements] WHERE prop_type IS NOT NULL ) d) a LEFT OUTER JOIN [elements] e ON e.elm_type = a.elm_type, AND e.elm_prop = a.elm_propRegards, |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-08 : 00:20:26
|
Remove comma before ANDquote: Originally posted by RonanKeating hi all, Does anybody can help to spot where I'm making the mistake with following code, cause SSMSE 2005 complains about the incorrect syntax near the keyword "AND".UPDATE eSET e.prop_type = a.row_nFROM ( SELECT d.elm_type, d.elm_prop, ROW_NUMBER() OVER(ORDER BY d.elm_type ASC, d.elm_prop ASC) as row_n FROM ( SELECT DISTINCT elm_type, elm_prop FROM [elements] WHERE prop_type IS NOT NULL ) d) a LEFT OUTER JOIN [elements] e ON e.elm_type = a.elm_type, AND e.elm_prop = a.elm_propRegards,
|
 |
|
|
RonanKeating
Starting Member
3 Posts |
Posted - 2011-03-08 : 04:27:02
|
| thanks in advance that works now,I already have SQL books and I'm studying it but there are still difficulties for my perception for example with the posted code.Here is indented order and my interpretation for the posted code. As far as I understand when interpreting I should start from inner-most brackets/paranthesis.1. UPDATE e2. SET e.prop_type = a.row_n3. FROM (4. SELECT d.elm_type, d.elm_prop,5. ROW_NUMBER() OVER(ORDER BY d.elm_type ASC, d.elm_prop ASC) as row_n6. FROM (7. SELECT DISTINCT elm_type, elm_prop8. FROM [elements]9. WHERE prop_type IS NOT NULL10. ) d11. ) a12. LEFT OUTER JOIN [elements] e13. ON e.elm_type = a.elm_type,14. AND e.elm_prop = a.elm_propA) 6. to 10. lines defines inner-most distinct table and provide the resulting dataset to 3. line SQL statement as variable "d".B)3. line performs the selection based on the input from "d" result data set, and here it somehow implements the Row_Number custom function and returns the result as variable "row-n". Question comes in mind that can we call like that ORDER BY in 5.th line ?C.) and all the returned dataset is returned as input to LEFT OUTTER JOIN as "a" variable.I'm experiencing difficulties when trying to comrise the logic of that nested SQL statements. Could point out which is the best way?Please comment on my interpretations |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-08 : 06:03:24
|
quote: Originally posted by RonanKeating thanks in advance that works now,I already have SQL books and I'm studying it but there are still difficulties for my perception for example with the posted code.Here is indented order and my interpretation for the posted code. As far as I understand when interpreting I should start from inner-most brackets/paranthesis.1. UPDATE e2. SET e.prop_type = a.row_n3. FROM (4. SELECT d.elm_type, d.elm_prop,5. ROW_NUMBER() OVER(ORDER BY d.elm_type ASC, d.elm_prop ASC) as row_n6. FROM (7. SELECT DISTINCT elm_type, elm_prop8. FROM [elements]9. WHERE prop_type IS NOT NULL10. ) d11. ) a12. LEFT OUTER JOIN [elements] e13. ON e.elm_type = a.elm_type,14. AND e.elm_prop = a.elm_propA) 6. to 10. lines defines inner-most distinct table and provide the resulting dataset to 3. line SQL statement as variable "d".B)3. line performs the selection based on the input from "d" result data set, and here it somehow implements the Row_Number custom function and returns the result as variable "row-n". Question comes in mind that can we call like that ORDER BY in 5.th line ?C.) and all the returned dataset is returned as input to LEFT OUTTER JOIN as "a" variable.I'm experiencing difficulties when trying to comrise the logic of that nested SQL statements. Could point out which is the best way?Please comment on my interpretations
What is your actual requirment. Please provide some sample data and expected output. |
 |
|
|
RonanKeating
Starting Member
3 Posts |
Posted - 2011-03-08 : 06:54:07
|
1ST TABLE 2ND TABLE Actually the 2nd table is nothing more than derivation of 1st table with following command :"select distinct elm_prof, elm_mat from elements"What I want to achieve is:Fill up the 1st table prop_type column with 2nd table matching rowsThat posted code is sufficient but I need to learn the logic. |
 |
|
|
|
|
|
|
|