473,437 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,437 developers and data experts.

Important Date Related Queries - 2

debasisdas
8,127 Expert 4TB
12.Alternate method for determining Quarter Start and End Dates for Quarter(not recommended)
----------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select add_months(q_end,-2) q_start,
  2. last_day(q_end) q_end
  3. from (select to_date(substr(yrq,1,4)||mod(yrq,10)*3,'yyyymm') q_end
  4. from (
  5. select 20071 yrq from dual union all
  6. select 20072 yrq from dual union all
  7. select 20073 yrq from dual union all
  8. select 20074 yrq from dual
  9. ) x
  10. ) y
13.Filling in Missing Dates
----------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. with x
  2. as (
  3. select add_months(start_date,level-1) start_date
  4. from (
  5. select min(trunc(hiredate,'y')) start_date,
  6. add_months(max(trunc(hiredate,'y')),12) end_date from emp) connect by level <= months_between(end_date,start_date))     
  7. select x.start_date MTH, count(e.hiredate) num_hired from x, emp e where x.start_date = trunc(e.hiredate(+),'mm')
  8. group by x.start_date
  9. order by 1
14.Alternate Method.
-----------------------------------
Expand|Select|Wrap|Line Numbers
  1. with x
  2. as (
  3. select add_months(start_date,level-1) start_date
  4. from (
  5. select min(trunc(hiredate,'y')) start_date,
  6. add_months(max(trunc(hiredate,'y')),12) end_date
  7. from emp)
  8. connect by level <= months_between(end_date,start_date)
  9. )
  10. select x.start_date MTH, count(e.hiredate) num_hired
  11. from x left join emp e
  12. on (x.start_date = trunc(e.hiredate,'mm'))
  13. group by x.start_date
  14. order by 1
15.Find all employees hired in February or December, as well as employees hired on a Tuesday.
-----------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select ename from emp
  2. where rtrim(to_char(hiredate,'month')) in ('february','december')
  3. or rtrim(to_char(hiredate,'day')) = 'tuesday'
16.Comparing Records Using Specific Parts of a Date
----------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select a.ename ||
  2.        ' was hired on the same month and weekday as '||
  3.        b.ename as msg
  4.   from emp a, emp b
  5. where to_char(a.hiredate,'DMON') =
  6.       to_char(b.hiredate,'DMON')
  7.   and a.empno < b.empno
  8. order by a.ename
17.Finding Differences Between Rows in the Same Group or Partition
---------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select deptno, ename, sal, hiredate,
  2. lpad(nvl(to_char(sal-next_sal), 'N/A'), 10) diff from ( select deptno, ename, sal, hiredate,
  3. lead(sal)over(partition by deptno
  4. order by hiredate) next_sal from emp)
18.Locating a Range of Consecutive Values
------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select proj_id, proj_start, proj_end
  2. from (
  3. select proj_id, proj_start, proj_end,
  4. lead(proj_start)over(order by proj_id) next_proj_start from job)
  5. where next_proj_start = proj_end
19.Locating the Beginning and End of a Range of Consecutive Values
===================================
Expand|Select|Wrap|Line Numbers
  1. select proj_grp, min(proj_start), max(proj_end)
  2.   from (
  3. select proj_id,proj_start,proj_end,
  4. sum(flag)over(order by proj_id) proj_grp
  5. from ( select proj_id,proj_start,proj_end,
  6. case when
  7. lag(proj_end)over(order by proj_id) =proj_start
  8. then 0 else 1 end flag from job
  9. )) group by proj_grp
20.Filling in Missing Values in a Range of Values
-----------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. select x.yr, coalesce(cnt,0) cnt
  2.         from (
  3.       select extract(year from min(hiredate)over( )) -
  4.              mod(extract(year from min(hiredate)over( )),10) +
  5.              rownum-1 yr
  6.         from emp
  7.        where rownum <= 10
  8.              ) x,
  9.              (
  10.      select to_number(to_char(hiredate,'YYYY')) yr, count(*) cnt
  11.        from emp
  12.       group by to_number(to_char(hiredate,'YYYY'))
  13.             ) y
  14.       where x.yr = y.yr(+)
21.Generating Consecutive Numeric Values
------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. with x
  2. as (
  3. select level id
  4. from dual
  5. connect by level <= 10
  6. )
  7. select * from x
22.PRINTING CALANDER
-----------------------------------------
Expand|Select|Wrap|Line Numbers
  1. with x
  2.       as (
  3.    select *
  4.      from (
  5.    select to_char(trunc(sysdate,'mm')+level-1,'iw') wk,
  6.           to_char(trunc(sysdate,'mm')+level-1,'dd') dm,
  7.           to_number(to_char(trunc(sysdate,'mm')+level-1,'d')) dw,
  8.           to_char(trunc(sysdate,'mm')+level-1,'mm') curr_mth,
  9.           to_char(sysdate,'mm') mth
  10.     from dual
  11.    connect by level <= 31
  12.          )
  13.    where curr_mth = mth
  14.   )
  15.   select max(case dw when 2 then dm end) Mo,
  16.          max(case dw when 3 then dm end) Tu,
  17.          max(case dw when 4 then dm end) We,
  18.          max(case dw when 5 then dm end) Th,
  19.          max(case dw when 6 then dm end) Fr,
  20.          max(case dw when 7 then dm end) Sa,
  21.          max(case dw when 1 then dm end) Su
  22.     from x
  23.    group by wk
  24.    order by wk
Sep 6 '07 #1
0 6938

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
6
by: BlackFireNova | last post by:
Using Access 2002 I am writing a report which draws data from several different tables. I can't link all the tables in a query, as some can not be related without truncating the data. I plan...
5
by: David B | last post by:
I have a number of queries, running one after the other, which do quite a complex calculation. A text box on a form provides the date for this routine. I have another routine I wish to do and it...
2
by: carl.barrett | last post by:
Hi, I'm back with the same question as I still can't get it to display my data the way I want it to. The table lists information about a perpetrator involved with an anti social behaviour...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
0
debasisdas
by: debasisdas | last post by:
This article consistes of some of the frequently asked date related queries. Hope the users find it useful. ========================== 1.Determining the Date of the First and Last Occurrence of a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.