Vous êtes sur la page 1sur 4

Query to find the #week from the date given

select to_char(sysdate, 'ww') from dual;


================================================================================
====================================
select sysdate, sysdate+1/24, sysdate +1/1440, sysdate + 1/86400 from dual;
select sysdate, sysdate-30 from dual;
--Last month
SELECT TRUNC(ADD_MONTHS(SYSDATE, -1),'MM') , LAST_DAY(ADD_MONTHS(SYSDATE,-1)) FR
OM dual
SELECT TRUNC(ADD_MONTHS(SYSDATE, -1),'MM') from dual
select ADD_MONTHS(SYSDATE, -1) from dual
select ADD_MONTHS(SYSDATE, -3) from dual
select trunc(ADD_MONTHS(SYSDATE, -1),'MM') from dual
--previous MONTH Data
SELECT AGENT_NAME, REGION, SALE_DATE, SUM(SALE_VALUE) total FROM TRS.SALES
WHERE
SALE_DAY >= TRUNC(ADD_MONTHS(SYSDATE, -1),'MM')
AND
SALE_DAY <= LAST_DAY(ADD_MONTHS(SYSDATE, -1))
GROUP BY ROLLUP( AGENT_NAME, AGENT_NAME);
--previous YEAR Data
* and provide a total count by client
SELECT REQUESTOR_ID, SYSTEM_ID, SUM(SESSION_COUNT) total FROM EBR.SESSION_SUMM
WHERE
/* first day of previous YEAR*/
SERVICE_DAY >= TRUNC(ADD_MONTHS(SYSDATE, -12),'SYYYY')
AND
/*last day of last YEAR*/
SERVICE_DAY <= LAST_DAY(ADD_MONTHS(TRUNC(ADD_MONTHS(SYSDATE, -12),'SYYYY'),
11))
/* group sums by Client IDs Requestor, then system */
GROUP BY ROLLUP( REQUESTOR_ID, SYSTEM_ID);
select TRUNC(ADD_MONTHS(SYSDATE, -12),'SYYYY') from dual;
select LAST_DAY(ADD_MONTHS(TRUNC(ADD_MONTHS(SYSDATE, -12),'SYYYY'), 11)) from du
al;
================================================================================
====================================
Oracle is actually very good for date manipulation. A date is basically held as
a fixed point number where 1 represents 1 day.
So for the date 7 days before a given date:
select to_date('01-jan-2008','dd-mm-yyyy')-7 from dual
So to look for records in the last 7 days (from the current time i.e. current ti

me-(7*24 hours)) you can do:


select * from table where date_column>=sysdate-7
for the last 7 days from midnight
select * from table where date_column>=trunc(sysdate)-7
Using the same technique you can select data from the last n hours or minutes i.
e. for data in last 10 minutes
select * from table where date_column>=sysdate-10/1440
(1440 is the number of minutes in a day)
================================================================================
====================================
================================================================================
====================================
In Which month maximum Employees joined in our company ?
================================================================================
====================================
SELECT *
FROM (SELECT TO_CHAR (hiredate, 'MON'), COUNT (empno)
FROM emp
GROUP BY TO_CHAR (hiredate, 'MON')
ORDER BY COUNT (empno) DESC)
WHERE ROWNUM = 1;
================================================================================
====================================
In Which year maximum Employees joined in our company ?
================================================================================
====================================
SELECT *
FROM (SELECT TO_CHAR (hiredate, 'YYYY'), COUNT (empno)
FROM emp
GROUP BY TO_CHAR (hiredate, 'YYYY')
ORDER BY COUNT (empno) DESC)
WHERE ROWNUM = 1;
================================================================================
====================================
In Which month-year maximum Employees joined in our company ?
================================================================================
====================================
SELECT TO_CHAR (hiredate, 'MON-YYYY'), COUNT (empno)
FROM emp
GROUP BY TO_CHAR (hiredate, 'MON-YYYY')
ORDER BY COUNT (empno) DESC;
================================================================================
====================================
================================================================================
====================================
First day
================================================================================
====================================
--First day of the current month

select trunc(sysdate,'MM') from dual;


--First day of the previous month
select trunc(trunc(sysdate,'MM')-1,'MM') from dual;
--First day of the current year
select trunc(sysdate,'YY') from dual
--First day of Previous year
select to_char(trunc(trunc(sysdate,'YY')-1,'YY'),'dd/mm/yyyy') from dual;
--or
select add_months(trunc(sysdate,'YY'),-12) from dual;
================================================================================
====================================
Last day
================================================================================
====================================
--Last day of the previous month
select trunc(sysdate,'MM')-1 from dual;
--or
select LAST_DAY(ADD_MONTHS(SYSDATE,-1)) FROM dual;
--Last day of the current month
select LAST_DAY(ADD_MONTHS(SYSDATE,0)) FROM dual;
--Last day of the current year
select add_months(trunc(sysdate,'YY'),12)-1 from dual;
--Last day of Previous year
select to_char(trunc(sysdate,'YY')-1,'dd/mm/yyyy') from dual;
================================================================================
====================================
--List out the employee details who joined in previous month
Select * from emp
Where hiredate between trunc(trunc(sysdate,'MM')-1,'MM') and trunc(sysdate,'MM')
-1;
----List out the employee details who joined in current month
Select * from emp
Where hiredate between trunc(sysdate,'MM') and LAST_DAY(ADD_MONTHS(SYSDATE,0));
--List out the employee details who joined in previous year
SELECT *
FROM emp
WHERE hiredate BETWEEN trunc(trunc(sysdate,'YY')-1,'YY')
AND (TRUNC (SYSDATE, 'YY') - 1);
----List out the employees details who joined in current year
Select * from emp
Where hiredate between trunc(sysdate,'YY') and add_months(trunc(sysdate,'YY'),12
)-1;

================================================================================
====================================
--First day of the month as per the column date value
select trunc(datecolumn,'MM') from dual
-select dense_rank() over (partition by to_char(HIREDATE,'MON') order by count(em
pno)desc) rnk,to_char(HIREDATE,'MON'),count(empno) from emp group by to_char(HI
REDATE,'MON');
select to_char(HIREDATE,'MON'),count(empno) from emp group by to_char(HIREDATE,'
MON') order by count(empno) desc

Vous aimerez peut-être aussi