I have the following data in a table i.e. No, Date and Step
+---------+----------------------------+----------------------------+
| No | Date | Step |
+---------+----------------------------+----------------------------+
| 643995G | 03/12/2012 3:22:48 PM | TransferInStart |
| 643995G | 03/12/2012 3:22:50 PM | |
| 643995G | 15/02/2013 10:53:57 AM | Hold |
| 643995G | 15/02/2013 10:54:00 AM | Hold Copy Processing Start |
| 643995G | 20/02/2013 4:38:26 PM | |
| 643995G | 21/02/2013 3:27:01 PM | ExceptionStart |
| 643995G | 22/02/2013 9:38:32 AM | ExceptionEnd |
| 643995G | 22/02/2013 9:39:32 AM | |
| 643995G | 22/02/2013 10:04:53 AM | |
| 643995G | 22/02/2013 10:04:56 AM | |
| 643995G | 25/02/2013 10:48:18 AM | TransferIn |
| 643995G | 25/02/2013 10:48:19 AM | CatalougingStart |
| 643995G | 27/02/2013 1:59:44 PM | |
| 643995G | 27/02/2013 1:59:45 PM | |
| 643995G | 27/02/2013 2:05:01 PM | Catalouging |
| 643995G | 27/02/2013 2:05:02 PM | ProcessingStart |
| 643995G | 27/02/2013 6:09:51 PM | |
| 643995G | 27/02/2013 6:09:52 PM | |
| 643995G | 04/03/2013 2:03:16 PM | |
| 643995G | 04/03/2013 2:03:17 PM | Hold Copy Processing Start |
| 643995G | 11/03/2013 2:27:14 PM | |
| 643995G | 12/03/2013 10:09:55 AM | ExceptionStart |
| 643995G | 12/03/2013 10:10:27 AM | ExceptionEnd |
| 643995G | 12/03/2013 10:10:52 AM | |
| 643995G | 12/03/2013 10:11:27 AM | |
| 643995G | 12/03/2013 10:28:37 AM | |
| 643995G | 12/03/2013 10:46:21 AM | Processing |
+---------+----------------------------+----------------------------+
Would like to fill in the empty records with the previous step.
After filling the above value where ever I found the Exception Start and Exception End would like to fill with the above value.
Please refer blow step2 and Step3 as examples output.
+---------+------------------------+----------------------------+----------------------------+----------------------------+
| No | Date | ProcessStep | Step2 | Step3 |
+---------+------------------------+----------------------------+----------------------------+----------------------------+
| 643995G | 03/12/2012 3:22:48 PM | TransferInStart | TransferInStart | TransferInStart |
| 643995G | 03/12/2012 3:22:50 PM | | TransferInStart | TransferInStart |
| 643995G | 15/02/2013 10:53:57 AM | Hold | Hold | Hold |
| 643995G | 15/02/2013 10:54:00 AM | Hold Copy Processing Start | Hold Copy Processing Start | Hold Copy Processing Start |
| 643995G | 20/02/2013 4:38:26 PM | | Hold Copy Processing Start | Hold Copy Processing Start |
| 643995G | 21/02/2013 3:27:01 PM | ExceptionStart | ExceptionStart | Hold Copy Processing Start |
| 643995G | 22/02/2013 9:38:32 AM | ExceptionEnd | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 22/02/2013 9:39:32 AM | | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 22/02/2013 10:04:53 AM | | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 22/02/2013 10:04:56 AM | | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 25/02/2013 10:48:18 AM | TransferIn | TransferIn | TransferIn |
| 643995G | 25/02/2013 10:48:19 AM | CatalougingStart | CatalougingStart | CatalougingStart |
| 643995G | 27/02/2013 1:59:44 PM | | CatalougingStart | CatalougingStart |
| 643995G | 27/02/2013 1:59:45 PM | | CatalougingStart | CatalougingStart |
| 643995G | 27/02/2013 2:05:01 PM | Catalouging | Catalouging | Catalouging |
| 643995G | 27/02/2013 2:05:02 PM | ProcessingStart | ProcessingStart | ProcessingStart |
| 643995G | 27/02/2013 6:09:51 PM | | ProcessingStart | ProcessingStart |
| 643995G | 27/02/2013 6:09:52 PM | | ProcessingStart | ProcessingStart |
| 643995G | 04/03/2013 2:03:16 PM | | ProcessingStart | ProcessingStart |
| 643995G | 04/03/2013 2:03:17 PM | Hold Copy Processing Start | Hold Copy Processing Start | Hold Copy Processing Start |
| 643995G | 11/03/2013 2:27:14 PM | | Hold Copy Processing Start | Hold Copy Processing Start |
| 643995G | 12/03/2013 10:09:55 AM | ExceptionStart | ExceptionStart | Hold Copy Processing Start |
| 643995G | 12/03/2013 10:10:27 AM | ExceptionEnd | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 12/03/2013 10:10:52 AM | | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 12/03/2013 10:11:27 AM | | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 12/03/2013 10:28:37 AM | | ExceptionEnd | Hold Copy Processing Start |
| 643995G | 12/03/2013 10:46:21 AM | Processing | Processing | Processing |
+---------+------------------------+----------------------------+----------------------------+----------------------------+
Input
Output
Different databases have different ways of solving this problem. One method uses a correlated subquery in a select. A typical method can be expressed in ANSI standard SQL as:
update t
set step2 = (select t2.processstep
from t t2
where t2.no = t.no and
t2.date <= t.date and
t2.processstep is not null
order by date desc
fetch first 1 row only
);
In some databases, you would use limit or top to get the first row. Even so, this might not work in all databases, but something similar is likely to work.
Related
my difficulty is to join two tables with gmv value.
table 1
| id | historical_date| status |
| -------- | -------------- | -------------- |
| 45615266 | 2021-06-02 | Pending |
| 45615266 | 2021-12-05 | Validated |
table 2
| id | grass_date | value |
| -------- | -------------- | -------------- |
| 45615266 | 2021-02-02 | 24.02 |
| 45615266 | 2021-03-17 | 15.48 |
| 45615266 | 2020-12-21 | 1993.85 |
| 45615266 | 2021-06-02 | 74.56 |
| 45615266 | 2021-07-14 | 74.48 |
| 45615266 | 2021-12-04 | 99.48 |
Expected
| id | historical_date | status | grass_date | value |
| 45615266 | 2021-06-02 | Pending | 2021-02-02 | 24.02 |
| 45615266 | 2021-06-02 | Pending | 2021-03-17 | 15.48 |
| 45615266 | 2021-06-02 | Pending | 2020-12-21 | 1993.85 |
| 45615266 | 2021-12-05 | Validated | 2021-06-02 | 74.56 |
| 45615266 | 2021-12-05 | Validated | 2021-07-14 | 74.48 |
| 45615266 | 2021-12-05 | Validated | 2021-12-04 | 99.48 |
I use Trino (prestoSQL) and the subquery is limited to one column
You can use a NATURAL JOIN:
SELECT * FROM table 1 NATURAL JOIN table 2
I'd expect this to work to get me a list of calendar dates over the past 12 months excluding weekends; but it just gives me the entire list of dates - which I suppose is fine - but want to know why the below is incorrect.
SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum AS CalendarDate
FROM all_objects
WHERE ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum <= sysdate
AND to_char(sysdate,'DY') NOT IN ('SAT','SUN')
Because you're doing this:
AND to_char(sysdate,'DY') NOT IN ('SAT','SUN')
And today isn't Saturday or Sunday. You need to look at the calculated CalendarDate value; but you can't do that in the same level of subquery. You could try to recalculate it:
AND to_char(ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum,'DY') NOT IN ('SAT','SUN')
but this will return no rows - at least when run at the moment. As it happens, March 1st 2020 was a Sunday, so that is excluded; and because of when and how rownum is generated, that result is excluded, and the next one sees the same value, which is excluded, and so on.
You can use an inline view to avoid both issues:
SELECT CalendarDate
FROM (
SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum AS CalendarDate
FROM all_objects
WHERE ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum <= sysdate
)
WHERE to_char(CalendarDate,'DY','NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('SAT','SUN')
CALENDARDATE
02-MAR-20
03-MAR-20
04-MAR-20
05-MAR-20
06-MAR-20
09-MAR-20
10-MAR-20
...
db<>fiddle
I've chucked in a language modifier to stop it behaving differently for users with sessions not set to English.
Querying against all_objects isn't ideal though, it would be better to use a hierarcical query:
SELECT *
FROM (
SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + level AS CalendarDate
FROM dual
CONNECT BY level <= TRUNC(SYSDATE) - ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) + 1
)
WHERE to_char(CalendarDate,'DY','NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('SAT','SUN')
ORDER BY CalendarDate
db<>fiddle
or a recursive CTE, if you're 11gR2+:
WITH rcte (CalendarDate) AS (
SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12)
FROM dual
UNION ALL
SELECT rcte.CalendarDate + interval '1' day
FROM rcte
WHERE rcte.CalendarDate < TRUNC(SYSDATE)
)
SELECT CalendarDate
FROM rcte
WHERE to_char(CalendarDate,'DY','NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('SAT','SUN')
ORDER BY CalendarDate
db<>fiddle (as 18c to avoid a couple of issues with the patch level in the 11g version it uses).
You checking whether today is sunday or monday with to_char(sysdate,'DY'). you need to check CalendarDate which is not available in your window. You can use cte to calculate the calendar then you can remove weekends with your condition as below.
with cte (CalendarDate) as
(
SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum AS CalendarDate
FROM all_objects
WHERE ADD_MONTHS(TRUNC(SYSDATE,'MM'),-12) - 1 + rownum <= sysdate
)
select * from cte where
to_char(CalendarDate,'DY') not in ('SAT','SUN');
| CALENDARDATE |
| :----------- |
| 02-MAR-20 |
| 03-MAR-20 |
| 04-MAR-20 |
| 05-MAR-20 |
| 06-MAR-20 |
| 09-MAR-20 |
| 10-MAR-20 |
| 11-MAR-20 |
| 12-MAR-20 |
| 13-MAR-20 |
| 16-MAR-20 |
| 17-MAR-20 |
| 18-MAR-20 |
| 19-MAR-20 |
| 20-MAR-20 |
| 23-MAR-20 |
| 24-MAR-20 |
| 25-MAR-20 |
| 26-MAR-20 |
| 27-MAR-20 |
| 30-MAR-20 |
| 31-MAR-20 |
| 01-APR-20 |
| 02-APR-20 |
| 03-APR-20 |
| 06-APR-20 |
| 07-APR-20 |
| 08-APR-20 |
| 09-APR-20 |
| 10-APR-20 |
| 13-APR-20 |
| 14-APR-20 |
| 15-APR-20 |
| 16-APR-20 |
| 17-APR-20 |
| 20-APR-20 |
| 21-APR-20 |
| 22-APR-20 |
| 23-APR-20 |
| 24-APR-20 |
| 27-APR-20 |
| 28-APR-20 |
| 29-APR-20 |
| 30-APR-20 |
| 01-MAY-20 |
| 04-MAY-20 |
| 05-MAY-20 |
| 06-MAY-20 |
| 07-MAY-20 |
| 08-MAY-20 |
| 11-MAY-20 |
| 12-MAY-20 |
| 13-MAY-20 |
| 14-MAY-20 |
| 15-MAY-20 |
| 18-MAY-20 |
| 19-MAY-20 |
| 20-MAY-20 |
| 21-MAY-20 |
| 22-MAY-20 |
| 25-MAY-20 |
| 26-MAY-20 |
| 27-MAY-20 |
| 28-MAY-20 |
| 29-MAY-20 |
| 01-JUN-20 |
| 02-JUN-20 |
| 03-JUN-20 |
| 04-JUN-20 |
| 05-JUN-20 |
| 08-JUN-20 |
| 09-JUN-20 |
| 10-JUN-20 |
| 11-JUN-20 |
| 12-JUN-20 |
| 15-JUN-20 |
| 16-JUN-20 |
| 17-JUN-20 |
| 18-JUN-20 |
| 19-JUN-20 |
| 22-JUN-20 |
| 23-JUN-20 |
| 24-JUN-20 |
| 25-JUN-20 |
| 26-JUN-20 |
| 29-JUN-20 |
| 30-JUN-20 |
| 01-JUL-20 |
| 02-JUL-20 |
| 03-JUL-20 |
| 06-JUL-20 |
| 07-JUL-20 |
| 08-JUL-20 |
| 09-JUL-20 |
| 10-JUL-20 |
| 13-JUL-20 |
| 14-JUL-20 |
| 15-JUL-20 |
| 16-JUL-20 |
| 17-JUL-20 |
| 20-JUL-20 |
| 21-JUL-20 |
| 22-JUL-20 |
| 23-JUL-20 |
| 24-JUL-20 |
| 27-JUL-20 |
| 28-JUL-20 |
| 29-JUL-20 |
| 30-JUL-20 |
| 31-JUL-20 |
| 03-AUG-20 |
| 04-AUG-20 |
| 05-AUG-20 |
| 06-AUG-20 |
| 07-AUG-20 |
| 10-AUG-20 |
| 11-AUG-20 |
| 12-AUG-20 |
| 13-AUG-20 |
| 14-AUG-20 |
| 17-AUG-20 |
| 18-AUG-20 |
| 19-AUG-20 |
| 20-AUG-20 |
| 21-AUG-20 |
| 24-AUG-20 |
| 25-AUG-20 |
| 26-AUG-20 |
| 27-AUG-20 |
| 28-AUG-20 |
| 31-AUG-20 |
| 01-SEP-20 |
| 02-SEP-20 |
| 03-SEP-20 |
| 04-SEP-20 |
| 07-SEP-20 |
| 08-SEP-20 |
| 09-SEP-20 |
| 10-SEP-20 |
| 11-SEP-20 |
| 14-SEP-20 |
| 15-SEP-20 |
| 16-SEP-20 |
| 17-SEP-20 |
| 18-SEP-20 |
| 21-SEP-20 |
| 22-SEP-20 |
| 23-SEP-20 |
| 24-SEP-20 |
| 25-SEP-20 |
| 28-SEP-20 |
| 29-SEP-20 |
| 30-SEP-20 |
| 01-OCT-20 |
| 02-OCT-20 |
| 05-OCT-20 |
| 06-OCT-20 |
| 07-OCT-20 |
| 08-OCT-20 |
| 09-OCT-20 |
| 12-OCT-20 |
| 13-OCT-20 |
| 14-OCT-20 |
| 15-OCT-20 |
| 16-OCT-20 |
| 19-OCT-20 |
| 20-OCT-20 |
| 21-OCT-20 |
| 22-OCT-20 |
| 23-OCT-20 |
| 26-OCT-20 |
| 27-OCT-20 |
| 28-OCT-20 |
| 29-OCT-20 |
| 30-OCT-20 |
| 02-NOV-20 |
| 03-NOV-20 |
| 04-NOV-20 |
| 05-NOV-20 |
| 06-NOV-20 |
| 09-NOV-20 |
| 10-NOV-20 |
| 11-NOV-20 |
| 12-NOV-20 |
| 13-NOV-20 |
| 16-NOV-20 |
| 17-NOV-20 |
| 18-NOV-20 |
| 19-NOV-20 |
| 20-NOV-20 |
| 23-NOV-20 |
| 24-NOV-20 |
| 25-NOV-20 |
| 26-NOV-20 |
| 27-NOV-20 |
| 30-NOV-20 |
| 01-DEC-20 |
| 02-DEC-20 |
| 03-DEC-20 |
| 04-DEC-20 |
| 07-DEC-20 |
| 08-DEC-20 |
| 09-DEC-20 |
| 10-DEC-20 |
| 11-DEC-20 |
| 14-DEC-20 |
| 15-DEC-20 |
| 16-DEC-20 |
| 17-DEC-20 |
| 18-DEC-20 |
| 21-DEC-20 |
| 22-DEC-20 |
| 23-DEC-20 |
| 24-DEC-20 |
| 25-DEC-20 |
| 28-DEC-20 |
| 29-DEC-20 |
| 30-DEC-20 |
| 31-DEC-20 |
| 01-JAN-21 |
| 04-JAN-21 |
| 05-JAN-21 |
| 06-JAN-21 |
| 07-JAN-21 |
| 08-JAN-21 |
| 11-JAN-21 |
| 12-JAN-21 |
| 13-JAN-21 |
| 14-JAN-21 |
| 15-JAN-21 |
| 18-JAN-21 |
| 19-JAN-21 |
| 20-JAN-21 |
| 21-JAN-21 |
| 22-JAN-21 |
| 25-JAN-21 |
| 26-JAN-21 |
| 27-JAN-21 |
| 28-JAN-21 |
| 29-JAN-21 |
| 01-FEB-21 |
| 02-FEB-21 |
| 03-FEB-21 |
| 04-FEB-21 |
| 05-FEB-21 |
| 08-FEB-21 |
| 09-FEB-21 |
| 10-FEB-21 |
| 11-FEB-21 |
| 12-FEB-21 |
| 15-FEB-21 |
| 16-FEB-21 |
| 17-FEB-21 |
| 18-FEB-21 |
| 19-FEB-21 |
| 22-FEB-21 |
| 23-FEB-21 |
| 24-FEB-21 |
| 25-FEB-21 |
| 26-FEB-21 |
| 01-MAR-21 |
| 02-MAR-21 |
| 03-MAR-21 |
| 04-MAR-21 |
| 05-MAR-21 |
| 08-MAR-21 |
| 09-MAR-21 |
db<>fiddle here
I have a table like this:
---------------------------------------------------------
| ArticleID |ReleaseTime | PurchaseTime |
--------------------------------------------------------
| 7 | 7/24/20 3:00 PM | NULL |
--------------------------------------------------------
| 5 | 7/16/20 1:00 PM | NULL |
---------------------------------------------------------
| 4 | 7/24/20 2:00 PM | NULL |
--------------------------------------------------------
| 1 | NULL | 7/25/20 5:45 PM |
--------------------------------------------------------
| 3 | NULL | 7/26/20 9:00 AM |
--------------------------------------------------------
| 3 | 7/25/20 8:30 AM | NULL |
---------------------------------------------------------
| 1 | 7/24/20 5:00 PM | NULL |
--------------------------------------------------------
| 1 | NULL | 7/25/20 6:00 PM |
---------------------------------------------------------
| 6 | 7/24/20 3:30 PM | NULL |
which needs to be sorted by ReleaseTime ASC and then by all PurchaseTime ASC for that ID. Result should look like this:
---------------------------------------------------------
| ArticleID |ReleaseTime | PurchaseTime |
--------------------------------------------------------
| 5 | 7/16/20 1:00 PM | NULL |
--------------------------------------------------------
| 4 | 7/24/20 2:00 PM | NULL |
---------------------------------------------------------
| 7 | 7/24/20 3:00 PM | NULL |
--------------------------------------------------------
| 6 | 7/24/20 3:30 PM | NULL |
--------------------------------------------------------
| 1 | 7/24/20 5:00 PM | NULL |
--------------------------------------------------------
| 1 | NULL | 7/25/20 5:45 PM |
--------------------------------------------------------
| 1 | NULL | 7/25/20 6:00 PM |
--------------------------------------------------------
| 3 | 7/25/20 8:30 AM | NULL |
---------------------------------------------------------
| 3 | NULL | 7/26/20 9:00 AM |
Any suggestion? Thanks in advance!
If I understand correctly, you want to sort by the minimum release time for each id and then sort by purchase time.
You can use a window function in the order by:
order by min(releasetime) over (partition by id),
id,
purchasetime asc
timesetup table (Unnormalized), this table is for time conditioning that used for filtering result
| time_id | period_from | period_to | session1_from | session1_to | session2_from | session2_to | session3_from | session3_to | session4_from | session4_to | session5_from | session5_to |
|---------|-------------|------------|---------------|-------------|---------------|-------------|---------------|-------------|---------------|-------------|---------------|-------------|
| 1 | 10/09/2015 | 11/09/2015 | 04:00:00 | 05:00:00 | 12:00:00 | 13:00:00 | 15:00:00 | 16:00:00 | 18:00:00 | 18:35:00 | 19:00:00 | 20:00:00 |
| 2 | 12/09/2015 | 13/09/2015 | 04:10:00 | 05:10:00 | 12:10:00 | 13:10:00 | 15:10:00 | 16:10:00 | 18:10:00 | 18:45:00 | 19:10:00 | 20:10:00 |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Normalized Version :
dateperiode table :
| period_id | date_from | date_to |
|-----------|------------|------------|
| 1 | 10/09/2015 | 11/09/2015 |
| 2 | 12/09/2015 | 13/09/2015 |
|-------------------------------------|
sessionrange table :
| sessionrange_id | session | session_from | session_to |
|-----------------|-----------|--------------|-------------|
| 1 | 1 | 04:00:00 | 05:00:00 |
| 2 | 2 | 12:00:00 | 13:00:00 |
| 3 | 3 | 15:00:00 | 16:00:00 |
| 4 | 4 | 18:00:00 | 18:35:00 |
| 5 | 5 | 19:00:00 | 20:00:00 |
| 6 | 1 | 04:10:00 | 05:10:00 |
| 7 | 2 | 12:10:00 | 13:10:00 |
| 8 | 3 | 15:10:00 | 16:10:00 |
| 9 | 4 | 18:10:00 | 18:45:00 |
| 10 | 5 | 19:10:00 | 20:10:00 |
|-----------------|-----------|--------------|-------------|
timsetup table :
| period_id | sessionrange_id |
|-----------|-----------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 2 | 6 |
| 2 | 7 |
| 2 | 8 |
| 2 | 9 |
| 2 | 10 |
|-----------|-----------------|
checktime table, this table is containing data from fingerprint scanner (tapping_time field)
|userid | tapping_time |
|--------|----------------------|
|234 | 10/09/2015 04:20:04 |
|234 | 10/09/2015 04:20:06 |
|234 | 10/09/2015 12:15:35 |
|234 | 10/09/2015 15:31:11 |
|234 | 10/09/2015 18:19:10 |
|234 | 10/09/2015 18:19:15 |
|234 | 10/09/2015 19:37:53 |
|234 | 11/09/2015 04:38:42 |
|234 | 11/09/2015 04:38:47 |
|234 | 11/09/2015 12:21:27 |
|234 | 11/09/2015 15:45:30 |
|234 | 11/09/2015 15:45:37 |
|234 | 11/09/2015 18:27:15 |
|234 | 11/09/2015 19:55:08 |
|234 | 11/09/2015 19:55:12 |
|234 | 12/09/2015 04:45:10 |
|234 | 12/09/2015 04:45:13 |
|234 | 12/09/2015 13:12:55 |
|234 | 12/09/2015 16:35:08 |
|234 | 12/09/2015 18:49:10 |
|234 | 12/09/2015 20:20:57 |
|234 | 13/09/2015 05:11:56 |
|234 | 13/09/2015 05:12:05 |
|234 | 13/09/2015 12:45:13 |
|234 | 13/09/2015 15:47:25 |
|234 | 13/09/2015 18:31:27 |
|234 | 13/09/2015 18:31:30 |
|234 | 13/09/2015 20:01:18 |
|-------------------------------|
I'm trying to create an Access query that groups the date/time into the periods in the timesetup table. tapping_time should have only one result per session (even when user tapping more than once at a time) based on sessions condition in timesetup table. Also users tap times outside of sessions (time between session_from and session_to) field should never counted or appear in result table.
Desired Result:
| userid | date | tapping_on | session |
|--------|------------|------------|---------|
| 234 | 10/09/2015 | 04:20:04 | 1 |
| 234 | 10/09/2015 | 12:15:35 | 2 |
| 234 | 10/09/2015 | 15:31:11 | 3 |
| 234 | 10/09/2015 | 18:19:10 | 4 |
| 234 | 10/09/2015 | 19:37:53 | 5 |
| 234 | 11/09/2015 | 04:38:42 | 1 |
| 234 | 11/09/2015 | 12:21:27 | 2 |
| 234 | 11/09/2015 | 15:45:30 | 3 |
| 234 | 11/09/2015 | 18:27:15 | 4 |
| 234 | 11/09/2015 | 19:55:08 | 5 |
| 234 | 12/09/2015 | 04:45:10 | 1 |
| 234 | 12/09/2015 | 16:35:08 | 3 |
| 234 | 12/09/2015 | 18:49:10 | 4 |
| 234 | 13/09/2015 | 12:45:13 | 2 |
| 234 | 13/09/2015 | 15:47:25 | 3 |
| 234 | 13/09/2015 | 18:31:27 | 4 |
| 234 | 13/09/2015 | 20:01:18 | 5 |
|--------------------------------------------|
You might have gone a little bit overboard on the normalization, since the relation between dateperiode and sessionrange looks like a one to many relationship to me, thus doesn't require a junction table.
With the following data, I believe the following query should work, but unfortunately I don't have a decent test setup at the ready:
SELECT userid, tDatepart As [date], Min(t.tapping_time) As tapping_on, session FROM
(
SELECT session_from, session_to, date_from, date_to, session
FROM timsetup i
INNER JOIN dateperiode d ON i.period_id = d.period_id
INNER JOIN sessionrange q ON t.sessionrange_id = q.sessionrange_id
) As s
INNER JOIN
(SELECT Int(tapping_time) As tDatepart, CDate(tapping_time - Int(tapping_time)) As tTimepart, userid, tapping_time
FROM checktime) t
ON (t.tDatePart >= s.date_from AND t.tDatePart <= s.date_to AND t.tTimePart >= s.session_from AND t.tTimePart <= s.session_to)
GROUP BY userid, tDatepart, session
I'm filling empty arrivals time in a bus timetable, I have start,end and some midpoints times, I do by calculating average speed between start and end route, but results are inexact, so must use intervals speed to compute arrivals times.
General Timetable shown in First Code Snippet, please run to see table, and Computed arrival times for Intervals in Second Code Snippet .
What I'm trying to do is to fill in av_speed ingeneral timetable with Intervals average speed, i.e. put 251.17 into timetable stops_sequences 1 to 5, 230.68 into sequences 6 to 10 and so on.
+============+===============+==============+==========+===========+===========+==========+
| stoptimeid | stop_sequence | arrival_time | distance | dist_trav | time_trav | av_speed |
+============+===============+==============+==========+===========+===========+==========+
| 54689 | 1 | 6:05:00 | 0,00 | | | 220,98 |
| 54690 | 2 | | 0,35 | | | 220,98 |
| 54691 | 3 | | 0,49 | | | 220,98 |
| 54692 | 4 | | 0,91 | | | 220,98 |
| 54693 | 5 | 6:10:00 | 1,19 | | | 220,98 |
| 54694 | 6 | | 1,50 | | | 220,98 |
| 54695 | 7 | | 1,67 | | | 220,98 |
| 54696 | 8 | | 1,96 | | | 220,98 |
| 54697 | 9 | | 2,16 | | | 220,98 |
| 54698 | 10 | 6:15:00 | 2,49 | | | 220,98 |
| 54699 | 11 | | 2,64 | | | 220,98 |
| 54700 | 12 | | 3,11 | | | 220,98 |
| 54701 | 13 | | 3,79 | | | 220,98 |
| 54702 | 14 | | 4,14 | | | 220,98 |
| 54703 | 15 | | 4,39 | | | 220,98 |
| 54704 | 16 | | 4,96 | | | 220,98 |
| 54705 | 17 | | 5,10 | | | 220,98 |
| 54706 | 18 | 6:25:00 | 5,21 | | | 220,98 |
+============+===============+==============+==========+===========+===========+==========+
+============+===============+==============+==========+===========+===========+==========+
| stoptimeid | stop_sequence | arrival_time | distance | dist_trav | time_trav | av_speed |
+============+===============+==============+==========+===========+===========+==========+
| 54689 | 1 | 6:05:00 | 0,00 | | | |
| 54693 | 5 | 6:10:00 | 1,19 | 1,194423 | 300 | 251,17 |
| 54698 | 10 | 6:15:00 | 2,49 | 1,300520 | 300 | 230,68 |
| 54706 | 18 | 6:25:00 | 5,21 | 2,715214 | 600 | 220,98 |
+============+===============+==============+==========+===========+===========+==========+
Have checked the use of a CASE function that fills in descending order, fills 18 to 1, then fills 10 to 5 and finally 5 to 1, so final result it's ok.
This is what I tried but only fill with only one av_speed, the last one.
CREATE OR REPLACE VIEW arrivaltime_check AS
SELECT
CASE
WHEN arrivaltime.stop_sequence<=foo.stop_sequence THEN foo.av_speed
ELSE foo.av_speed END,foo.trip_id2,foo.stop_sequence
FROM
public.arrivaltime,
(SELECT
b.rid,
b.trip_id2,
b.stoptimeid,
b.stop_sequence,
b.arrival_time,
extract(epoch from (b.arrival_time)) as time_epoch,
b.distance,
b.distance -lag(b.distance,1) over (partition by b.trip_id2 ) as dist_trav,
b.time_epoch -lag(b.time_epoch,1) over (partition by b.trip_id2 ) as time_trav,
(b.time_epoch -lag(b.time_epoch,1) over (partition by b.trip_id2 ))/(b.distance -lag(b.distance,1) over (partition by b.trip_id2 )) as av_speed
from public.timecalc1 b
order by b.trip_id2,b.stop_sequence desc) foo;
foo subquery only computes values when arrivaltimes is not null,
How can I fill in timetable with intervals av_speed?