[b]select[/b]
*
[b]from[/b] (
[b]select[/b]
*,
[gray]-- difference in days between the birth day's and current day's ordinal number[/gray]
[gray]-- the differences are made continuous, no negative difference[/gray]
(to_char(birth,[i]'ddd'[/i])::int-to_char(now(),[i]'ddd'[/i])::int+div)%div [b]as[/b] dif
[b]from[/b] employee
,(
[b]select[/b]
[gray]-- ordinal number of the year's last day = number of days in the year[/gray]
to_char((to_char(now(),[i]'yyyy'[/i])||[i]'-12-31'[/i])::date,[i]'ddd'[/i])::int [b]as[/b] div[/gray]
) foo
) bar
[gray]-- where the difference is greater than or equal to 0 and less than or equal 6[/gray]
[b]where[/b] dif [b]between[/b] 0 [b]and[/b] 6
[gray]-- order by the same difference number[/gray]
[b]order by[/b] dif
[gray]/*
to_char(now(),[i]'ddd'[/i]) [i]= ordinal number of current day ( ie, 2007-02-19 is the year's 50[sup]th[/sup] day )[/i]
to_char(now(),[i]'ddd'[/i])::int [i]= the character result of the function casted to numeric value for arithmetic operations[/i]
to_char(now(),[i]'yyyy'[/i]) [i]= the year ( ie, 2007-02-19 is in year 2007 )[/i]
to_char(now(),[i]'yyyy'[/i])||[i]'-12-31'[/i])::date [i]= current year's last day ( ie, 2007's last day is 2007-12-31 )[/i]
to_char((to_char(now(),[i]'yyyy'[/i])||[i]'-12-31'[/i])::date,[i]'ddd'[/i]) [i]= ordinal number of the last day of the year( ie, 2007's last day is the year's 365[sup]th[/sup] day )[/i]
*/[/gray]