Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SELECT FLOOR (Months / 12) "Year", MOD (Months, 12) "Months Between"
FROM (SELECT MONTHS_BETWEEN (SYSDATE, '25-apr-2014') Months FROM DUAL);
select ADD_MONTHS(trunc(to_date(SUBSTR(V_STR,1,4),'yyyy'),'YEAR')
QUARTER_START_DATE,(SUBSTR(V_STR,5,1)-1)*3) ,
ADD_MONTHS(trunc(to_date(SUBSTR(V_STR,1,4),'yyyy'),'YEAR'),SUBSTR(V_STR,5,1)*3)-1
QUARTER_END_DATE
Since there is no START WITH clause, every row in the dual table will be on
LEVEL = 1.
The CONNECT BY clause means what CONNECT BY always means: if there is a row on
LEVEL = N, then it will be connected to any row that meets the CONNECT BY
conditions.
So, what rows will be added on LEVEL=2? All rows where the condition 2 <= 10
is TRUE, so every row in the dual table will be on LEVEL = 2.
What rows will be added on LEVEL=3? All rows where the condition 3 <= 10 is
TRUE, so every row in the dual table will be on LEVEL = 3.
...
What rows will be added on LEVEL=10? All rows where the condition 10 <= 10 is
TRUE, so every row in the dual table will be on LEVEL = 10.
What rows will be added on LEVEL=11? All rows where the condition 11 <= 10 is
TRUE, so no row in the dual table will be on LEVEL = 11.
Since no rows are added at LEVEL=11, the CONNECT BY query stops at that point.
SELECT trns.trans_num
FROM transactions trns
WHERE trns.trans_date = TRUNC (SYSDATE - CASE WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'D'))
= 2 THEN 3 ELSE 1 END)
AND trns.trans_type = 'SALE'
Question - Display count of employees hired every year?
Answer - select to_char(orderdate,'yyyy'),count(*) from orders?
group by to_char(orderdate,'yyyy');
If you divide the number of days by 7 then you get the minimum number of sundays.
It could be 1 sunday more, if the remaining days include a sunday.
This logic can be transfered to some SQL statement.
with mytab as
(
select sysdate dt1, (sysdate-951) dt2 from dual
)
select
case when
round(round(last_day(dt2) - dt2) + round(dt1 - trunc(dt1,'month'))) >
to_char(last_day(dt2),'dd')
then
round(round(last_day(dt2) - dt2) + round(dt1 - trunc(dt1,'month'))) -
to_char(last_day(dt2),'dd')
else
round(round(last_day(dt2) - dt2) + round(dt1 - trunc(dt1,'month')))
end days,
mod(floor(months_between(dt1,dt2)),12) months,
floor(months_between(dt1,dt2)/12) years
from
mytab;