SQL & PL/SQL :: Grouping Totals By Date Ranges
Oct 7, 2010
I have to get totals from a table using different criteria, which I do like this:
<QUERY>
SELECT DISTINCT
SUM(CASE WHEN MYCONDITION1 THEN 1 ELSE 0 END) AS TOTAL1,
SUM(CASE WHEN MYCONDITION2 THEN 1 ELSE 0 END) AS TOTAL2
FROM TABLE1, TABLE2
WHERE COMMON_CONDITION1 AND COMMON_CONDITION2
AND datevalue1 >= DATE1 AND datevalue1 <= DATE2;
<QUERY>
This works fine and I get the intended result.Now, I have to repeat this for every week for the last 12 months, excluding holidays period. So, I generate a set of date ranges which will be used in the queries. So, I repeat the above sql statement for all the date ranges, which is a lengthy process.How can I do that in a single shot and get all totals for each date range.
View 4 Replies
ADVERTISEMENT
Oct 21, 2010
I have this table,
Create table TBL_OK_HIST
(
DATE_KEY NUMBER,
A_N VARCHAR2(22 BYTE),
R_DUR VARCHAR2(8 BYTE),
CH_DUR VARCHAR2(8 BYTE),
REV VARCHAR2(20 BYTE)
)
insert into TBL_OK_HIST
values (20101010,123456768,5,20,2);
insert into TBL_OK_HIST
values (20101010,123496568,15,20,2);
insert into TBL_OK_HIST
values (20101012,122235768,25,25,3);
[Code] ......
Thus, applying the following would yeld:
Select * from TBL_OK_HIST
DATE_KEYA_N R_DUR CH_DUR REV
201010101234567685202
2010101012349656815202
2010101212223576825253
201010112345676819202
2010101234567681252527
2010101323456768136365
2010101056768123411202
2010101134681256717202
2010101068123456755559
generate the following results:
range_start_rdur range_end_rdur no_of_an sum_of_rdur sum_of_chdur sum_of_rev
1 5 1 5 20 2
6 10 1 9 20 2
11 15 2 26 40 4
16 20 1 17 20 2
21 25 1 25 25 3
26 30 0 0 0 0
31 35 0 0 0 0
36 40 1 36 36 5
41 45 0 0 0 0
46 50 0 0 0 0
51 55 2 107 107 16
I thought I would make use of the following query, but I am not getting the proper results when applying it to a real table with more than 20 mln records:
SELECT trunc(R_DUR/6)*5+1 as range_start_rdur,
trunc(R_DUR/6)*5+5 range_end_rdur,
sum(noofan) as no_of_an,
sum(sumofrdur) as sum_of_rdur,
sum(sumofchdur) as sum_of_chdur,
[Code] ...........
View 1 Replies
View Related
Jun 1, 2010
I'm trying to work out how to take a table like this:
IDDate
12502-Feb-07
12516-Mar-07
12523-May-07
12524-May-07
12525-May-07
33302-Jan-09
33303-Jan-09
33304-Jan-09
33317-Mar-09
And display the data like this:
IDPeriodPeriod StartPeriod End
125102-Feb-0702-Feb-07
125216-Mar-0716-Mar-07
125323-May-0725-May-07
333102-Jan-0904-Jan-09
333217-Mar-0917-Mar-09
As you can see, it's split the entries into date ranges. If there is a 'lone' date, the 'period start' and the 'period end' are the same date.
View 13 Replies
View Related
Apr 30, 2012
My version of the Database:
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
"CORE10.2.0.5.0Production"
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production
I have a staff table with the columns staff_id and completion_date. The completion_date shows the date a staff member completed questionnaire. If the staff member did not complete the questionaire, then the completion_date column will be NULL.
Table Definition:
-- Create table
create table staff
(
staff_id number not null,
completion_date date
);
See attached text file (staff.sql) for Insert Statements.
The result set needs to have the following columns: ReportDate: the Sunday of each week. Completed: The number of staff who have completed the questionnaire by the ReportDate. NotCompleted: The number of staff who did not complete the questionnaire by the ReportDate. Total: The SUM of Completed and NotCompleted columns.
As the number of Completed goes up, the number of NotCompleted goes down. Eventually Completed will equal Total and NotCompleted goes to zero.
The result set would look similar as follows and used to generate a bar graph chart:
ReportDateCompletedNotCompletedTotal
2012-Apr-01814651473
2012-Apr-082214511473
2012-Apr-1514413291473
2012-Apr-2242010531473
2012-Apr-295788951473
View 8 Replies
View Related
Apr 30, 2010
Can we insert multiple dates within give range in a single insert statement say from 1st Feb to 31st MAY 04,something like after insertion it should resemble like below
01-FEB-04
02-FEB-04
03-FEB-04
04-FEB-04
05-FEB-04
.
.
.
31-MAY-04
View 5 Replies
View Related
Aug 2, 2011
I have data such as 'hours', 'date' when and employee worked on the project. What I need is to select the total amount of hours per month of March, April, May, etc...
I know how to select data per single date but wonder how to do it per multiple dates. How does one select total amount of hours per multiple date ranges (March, April...)?
View 4 Replies
View Related
Sep 3, 2010
Lets say I have a table,
Claim_id,dateA,dateB
it has 5 million rows
I need to see if any dateA,dateB of a claim_id falls within any other dateA,dateB of another claim ID
Basically
select * from table a, table b, where
(a.dateA,a.dateB) overlaps (b.dateA,b.dateB)
now I can write the query simply enough by aliasing the table 2x but no matter how I try I cant see a way to get around doing a Carteasion join
Index are ignored because it has to scan the full table anyway even if I hint the index
and the cost of the join ends up astronomical aka 5million rows X 5 million Rows....
And it ends up doing the full table scan a few times
AHHHHHHHHHHHHHHHHHH
this table is expected to grow to at least 20 million records
View 17 Replies
View Related
Oct 30, 2011
I'm looking to see if there's a solution to my problem that I can use within the context of my business application interface into an Oracle RDMS. I have access to write custom SQL statements and functions, but I am NOT able to create stored procedures using the interface I have.
The challenge I am having is comparing date ranges. I have a table containing two columns labelled START TS TIME and END TS TIME, both of type 'Date'. I have figured out how to query each row against a given Next Session Start and Next Session End and determine if each row overlaps that row.
I need a procedure that will be recursive: that is, set Next Session Start and Next Session End to START TS TIME and END TS TIME of the first row, compare all rows against it, then set Next Session Start and Next Session End to the next row, compare all rows, ... for all rows in the table. I want to know what the maximum number of matches is (i.e. the most time periods that overlap).
If I could use a stored procedure I could complete this query easily. Is there other techniques (i.e. functions) available to leverage in order compare each row of date ranges against ALL rows in the same table?
View 4 Replies
View Related
Apr 10, 2012
I have TableA which has data like below.
ID NAME CRT_DTE
1 AB 03/05/1992
2 EF 15/04/1995
3 CD 20/08/1995
4 GH 01/01/1999
5 UV 08/07/2001
[code]....
I want a query which splits the total time period (from min crt_dte to max crt_dte) into year ranges.For eg, lets say a range of 5 years then I need to get results like below.
start_dte end_dte
---------- ----------
03/05/1992 03/05/1997
03/05/1997 03/05/2002
03/05/2002 03/05/2007
03/05/2007 09/03/2012
how to write this query.
View 6 Replies
View Related
Mar 28, 2013
I have this requirement to pull the ACTIVE days that a participant holds true in a given month for a specific position he/she holds.
The three date ranges here are: LOA dates(leave of absence), XFER dates(TRANSFER in/out of the position),Quality compliance(QUAL start/end dates).
So,
INACTIVE DAYS = [(LOA + XFER + WW) - OVERLAPPING DAYS OF (LOA+XFER+QUAL)]
Here is the scenario:
---------------------
CREATE TABLE PAYEES
(
RUN_PERIOD DATE NOT NULL,
PAYEEID VARCHAR2(20 BYTE) NOT NULL,
LOA_START_DATE DATE,
LOA_END_DATE DATE,
[Code].....
---------------------------------------------------------
---------------------------------------------------------
Now Active days is to be pulled for Two payees - TESTD01 and TESTD02 for the period of June month.
Desired Results :
Payee InActive_days Active_Days Period
---------------------------------------------------------------------------------------
TESTD01 13 17 June
TESTD02 14 16 June
I am able to pull out the no of days on a individual basis(count of days for Xfer/LOA/Qual) but I am really not sure as to how should i go about determining the overlapping days between 3 date ranges to determine the ACTIVE/INactive days.
View 11 Replies
View Related
Oct 14, 2011
I have a table like this
Name Hours date
a810/11/2011
a 510/12/2011
a610/13/2011
a710/14/2011
a710/15/2011
a810/16/2011
a710/17/2011
a810/18/2011
a810/19/2011
a710/20/2011
a710/21/2011
If i want the sum of hours for 3 days range ,how should i do it.
E.g. say
name hrs startdate enddate
a 19 10/11/2011 10/13/2011
a 22 10/14/201110/16/2011
a 23 10/17/2011 10/19/2011
View 11 Replies
View Related
Jun 3, 2010
I am studying Oracle® Database VLDB and Partitioning Guide 11g Release 2 (11.2),
ORA-14767: Cannot specify this interval with existing high boundsby giving an example other than given in the above mentioned document.
the example given in the document is as follows:
To increase the interval for date ranges, then you need to ensure that you are at a relevant boundary for the new interval. For example, if the highest interval partition boundary in your daily interval partitioned table transactions is January 30, 2007 and you want to change to a monthly partition interval, then the following statement results in an error:
CREATE TABLE transactions
( id NUMBER
, transaction_date DATE
, value NUMBER)
PARTITION BY RANGE (transaction_date)
[code]/...
View 3 Replies
View Related
Jun 14, 2012
Actually I have a table with the following data:
--------------------------------------------------
DATE ITEM NOFEE TYPEAMOUNT
--------------------------------------------------
1/1/20121234561 $0.50
1/1/20121234562 $0.40
1/1/20121234563 $0.30
[code]...
I would like to have a data set like this: grouping by ITEM NO & DATE
----------------------------------------------------------------------
DATE ITEM NO1 2 3 4
----------------------------------------------------------------------
1/1/2012123456$0.50 $0.40 $0.30 $0.20
2/1/20121234567$0.50 $0.40 $0.30 $0.20
3/1/201212345678$0.00 $0.40 $0.30 $0.20
if you see, from the third column in the result set, each fee type becomes different columns.
View 1 Replies
View Related
Sep 2, 2011
I have installed Oracle Database 11g.2 by database configuration assistant on windows XP as and adminstrator on my laptop(no connection to network),but when I want to create database I face this warning: error securing database control ,Datatbase control has been brought up in non-secure mode . to secure the database conntrol execute following command....(error is attached).
View 7 Replies
View Related
Mar 26, 2011
I am writing a query where I need to get the total points the student received on assignments, this is in the grade table. I then need to divide that total from the total amount of points possible, located in the assignment table. At the end all I should see is the student_id and the percentage of the students grade.
Here is where I am so far
SELECT s.student_id, SUM(g.points)/SUM(a.points_possible) "GRADE"
FROM student s, grade g, assignment a
WHERE s.student_id = g.student_id
AND g.assignment_id = a.assignment_id
ORDER BY student_id;
However when I execute this is the error I get:
WHERE s.student_id = g.student_id
*
ERROR at line 3:
ORA-00937: not a single-group group function The asterisk's should be under the g I couldn't get it to line up.
View 2 Replies
View Related
Nov 23, 2012
Version : 11g
I have a table with the following format and data
Serial no exp_Date exp_type exp_amount
1 01-nov-2012 Rent 10000
2 02-nov-2012 gas 250
3 02-nov-2012 insurance 9500
.
.
.
I want to create a sql output for a yearly view in the format
exp_type JAN FEB MAR APR ..... NOV DEC TOTAL AVERAGE PROJECTED
Rent 10000 10000 10000 10000 10000 10000 120000 10000 120000
gas 250 250 250 250 250 250 3000 250 3000
.
.
Now a couple of things in this
1. the average gives the average for the year, so lets say its start of 2013 and we are in feb, there will not be any values for the remaining months, so it should do the average for that exp_type for Jan and Feb based on the exp_amount entered against that type and show what is the expected average. Similary, projected will that average amount and mulitply it by 12 to show the exp amount expected based on current expenses
I was able to come up with the following sql to get the sum based on months, was not sure about average, total and projected
SELECT exp_type
, SUM (CASE WHEN to_char(exp_date,'Mon') = 'Jan' THEN exp_amt END) AS Jan
, SUM (CASE WHEN to_char(exp_date,'Mon') = 'Feb' THEN exp_amt END) AS feb
, SUM (CASE WHEN to_char(exp_date,'Mon') = 'Mar' THEN exp_amt END) AS Mar
, SUM (CASE WHEN to_char(exp_date,'Mon') = 'Apr' THEN exp_amt END) AS Apr
, SUM (CASE WHEN to_char(exp_date,'Mon') = 'May' THEN exp_amt END) AS May
[Code]....
getting the correct avg, total and projected fields also in the same sql?
View 8 Replies
View Related
Dec 20, 2012
I'm trying to create a report in the following format
Year Name Reg1 Reg2 Reg3
2001 Al 3 4 5
2001 Le 4 1 1
2001 7 5 6
2002 Sue 2 4 1
2002 Al 1 3 6
2002 Jim 6 1 3
2002 16 15 16
2003 Jim 4 -3 2
2003 Le -2 4 5
2003 20 16 23
Note that the totals are accumulating horizontally, broken on year.
View 3 Replies
View Related
Oct 25, 2010
I'v created a report using Cross Tab in Discoverer 11g,Now I want the difference between the Amortization and Deferral as the sub totals. If you look at the attachment,I need a column below deferral column which is difference of Amortization and Deferral.
View 1 Replies
View Related
Nov 24, 2010
I have three blocks in my form , header,activity and item.One header can have many activities and one activity can have many items.I need to keep track of item value totals in header from items under each activity.
View 1 Replies
View Related
May 5, 2010
I have been wracking my brain on this. Is there a way to write an SQL code that will combine split ranges within a table?
SAMPLE_TABLE
Common_FieldLow_ValueHigh_Value
1123123
11243000
130023005
130064000
135003501
130064500
I would like to combine any ranges that may exist. It is also possible that some row ranges may be nested in other rows.
END_RESULT
Common FieldLow ValueHigh Value
11233000
130024500
View 9 Replies
View Related
Feb 23, 2011
Activity date1 date2
R1 1/1/2011 31/1/2011
R1 2/1/2011 2/28/2011
R1 ... ...
I have a particular activity like R1 where I need to find the results for some periods as above. To be clear, if the activity is completed in between Jan 2011 to March 2011, then can I get data as like the above format?
View 20 Replies
View Related
Sep 23, 2010
I've the following table:
MEASURE_VALUE(ID, VALUE) containing measure values. I would like to calculate the average of a specific id interval. In my case the id is the position where the value was captures.
Example:
id[m] value[mm]
1 1.2
2 1.5
9 2
11 3
18 1
28 1.2
I would like to group ids in a specified range. For n = 3 the result should look like this:
from_id, to_id, avg
1 2 1.35
9 11 1.5
18 18 1
28 28 1.2
I have to find a way to group ids to chunks.
View 9 Replies
View Related
Sep 3, 2008
we can check condition like ranges using decode?
View 3 Replies
View Related
Aug 19, 2013
I have a requirement to get the records group wise.Ex: For each departments, i need to get the employee details as a coma seperated.It means that the output must have the department name in first column and the second column must contain all the employees in that particular department (As a coma seperated).
View 3 Replies
View Related
Mar 27, 2013
In the below data, a container is moving from one city to another. 1,2 ,3 can be any number which i want to generate and use as keys to group the cities. Eg: AUH, JEB, CIW belong to the same key=2; SIN, IKT belong to a new group 4. The City where difference between the Seq# is greater than 1 (eg between S8W and AUH), a new group starts.
Conotainer #CitySeqI want this
-------------------------------------------
Container1S8W5251
Container1S8W5261
Container1AUH5362
Container1AUH5372
Container1JEB5382
Container1JEB5392
[code]....
View 4 Replies
View Related
Feb 10, 2013
How can I get the grp_id for unique combination of manager and department, grp_id should be created on asc order of manager_id.
In this example manager_id 100 is minimum, so it should be grp 1 and all the employees with that manager_id should be in grp_id 1, for manager_id 114 grp_id should be 2.
If, there is manager_id 117, it should create grp_id 3.
To get grp_num ,I can use row_number() over (partition by department_id,manager_id order by employee_id) grp_num
I am looking for an update statement for this issue.
Oracle version : Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
CREATE TABLE HR.EMPLOYEES_2
(
EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20 BYTE),
LAST_NAME VARCHAR2(25 BYTE),
EMAIL VARCHAR2(25 BYTE),
PHONE_NUMBER VARCHAR2(20 BYTE),
[Code]....
Expected result
----------------
EMPLOYEE_ID SALARY MANAGER_ID DEPARTMENT_ID GRP_NUM GRP_ID
114 11000 100 30 1 1
115 3100 100 30 2 1
116 2900 114 30 1 2
117 2800 114 30 2 2
118 2600 114 30 3 2
119 2500 114 30 4 2
View 8 Replies
View Related
May 19, 2013
I am trying to break down the balance_date to display the following groupings:
7:00-17:30 CDT
18:00-4:30 CDT
I currently have the query setup to display by day instead of these time ranges. I would like the output to read
19 May Day
19 May Night
20 May Day
20 May Night
I am fairly new to this, but how would I go about making this change?
SELECT
TO_CHAR(TRUNC(balance_date,'D') + 4,'YYYY') || '-' ||
TO_CHAR(TRUNC(balance_date,'D') + 4,'IW') as year_wk,
TO_CHAR(TRUNC(balance_date,'D') + 4,'IW')as wk,
[Code] ........
View 1 Replies
View Related
May 31, 2012
Am getting an output like this in oracle9i,
S.No Column1 Column2 Column3 DateCol
1 A B C 10/2001
2 A B C 03/2001
3 B B C 02/2001
4 B B C 01/2001
5 A B C 03/2000
But My real scenario is , i need to populate the output in below structure
S.No Column1 Column2 Column3 DateCol
1 A B C 10/2001
A B C 03/2001
2 B B C 02/2001
B B C 01/2001
3 A B C 03/2000
I dont know how to form the query , to retrieve the structure..
View 2 Replies
View Related
Jun 6, 2013
SELECT
pas_code,
pas_profile,
count(sutp_id),
sum(sutp_price),
[code]...
And the problem is, that when i use sutp_price_proc and pbk_price in grouping, it splits my results by those rows. If i delete them from grouping, sql gives me error about not a single grouping in line 1.
pas_codepas_profilesutp_idsutp_pricex
2664good stuff310069< because pbk_price is like 67 from that period
2664good stuff310071< because pbk_price is like 50 from other period
how to get all results in a single line like:
pas_codepas_profilesutp_idsutp_pricex
2664good stuff6200140
View 3 Replies
View Related
Feb 24, 2010
TASK_ID, TASK_STATUS, TASK_OWNER
================================
00001 , OPEN , ABC
00002 , OPEN , XYZ
00003 , WIP , ABC
00004 , CLOSED , XYZ
00005 , WIP , XYZ
00006 , CLOSED , XYZ
00007 , OPEN , XYZ
Output Required
Owner , Open , WIP, Closed
ABC 1 1 0
XYZ 2 1 2
View 9 Replies
View Related