I want to distribute values of my summed quantities over multiple date ranges, which overlap. So,
the end result of this query is simply 2 records with overlapping date ranges. But, the current query just duplicates the values (quantities) in each. But, I want to place the data in the first record only as it's the first record within the same date range as the 2nd record, so the second record quantity (AllocQty) I want to be 0. right now it's coming up with equal quantities for both records, and they are in the same date range. I am modifying existing code, so I'm wondering if theres a way to just modify the code to somehow subtract the sum from the first group, so it won't be placed in the 2nd group. Is there a way to do that without rewriting this substantially?
from c in com
join b in all on c.SKU equals b.SKU into ps
from b in ps.DefaultIfEmpty()
.Where(x => x == null || (c.StartDate <= x.FinishDate && c.EndDate >= x.FinishDate)).DefaultIfEmpty()
select new
{
ComId = c.ComId,
metadatafromtablec,
etc...
...
AllocQty = b != null ? b.VDC_Alloc : 0, //Here I want this value to be zero in the 2nd overlapping group (startdate)
} into x
//It groups by every single field except for the calculated sum of the VAlloc //and KAlloc quantities
group x by new { x.ComId, x.ComType, x.CustType, x.CustrId, x.SKU, x.StartDate, x.EndDate} into grp
select new CommitmentView
{
CommId = grp.Key.ComId,
...
...
AllocQty = grp.Sum(r => r.AllocQty),
TotalAllocQty = grp.Sum(r => r.AllocQty) + grp.Sum(r => r.KAllocQty),
} into cv
orderby cv.SKU ascending
select cv
table c (com):
COMID
COM_TYPE
CUSTTYPE
CUSTRID
SKU
START_DATE
END_DATE
VDC_CQ
KDC_CQ
COMSTATUS
CREATED_BY
CREATED_DATE
UPDATED_BY
UPDATED_DATE
108
RETAIL
BCL
0
111872
2/1/2021
4/12/2021
2400
1560
APPROVED
humak
2/9/2021 3:26:18 PM
chrj
2/23/2021 11:43:41 AM
107
RETAIL
BCL
0
111872
2/7/2021
4/13/2021
288
84
DRAFT
chrj
2/8/2021 3:28:24 PM
chrj
2/24/2021 6:27:51 PM
table b (all):
EVENT_ID
LINE_ID
SKU
CUSTRID
ALLOCQTY
SUBMISSION_STATUS
ERROR_CODE
ERROR_DESCRIPTION
CREATED_BY
CREATED_DATE
UPDATED_BY
UPDATED_DATE
FinishDate
UOM
CUSTOMER_TYPE
100150
5344
111872
3
12
SCHEDULED
chrj
2/23/2021 6:11:04 PM
2/23/2021 6:11:04 PM
4/13/2021
C
BCL
100148
5342
111872
3
12
SCHEDULED
chrj
2/8/2021 3:23:27 PM
2/8/2021 3:23:27 PM
2/9/2021
C
BCL
100149
5343
111872
3
12
SCHEDULED
chrj
2/9/2021 1:58:30 PM
2/9/2021 1:58:30 PM
2/9/2021
C
BCL
100139
4952
111872
160
12
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4954
111872
129
24
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4956
111872
228
60
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4958
111872
218
12
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4960
111872
167
36
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4961
111872
158
120
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4964
111872
76
36
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4966
111872
163
24
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
100139
4969
111872
174
12
SCHEDULED
chrj
2/5/2021 4:38:46 PM
2/5/2021 4:38:46 PM
2/11/2021
C
BCL
Current Output:
COMID
COM_TYPE
CUSTTYPE
CUSTRID
SKU
START_DATE
END_DATE
VDC_CQ
KDC_CQ
COMSTATUS
TOTALALLOCQTY
CREATED_BY
CREATED_DATE
UPDATED_BY
UPDATED_DATE
107
RETAIL
BCL
0
111872
2/7/2021
4/13/2021
288
84
DRAFT
372
chrj
2/8/2021 3:28:24 PM
chrj
2/24/2021 6:27:51 PM
108
RETAIL
BCL
0
111872
2/1/2021
4/12/2021
2400
1560
APPROVED
360
humak
2/9/2021 3:26:18 PM
chrj
2/23/2021 11:43:41 AM
You can see the quantity of 360 is 'duplicated' in both records. There was a change desired by management to allow these records to overlap by date (You can see start date to end date
overlaps from 2/7/2021 to 4/12/2021. So, now the 360 should be in the record with comid of 108, and the remaining quantity of 12 should be in record with comid of 107 like this:
Desired Output:
COMID
COM_TYPE
CUSTTYPE
CUSTRID
SKU
START_DATE
END_DATE
VDC_CQ
KDC_CQ
COMSTATUS
TOTALALLOCQTY
CREATED_BY
CREATED_DATE
UPDATED_BY
UPDATED_DATE
107
RETAIL
BCL
0
111872
2/7/2021
4/13/2021
288
84
DRAFT
12
chrj
2/8/2021 3:28:24 PM
chrj
2/24/2021 6:27:51 PM
108
RETAIL
BCL
0
111872
2/1/2021
4/12/2021
2400
1560
APPROVED
360
humak
2/9/2021 3:26:18 PM
chrj
2/23/2021 11:43:41 AM
It should distribute the quantity between the records that overlap and the FinishDate that's within the range of the start date and end date. You can see there is one record
from table b that is dated 4/13/2021, so that's the only record that should be in the output of comid == 107.
Related
I try to group a given table by date to get a min and max date of member IDs. The result should display a time range from when to when a member was part of an OE.
my given table (excerpt):
ID DATE OE
11 2021-03-06 00:00:00 2926
11 2021-03-07 00:00:00 3879
11 2021-03-08 00:00:00 3879
11 2021-03-09 00:00:00 3879
11 2021-03-10 00:00:00 2926
11 2021-03-11 00:00:00 2926
11 2021-03-12 00:00:00 2926
11 2021-03-13 00:00:00 2926
11 2021-03-14 00:00:00 2926
11 2021-03-15 00:00:00 2926
11 2021-03-16 00:00:00 1344
11 2021-03-17 00:00:00 1344
11 2021-03-18 00:00:00 1344
11 2021-03-19 00:00:00 1344
11 2021-03-20 00:00:00 1344
11 2021-03-21 00:00:00 1344
11 2021-03-22 00:00:00 2926
11 2021-03-23 00:00:00 2926
11 2021-03-24 00:00:00 2926
11 2021-03-25 00:00:00 2926
11 2021-03-26 00:00:00 2926
11 2021-03-27 00:00:00 2926
11 2021-03-28 00:00:00 2926
11 2021-03-29 00:00:00 2926
11 2021-03-30 00:00:00 2926
11 2021-03-31 00:00:00 2926
11 2021-04-01 00:00:00 1549
11 2021-04-02 00:00:00 1549
11 2021-04-03 00:00:00 1549
11 2021-04-04 00:00:00 2926
My Select:
select id, min(date) as mind, max(date) as maxd,OE
from <table>
group by id,oe
order by mind desc;
The output should be something like this:
ID | MIND | MAXD | OE
11 2021-04-04 00:00:00 2021-04-04 00:00:00 2926
11 2021-04-01 00:00:00 2021-04-03 00:00:00 1549
11 2021-03-22 00:00:00 2021-03-31 00:00:00 2926
11 2021-03-16 00:00:00 2021-03-21 00:00:00 1344
11 2021-03-10 00:00:00 2021-03-15 00:00:00 2926
11 2021-03-07 00:00:00 2021-03-09 00:00:00 3879
11 2021-03-06 00:00:00 2021-03-06 00:00:00 2926
But it is like this:
ID | MIND | MAXD | OE
11 2021-04-01 00:00:00 2021-04-03 00:00:00 1549
11 2021-03-16 00:00:00 2021-03-21 00:00:00 1344
11 2021-03-07 00:00:00 2021-03-09 00:00:00 3879
11 2021-03-06 00:00:00 2021-04-04 00:00:00 2926
The result should display a time range from when to when a member was part of an OE. Even if I add some other IDs (which I obviously have) it does not show the timeline of OE changes in correct order.
Any help highly appreciated!
TIA,
Michael
You can use a trick called tabibitosan to do this kind of grouping:
SELECT id,
MIN(dt) AS mind,
MAX(dt) AS maxd,
oe
FROM (SELECT id,
dt,
oe,
row_number() OVER (PARTITION BY ID ORDER BY dt) - row_number() OVER (PARTITION BY ID, oe ORDER BY dt) grp
FROM your_table)
GROUP BY id,
oe,
grp
ORDER BY mind DESC;
See this dbfiddle for results
This works by assigning row numbers across the whole set of data (in this case, it's across each id), and then finding the row numbers across the subsets of data (i.e. across each id and oe), and then subtracting one from the other to form a number you can group by. Consecutive rows get the same group number, but every time there's a non-consecutive row, the group number will change.
From Oracle 12, you can use MATCH_RECOGNIZE for row-by-row pattern matching:
SELECT *
FROM table_name
MATCH_RECOGNIZE(
PARTITION BY id
ORDER BY "DATE"
MEASURES
FIRST(oe) AS oe,
FIRST("DATE") AS mind,
LAST("DATE") AS maxd
PATTERN (same_oe+)
DEFINE
same_oe AS oe = FIRST(oe)
)
Which, for the sample data:
CREATE TABLE table_name (ID, "DATE", OE) AS
SELECT 11, DATE '2021-03-06', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-07', 3879 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-08', 3879 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-09', 3879 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-10', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-11', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-12', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-13', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-14', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-15', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-16', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-17', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-18', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-19', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-20', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-21', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-22', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-23', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-24', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-25', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-26', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-27', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-28', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-29', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-30', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-31', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-01', 1549 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-02', 1549 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-03', 1549 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-04', 2926 FROM DUAL;
Outputs:
ID
OE
MIND
MAXD
11
2926
2021-03-06 00:00:00
2021-03-06 00:00:00
11
3879
2021-03-07 00:00:00
2021-03-09 00:00:00
11
2926
2021-03-10 00:00:00
2021-03-15 00:00:00
11
1344
2021-03-16 00:00:00
2021-03-21 00:00:00
11
2926
2021-03-22 00:00:00
2021-03-31 00:00:00
11
1549
2021-04-01 00:00:00
2021-04-03 00:00:00
11
2926
2021-04-04 00:00:00
2021-04-04 00:00:00
fiddle
This is presentation table:
ID PRESENTATIONDAY PRESENTATIONSTART PRESENTATIONEND PRESENTATIONSTARTDATE PRESENTATIONENDDATE
622 Monday 12:00:00 02:00:00 01-05-2016 04-06-2016
623 Tuesday 12:00:00 02:00:00 01-05-2016 04-06-2016
624 Wednesday 08:00:00 10:00:00 01-05-2016 04-06-2016
625 Thursday 10:00:00 12:00:00 01-05-2016 04-06-2016
I would like to insert availabledate in schedule table. This is my current query :
insert into SCHEDULE (studentID,studentName,projectTitle,supervisorID,
supervisorName,examinerID,examinerName,exavailableID,
availableday,availablestart,availableend,
availabledate) //PROBLEM STARTS HERE
values (?,?,?,?,?,?,?,?,?,?,?,?));
The value availabledate are retrieved based on the exavailableID
. For example, if exavailableID = 2, the availableday = Monday, availablestart= 12pm, availableend = 2pm.
The dates will only be chosen only between PRESENTATIONSTARTDATE to PRESENTATIONENDDATE from presentation table.
In presentation table, it will match PRESENTATIONDAY, PRESENTATIONDATESTART and PRESENTATIONDATEEND with availableday, availablestart and availableend to get a list of all possible dates.
This is the query to get list of all possible dates based on particular days:
select
A.PRESENTATIONID,
A.PRESENTATIONDAY,
A.PRESENTATIONDATESTART+delta LIST_DATE
from
PRESENTATION A,
(
select level-1 as delta
from dual
connect by level-1 <= (
select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
)
)
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
and
a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
order by 1,2,3;
This query result is:
622 Monday 02-05-2016 12:00:00
...
622 Monday 30-05-2016 12:00:00
623 Tuesday 03-05-2016 12:00:00
...
623 Tuesday 31-05-2016 12:00:00
624 Wednesday 04-05-2016 12:00:00
...
624 Wednesday 01-06-2016 12:00:00
625 Thursday 05-05-2016 12:00:00
...
625 Thursday 02-06-2016 12:00:00
It will automatically assign dates from the SELECT query to be inserted in schedule table. However, each date can be used only 4 times. Once it reached 4 times, it will proceed to next date. For example, if Monday, '02-05-2016' to '09-05-2016'
How can I corporate these two queries (INSERT and SELECT) to have a result like this:
StudentName projectTitle SupervisorID ExaminerID availableday availablestart availableend availabledate
abc Hello 1024 1001 MONDAY 12.00pm 2.00pm 02-05-2016
def Hi 1024 1001 MONDAY 12.00pm 2.00pm 02-05-2016
ghi Hey 1002 1004 MONDAY 12.00pm 2.00pm 02-05-2016
xxx hhh 1020 1011 MONDAY 12.00pm 2.00pm 02-05-2016
jkl hhh 1027 1010 MONDAY 12.00pm 2.00pm 09-05-2016
try ttt 1001 1011 MONDAY 12.00pm 2.00pm 09-05-2016
654 bbb 1007 1012 MONDAY 12.00pm 2.00pm 09-05-2016
gyg 888 1027 1051 MONDAY 12.00pm 2.00pm 09-05-2016
yyi 333 1004 1022 TUESDAY 12.00pm 2.00pm 03-05-2016
fff 111 1027 1041 TUESDAY ..
ggg 222 1032 1007 TUESDAY .. .. .. ..
hhh 444 1007 1001 TUESDAY 12.00pm 2.00pm 03-05-2016
and so on :)
In short, I would like to use the list of dates from presentation table based on the day, start time and end time to insertion query where each date will only used 4 times. Thank you!
I am not sure this kind of syntax works with oracle (and have no good way to check), but changing the select part of insert like this may or may not work.
select
A.PRESENTATIONID,
A.PRESENTATIONDAY,
A.PRESENTATIONDATESTART+delta LIST_DATE
from
PRESENTATION A,
(
select level-1 as delta
from dual
connect by level-1 <= (
select max(PRESENTATIONDATEEND - PRESENTATIONDATESTART) from PRESENTATION
)
),
--MIGHT NEED ADDITIONAL LOGIC FOR THE EXAVAILABLEID COMPARISON
(SELECT count(S.*) as counter FROM SCHEDULE S WHERE S.EXAVAILABLEID=A.ID) C
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
and
a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
and
C.counter<4
order by 1,2,3;
EDIT: Changed the operator. Had >= before. Placed teh WHERE check at the right place. Deleted aliases.
EDIT2: changed the syntax to where the counter select statement is a part of the from clause.
This is my query to get all possible dates between two dates based on days.
select A.presentationID,
A.PRESENTATIONDAY,
TO_CHAR(A.PRESENTATIONDATESTART+delta,'DD-MM-YYYY','NLS_CALENDAR=GREGORIAN') LIST_DATE
from
PRESENTATION A,
(
select level-1 as delta
from dual
connect by level-1 <= (
select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
)
)
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
and
a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
order by 1,2,3;
The values are retrieved from presentation table which consist of presentationday, presentationdatestart and presentationdateend.
Result from this query is :
622 Monday 02-05-2016 12:00:00
622 Monday 09-05-2016 12:00:00
622 Monday 16-05-2016 12:00:00
622 Monday 23-05-2016 12:00:00
622 Monday 30-05-2016 12:00:00
623 Tuesday 03-05-2016 12:00:00
623 Tuesday 10-05-2016 12:00:00
623 Tuesday 17-05-2016 12:00:00
623 Tuesday 24-05-2016 12:00:00
623 Tuesday 31-05-2016 12:00:00
624 Wednesday 04-05-2016 12:00:00
624 Wednesday 11-05-2016 12:00:00
624 Wednesday 18-05-2016 12:00:00
624 Wednesday 25-05-2016 12:00:00
624 Wednesday 01-06-2016 12:00:00
625 Thursday 05-05-2016 12:00:00
625 Thursday 12-05-2016 12:00:00
625 Thursday 19-05-2016 12:00:00
625 Thursday 26-05-2016 12:00:00
625 Thursday 02-06-2016 12:00:00
How can I arrange these value into something like this:
622 Monday 02-05-2016
623 Tuesday 03-05-2016
624 Wednesday 04-05-2016
625 Thursday 05-05-2016
622 Monday 09-05-2016
623 Tuesday 10-05-2016
624 Wednesday 11-05-2016
625 Thursday 12-05-2016
622 Monday 16-05-2016
....
625 Thursday 02-06-2016
I think you're just after this:
select a.presentationid,
a.presentationday,
to_char (a.presentationdatestart + delta, 'DD-MM-YYYY', 'NLS_CALENDAR=GREGORIAN') list_date
from presentation a,
(select level - 1 as delta
from dual
connect by level - 1 <= (select max (presentationdateend - presentationdatestart)
from presentation))
where a.presentationdatestart + delta <= a.presentationdateend
and a.presentationday = to_char(a.presentationdatestart + delta, 'fmDay')
order by a.presentationdatestart + delta,
a.presentationid;
N.B. note how I've removed your trim() and replaced it with fm in the format mask.
P.S. You could rewrite your query to remove the join condition (and extra call to the presentation table) by doing it like so:
with presentation as (select 622 presentationid, 'Monday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('31/05/2016', 'dd/mm/yyyy') presentationdateend from dual union all
select 623 presentationid, 'Tuesday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('31/05/2016', 'dd/mm/yyyy') presentationdateend from dual union all
select 624 presentationid, 'Wednesday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('07/06/2016', 'dd/mm/yyyy') presentationdateend from dual union all
select 625 presentationid, 'Thursday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('07/06/2016', 'dd/mm/yyyy') presentationdateend from dual)
-- end of mimicking your presentation table with data in it. You wouldn't need this subquery as you have the table; see SQL below.
select presentationid,
presentationday,
to_char(next_day(presentationdatestart -1, presentationday) + 7*(level - 1), 'DD-MM-YYYY') list_date
from presentation
connect by prior presentationid = presentationid
and prior sys_guid() is not null
and next_day(presentationdatestart -1, presentationday) + 7*(level - 1) <= presentationdateend
order by next_day(presentationdatestart -1, presentationday) + 7*(level - 1),
presentationid;
PRESENTATIONID PRESENTATIONDAY LIST_DATE
-------------- --------------- ----------
622 Monday 02-05-2016
623 Tuesday 03-05-2016
624 Wednesday 04-05-2016
625 Thursday 05-05-2016
622 Monday 09-05-2016
623 Tuesday 10-05-2016
624 Wednesday 11-05-2016
625 Thursday 12-05-2016
622 Monday 16-05-2016
623 Tuesday 17-05-2016
624 Wednesday 18-05-2016
625 Thursday 19-05-2016
622 Monday 23-05-2016
623 Tuesday 24-05-2016
624 Wednesday 25-05-2016
625 Thursday 26-05-2016
622 Monday 30-05-2016
623 Tuesday 31-05-2016
624 Wednesday 01-06-2016
625 Thursday 02-06-2016
Use this:
TO_CHAR(A.PRESENTATIONDATESTART+delta,'DD-MM-YYYY','NLS_CALENDAR=GREGORIAN' ) LIST_DATE
instead of
A.PRESENTATIONDATESTART+delta LIST_DATE
this formats your date
updated answer for your new challenge:
select * from (
select A.presentationID,
A.PRESENTATIONDAY,
TO_CHAR(A.PRESENTATIONDATESTART+delta,'DD-MM-YYYY','NLS_CALENDAR=GREGORIAN') LIST_DATE,
row_number() over (partition by presentationID,PRESENTATIONDATESTART+delta
order by presentationID,PRESENTATIONDATESTART+delta) r
from
PRESENTATION A,
(
select level-1 as delta
from dual
connect by level-1 <= (
select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
)
)
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
and
a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
)
order by r
I have presentation table consists of:
ID DAY START END STARTDATE ENDDATE
622 Monday 12:00:00 02:00:00 01-05-2016 04-06-2016
623 Tuesday 12:00:00 02:00:00 01-05-2016 04-06-2016
624 Wednesday 08:00:00 10:00:00 01-05-2016 04-06-2016
625 Thursday 10:00:00 12:00:00 01-05-2016 04-06-2016
I would like to list down all possible dateS from STARTDATE to ENDDATE. I have tried a query which is working except that it will list ALL POSSIBLE dates for every IDs. I may just need from one ID because all rows are having the same dates basically.
Query:
select
A.PRESENTATIONID,
A.PRESENTATIONDATESTART+delta dt
from
PRESENTATION A,
(
select level-1 as delta
from dual
connect by level-1 <= (
select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
)
)
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
order by 1,2;
The result:
622 01-05-2016 12:00:00 //startdate
.....
622 03-06-2016 12:00:00
622 04-06-2016 12:00:00 //enddate
.....
625 04-06-2016 12:00:00
The result is very tedious!
I would like to list down ONLY possible dates where the day is stored in my presentation table. For example, in presentation table the days are MONDAY,TUESDAY,WEDNESDAY and THURSDAY. Possible outcome will be:
MONDAY
02-05-2016 12:00:00
09-05-2016 12:00:00
16-05-2016 12:00:00
23-05-2016 12:00:00
30-05-2016 12:00:00
TUESDAY
03-05-2016 12:00:00
10-05-2016 12:00:00
17-05-2016 12:00:00
24-05-2016 12:00:00
31-05-2016 12:00:00
.... until THURSDAY
On the way to explain, in this problem I would like to EXCLUDE dates for FRIDAY, SATURDAY and SUNDAY. Is it possible?
UPDATE
New query:
select
A.PRESENTATIONID,
A.PRESENTATIONDAY,
A.PRESENTATIONDATESTART+delta dt
from
PRESENTATION A,
(
select level-1 as delta
from dual
connect by level-1 <= (
select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
)
)
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
and
a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
order by 1,2,3;
Result:
622 Monday 02-05-2016 12:00:00
622 Monday 09-05-2016 12:00:00
622 Monday 16-05-2016 12:00:00
622 Monday 23-05-2016 12:00:00
622 Monday 30-05-2016 12:00:00
623 Tuesday 03-05-2016 12:00:00
623 Tuesday 10-05-2016 12:00:00
623 Tuesday 17-05-2016 12:00:00
623 Tuesday 24-05-2016 12:00:00
623 Tuesday 31-05-2016 12:00:00
624 Wednesday 04-05-2016 12:00:00
624 Wednesday 11-05-2016 12:00:00
624 Wednesday 18-05-2016 12:00:00
624 Wednesday 25-05-2016 12:00:00
624 Wednesday 01-06-2016 12:00:00
625 Thursday 05-05-2016 12:00:00
625 Thursday 12-05-2016 12:00:00
625 Thursday 19-05-2016 12:00:00
625 Thursday 26-05-2016 12:00:00
625 Thursday 02-06-2016 12:00:00
You may check the calendar to see if the days and dates are match :D Thank you again #dcieslak!
Add to your query:
and
a.day = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
It will remove days that are not equal to your DAY column.
The trim is important for 'Day' because we want to get rid of leading and trailing spaces.
I have a requirement to show 1 to 20 questions how many teams answered for 0-23 hours. The table structure
Team_id Question_id Updated
124 25092 02-SEP-14 11:30:12 AM
15 25076 02-SEP-14 02:31:15 PM
258 25061 02-SEP-14 03:02:33 PM
298 25196 02-SEP-14 03:32:49 PM
450 25203 02-SEP-14 07:01:05 PM
471 25351 02-SEP-14 07:47:31 PM
482 25350 02-SEP-14 08:01:03 PM
78 24924 02-SEP-14 08:01:29 PM
541 26032 02-SEP-14 09:35:00 PM
708 26485 02-SEP-14 12:19:48 PM
726 26125 02-SEP-14 01:00:11 PM
145 26221 02-SEP-14 03:45:32 PM
824 26436 02-SEP-14 06:30:16 PM
87 26505 02-SEP-14 06:30:22 PM
825 26488 02-SEP-14 06:31:34 PM
466 26488 02-SEP-14 06:34:48 PM
868 26091 02-SEP-14 08:30:59 PM
1356 28852 02-SEP-14 07:45:14 PM
1342 28852 02-SEP-14 07:45:27 PM
Query should return
Hours 1 2 3 4 5 to 20
10 6 0 2 3 6 to 25
11 3 2 1 1 7 to 300
12 2 0 5 0 7 to 30
13 1 6 1 1 7 to 40
these are count of team that answered only 1 question in hour 10
these are count of team that answered only 2 question in hour 10 etc.
Note it is not 1st or 2nd question.
Thanks
Arun
You need something like this I suppose:
select hour,
count(decode(cnt, 1, 1)) as q1,
count(decode(cnt, 2, 1)) as q2,
count(decode(cnt, 3, 1)) as q3,
count(decode(cnt, 4, 1)) as q4,
...
count(decode(cnt, 20, 1)) as q20
from (
select to_char(updated, 'HH24') as hour
, count(*) as cnt
from &table_name
group by to_char(updated, 'HH24'), team_id
)
group by hour;
As far as I understand, perhaps you want something like this :
SQL> WITH DATA AS(
2 SELECT 124 team_id, 25092 question_id, to_date('02-SEP-14 11:30:12 AM','DD-MON-RR HH:MI:SS AM') UPDATED FROM DUAL UNION ALL
3 SELECT 15 , 25076 ,to_date('02-SEP-14 02:31:15 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
4 SELECT 258 , 25061 ,to_date('02-SEP-14 03:02:33 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
5 SELECT 298 , 25196 ,to_date('02-SEP-14 03:32:49 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
6 SELECT 450 , 25203 ,to_date('02-SEP-14 07:01:05 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
7 SELECT 471 , 25351 ,to_date('02-SEP-14 07:47:31 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
8 SELECT 482 , 25350 ,to_date('02-SEP-14 08:01:03 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
9 SELECT 78 , 24924 ,to_date('02-SEP-14 08:01:29 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
10 SELECT 541 , 26032 ,to_date('02-SEP-14 09:35:00 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
11 SELECT 708 , 26485 ,to_date('02-SEP-14 12:19:48 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
12 SELECT 726 , 26125 ,to_date('02-SEP-14 01:00:11 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
13 SELECT 145 , 26221 ,to_date('02-SEP-14 03:45:32 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
14 SELECT 824 , 26436 ,to_date('02-SEP-14 06:30:16 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
15 SELECT 87 , 26505 ,to_date('02-SEP-14 06:30:22 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
16 SELECT 825 , 26488 ,to_date('02-SEP-14 06:31:34 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
17 SELECT 466 , 26488 ,to_date('02-SEP-14 06:34:48 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
18 SELECT 868 , 26091 ,to_date('02-SEP-14 08:30:59 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
19 SELECT 1356 , 28852 ,to_date('02-SEP-14 07:45:14 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL UNION ALL
20 SELECT 1342 , 28852 ,to_date('02-SEP-14 07:45:27 PM','DD-MON-RR HH:MI:SS AM') FROM DUAL)
21 SELECT 5 hours,
22 COUNT(team_id)
23 FROM DATA
24 WHERE UPDATED BETWEEN to_date('02-SEP-14 10:00:01','DD-MON-RR HH24:MI:SS') AND to_date('02-SEP-14 14:59:59','DD-MON-RR HH24:MI:SS')
25 UNION
26 SELECT 6 hours,
27 COUNT(team_id)
28 FROM DATA
29 WHERE UPDATED BETWEEN to_date('02-SEP-14 10:00:01','DD-MON-RR HH24:MI:SS') AND to_date('02-SEP-14 15:59:59','DD-MON-RR HH24:MI:SS')
30 UNION
31 SELECT 10 hours,
32 COUNT(team_id)
33 FROM DATA
34 WHERE UPDATED BETWEEN to_date('02-SEP-14 10:00:01','DD-MON-RR HH24:MI:SS') AND to_date('02-SEP-14 19:59:59','DD-MON-RR HH24:MI:SS')
35 UNION
36 SELECT 24 hours,
37 COUNT(team_id)
38 FROM DATA
39 WHERE UPDATED BETWEEN to_date('02-SEP-14 00:00:01','DD-MON-RR HH24:MI:SS') AND to_date('02-SEP-14 23:59:59','DD-MON-RR HH24:MI:SS')
40 /
HOURS COUNT(TEAM_ID)
---------- --------------
5 4
6 7
10 15
24 19
The time interval as an example, I took varying intervals, and the HOURS column is the time interval mentioned in the WHERE clause.