![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I was always told that the order of execution in T-SQL was FROM, SELECT, WHERE. This turns out to be important if you want to avoid certain errors, such as "divide by zero" error. But now look at the following table: ID Name Pos 1 Dom 1 1 Kim 2 1 Sam 3 In the following, it looks like the windowing function is done AFTER, not BEFORE, the where clause: select Name, max (Pos) over (partition by Group_ID) from T_Table where Last_Name < 'S' The value for max (Pos) is 2, not 3. Why did that happen? |
#3
| |||
| |||
|
|
I was always told that the order of execution in T-SQL was FROM, SELECT, WHERE. This turns out to be important if you want to avoid certain errors, such as "divide by zero" error. But now look at the following table: ID Name Pos 1 Dom 1 1 Kim 2 1 Sam 3 In the following, it looks like the windowing function is done AFTER, not BEFORE, the where clause: select Name, max (Pos) over (partition by Group_ID) from T_Table where Last_Name < 'S' The value for max (Pos) is 2, not 3. Why did that happen? |
#4
| |||
| |||
|
|
On 2010-09-17 18:10, Dom wrote: I was always told that the order of execution in T-SQL was FROM, SELECT, WHERE. *This turns out to be important if you want to avoid certain errors, such as "divide by zero" error. But now look at the following table: ID * * Name * * Pos 1 * * *Dom * * * 1 1 * * *Kim * * * * 2 1 * * *Sam * * * 3 In the following, it looks like the windowing function is done AFTER, not BEFORE, the where clause: select * * Name, max (Pos) over (partition by Group_ID) from * * * T_Table where * * Last_Name < 'S' The value for max (Pos) is 2, not 3. Why did that happen? I don't see anything controversial about that. What do you expect from a query like: select * * Id, max (Pos) from * * * T_Table where * * Last_Name < 'S' group by Id 1,3 or 1,2? If you want to investigate how it is defined (not exactly a walk in the park though :-), you can download draft documents of the standard from: http://www.wiscorp.com/SQLStandards.html For the 2003 variant the relevant document is named 5WD-02-Foundation-2003-09.pdf /Lennart- Hide quoted text - - Show quoted text - |
#5
| |||
| |||
|
|
Hugo. Yes, that was a type. Sorry. But you reproduced the results that I got. The problem is that I thought max (pos) would give me 3 instead of 2, since I thought the WHERE clause did not kick in until after the SELECT clause did it's work. |
|
I was always told that the order of execution in T-SQL was FROM, SELECT, WHERE. This turns out to be important if you want to avoid certain errors, such as "divide by zero" error. |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
SELECT f(c2) AS c1, f(c1) AS c2 FROM Foobar; That is why such nonsense is illegal syntax. OK, you made me sweat there for a split second because you violated your |
#8
| |||
| |||
|
|
SELECT f(c2) AS c1, f(c1) AS c2 FROM Foobar; That is why such nonsense is illegal syntax. OK, you made me sweat there for a split second because you violated your own golden rule by not providing DDL: CREATE TABLE foobar(c1 INTEGER); or CREATE TABLE foobar(c2 INTEGER); Of course CREATE TABLE foobar(c1 INTEGER, c2 INTEGER) will work just fine. -- Serge Rielau SQL Architect DB2 for LUW IBM Toronto Lab |
![]() |
| Thread Tools | |
| Display Modes | |
| |