Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 Oracle PL/SQL: Avoid overlapping months and years Jump to:  
Category: >> Oracle PL/SQL >> Avoid overlapping months and years Bookmark and Share

<< lastnext >>

Snippet Name: Avoid overlapping months and years

Description: Useful way to group data by week to avoid the quandary of weeks overlapping months and years.

Also see:
» FUNCTIONS: date/time
» UPDATE: Update a partitioned table
» UPDATE: Update based on a record
» UPDATE: Update Object Table
» UPDATE: with RETURNING clause
» UPDATE: Nested Table Update example
» UPDATE: Correlated Update
» UPDATE: Update from a SELECT statement
» UPDATE: based on multiple returned val...
» UPDATE: Update based on a query
» UPDATE: Update multiple rows
» UPDATE: update a specific record
» UPDATE: Single row
» Date Functions: EXTRACT
» Date Functions: TO_DSINTERVAL
» Date Functions: TO_NCHAR
» Date Functions: TO_YMINTERVAL
» Date Functions: Calculate elapsed time...
» Date/Time Calculations: Get seconds be...
» Date: Find first day of the month
» Date Calculations
» Date Functions: WHERE Clause Joins
» Date Functions: TRUNC
» Date Functions: SYSDATE
» Date Functions: TO_CHAR
» Date Functions: ROUND
» Date Functions: NEXT_DAY
» Date Functions: NEW_TIME
» Date Functions: MONTHS_BETWEEN
» Date Functions: MIN

Comment: (none)

Language: PL/SQL
Highlight Mode: PLSQL
Last Modified: March 12th, 2009

CURSOR x_cur IS 
SELECT  DISTINCT SUBSTR(TO_CHAR(date1),4,3) m, SUBSTR(TO_CHAR(date1),1,2) w, COUNT (*) cnt
FROM cpad_errors
GROUP BY SUBSTR(TO_CHAR(date1),4,3), SUBSTR(TO_CHAR(date1),1,2);
x_rec x_cur%ROWTYPE;
week_var NUMBER;
     EXECUTE IMMEDIATE 'truncate table week_test';
     OPEN x_cur;
     FETCH x_cur INTO x_rec;
     EXIT WHEN x_cur%notfound;
     IF TO_NUMBER(x_rec.w) < 8 
     THEN week_var := 1;
     ELSIF TO_NUMBER(x_rec.w) < 15 AND TO_NUMBER(x_rec.w) > 7 
     THEN week_var := 2;
     ELSIF TO_NUMBER(x_rec.w) < 22 AND TO_NUMBER(x_rec.w) > 16
     THEN week_var := 3;
     ELSE week_var := 4;
     END IF;
     INSERT INTO week_test (WEEK_NUM, TTL, MNTH)
     VALUES (week_var, x_rec.cnt, x_rec.m);
     END LOOP;
     CLOSE x_cur;
END weekly_proc;
SELECT mnth|| ' week '|| week_num, SUM(ttl)
FROM week_test
GROUP BY mnth|| ' week '|| week_num;

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 29 users online    © 2009 psoug.org

Forgot your password?