How can I create an overview without duplicates in column "CHUTE"?
Eg in below result: AX002 = 129
select
COUNT(PPL_SDCC),
substr (PPL_DISCHARGEID,6,5) as CHUTE
from T1LOG.PPL_PIECELOG
where
PPL_DISCHARGEID like 'PS%X%'
and substr (PPL_SDCC,11,1)='1'
and PPL_DISCHARGETIME between TO_DATE ('01/08/2021 10:00:00' , 'DD/MM/YYYY hh24:mi:ss') and TO_DATE ('02/08/2021 09:00:00' , 'DD/MM/YYYY hh24:mi:ss')
group by PPL_DISCHARGEID
order by CHUTE
RESULT:
Rahther than grouping only column PPL_DISCHARGEID, You should actually include the exact calculation in group by clause. Please update your group by clause in your query to -
SELECT COUNT(PPL_SDCC),
SUBSTR(PPL_DISCHARGEID,6,5) as CHUTE
FROM T1LOG.PPL_PIECELOG
WHERE PPL_DISCHARGEID like 'PS%X%'
AND SUBSTR(PPL_SDCC,11,1)='1'
AND PPL_DISCHARGETIME BETWEEN TO_DATE('01/08/2021 10:00:00', 'DD/MM/YYYY hh24:mi:ss') and TO_DATE('02/08/2021 09:00:00', 'DD/MM/YYYY hh24:mi:ss')
GROUP BY SUBSTR(PPL_DISCHARGEID,6,5)
ORDER BY CHUTE;
Related
I'm trying to get an account of a daily count from an Oracle query to display count by hours from 14:00 to 19:00. I'm using this query. I want to group the count output.
Select count(*), extract(hour from eventtime) as hours
from TR_MFS_LOADCARRIER
WHERE eventid = 5
And eventtime BETWEEN to_date('05/09/2022 14:00:00', 'dd/mm/yyyy hh24:mi:ss')
and to_date('05/09/2022 19:00:00', 'dd/mm/yyyy hh24:mi:ss')
group by hours
It fails where am I going wrong.
The hours alias is defined in the SELECT clause after the GROUP BY clause is evaluated so it cannot be used in the GROUP BY clause; use EXTRACT(hour from eventtime) instead.
Select count(*),
extract(hour from eventtime) as hours
from TR_MFS_LOADCARRIER
WHERE eventid = 5
And eventtime BETWEEN to_date('05/09/2022 14:00:00', 'dd/mm/yyyy hh24:mi:ss')
and to_date('05/09/2022 19:00:00', 'dd/mm/yyyy hh24:mi:ss')
group by extract(hour from eventtime)
If your eventtime column is a DATE data-type then you cannot EXTRACT the hours field and need to cast it to a TIMESTAMP data-type:
Select count(*),
extract(hour from CAST(eventtime AS TIMESTAMP)) as hours
from TR_MFS_LOADCARRIER
WHERE eventid = 5
And eventtime BETWEEN to_date('05/09/2022 14:00:00', 'dd/mm/yyyy hh24:mi:ss')
and to_date('05/09/2022 19:00:00', 'dd/mm/yyyy hh24:mi:ss')
group by extract(hour from CAST(eventtime AS TIMESTAMP))
fiddle
So i've got this code:
SELECT to_char(dstamp, 'HH24') as HOUR, SUM(update_qty) total_received
FROM inventory_transaction
WHERE dstamp BETWEEN to_date('28/05/2021 18:00:00', 'dd/mm/yyyy hh24:mi:ss')
AND to_date('29/05/2021 06:00:00', 'dd/mm/yyyy hh24:mi:ss')
AND code = 'Receipt'
GROUP BY to_char(dstamp, 'HH24')
ORDER BY HOUR ASC ;
Im trying to add an additional column to it, thats gonna be showing me total_putaway, so exactly the same query, but code = 'Putaway'. I was trying to do it by the CROSS JOIN, or some subqueries but it doesent really work, as i want to group my records by hour so 12 rows in total.
Use conditional aggregation:
SELECT to_char(dstamp, 'HH24') as HOUR,
SUM(CASE WHEN code = 'Receipt' THEN update_qty END) as total_received,
SUM(CASE WHEN code = 'Putaway' THEN update_qty END) as total_putaway
FROM inventory_transaction
WHERE dstamp BETWEEN to_date('28/05/2021 18:00:00', 'dd/mm/yyyy hh24:mi:ss') AND
to_date('29/05/2021 06:00:00', 'dd/mm/yyyy hh24:mi:ss')
GROUP BY to_char(dstamp, 'HH24')
ORDER BY HOUR ASC
I have a big table with start times and end times.
It looks like this:
Start_time date-time (format: dd/mm/yyyy hh24:mi:ss),
End_time date-time (format: dd/mm/yyyy hh24:mi:ss)
I might have rows that represent time which is included in other rows
My desiarable result is a table that can solve this containing. I want take any firsy time and see next to him the last end time.
I tried to left join the table with itself on start time between start time and end time if the end of the second is greater than the ending of the first. Then to do a sliding window and take the max end time with sliding window or even with group by.
However, this idea does take in account then I may have, for example:
10:05-10:10
10:07-10:12
10:09-10:15
10:11-10:20
So when I am joined I allegedly get 10:05-10:15 and 10:11-10:20. The row of 10:11 is not joined to the first row because it is not included in that time.
I have here again the same problem I had in the begining.
My desiarable result is actually for the rows above:
10:05-10:20
Seem to be a difficult problem.
I dont know plsql but thought maybe about doing some function that repeat this query until it has nothing to join?
Hope to get ypur help!
Thanks.
I dont know how to format, but you can copy paste in your editor and than format.
Insted of my test data with operator "with" you may use your table.
I suppose you have some sort of ID so i include it:
with test_table as (
select 1 id, to_date('2019-04-07 10:05', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-07 10:08', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 2 id, to_date('2019-04-07 10:07', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-07 10:10', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 3 id, to_date('2019-04-07 10:11', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-07 10:15', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 4 id, to_date('2019-04-07 10:12', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-07 10:20', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 5 id, to_date('2019-04-08 10:05', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-08 10:10', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 6 id, to_date('2019-04-08 10:07', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-08 10:12', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 7 id, to_date('2019-04-08 10:09', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-08 10:15', 'yyyy-mm-dd hh24:mi') end_time from dual union all
select 8 id, to_date('2019-04-08 10:11', 'yyyy-mm-dd hh24:mi') start_time, to_date('2019-04-08 10:20', 'yyyy-mm-dd hh24:mi') end_time from dual
)
select id, to_char(start_time, 'yyyy-mm-dd hh24:mi') start_time, to_char(end_time, 'yyyy-mm-dd hh24:mi') end_time,
(SELECT MAX(to_char(end_time, 'yyyy-mm-dd hh24:mi'))
from test_table t2
connect by nocycle
prior t2.id != t2.id and
PRIOR end_time > start_time and
PRIOR start_time < end_time
start with t2.id = t1.id) max_date
from test_table t1;
Following is my sql code. I want all the ids with description that have create time or update time between 25-Aug to 31-Aug.
Currently, if an id has create time between 25-Aug to 31-Aug, though the update time is in Sep, the sql is returning the record for the update time.
Ideally it should only return the records that have create or update times between 25-Aug to 31-Aug. Why is my case ignoring the condition for update time?
select t1.id, t1.desc,
Case
WHEN t1.create_time IS NOT NULL
THEN t1.create_time
WHEN t2.update_time IS NOT NULL
THEN t2.update_time
END AS "TimeStamp"
from t1, t2
where t1.id=t2.id(+)
AND ( t1.create_time BETWEEN TO_DATE ('25-AUG-2016 00:00:00',
'dd-mon-yyyy HH24:Mi:SS')
AND TO_DATE ('31-AUG-2016 23:59:59',
'dd-mon-yyyy HH24:Mi:SS')
OR ( t2.update_time
BETWEEN TO_DATE ('25-AUG-2016 00:00:00',
'dd-mon-yyyy HH24:Mi:SS')
AND TO_DATE ('31-AUG-2016 23:59:59',
'dd-mon-yyyy HH24:Mi:SS')
)
)
In your query conditions for create_time and update_time combined by OR. When we combine two condition by OR, if one of condition is TRUE then result also will be TRUE. As result, in your query if create_time falls within the range, then not metter update_time falls within the range or not.
You can use subqueries to filter tables before you join them
select t1_1.id, t1_1.desc,
CASE
WHEN t1_1.create_time IS NOT NULL
THEN t1_1.create_time
WHEN t2_1.update_time IS NOT NULL
THEN t2_1.update_time
END AS "TimeStamp"
from (select * from t1
where t1.create_time BETWEEN TO_DATE ('25-AUG-2016 00:00:00',
'dd-mon-yyyy HH24:Mi:SS')
AND TO_DATE ('31-AUG-2016 23:59:59',
'dd-mon-yyyy HH24:Mi:SS')) t1_1,
(select * from t2
where t2.update_time BETWEEN TO_DATE ('25-AUG-2016 00:00:00',
'dd-mon-yyyy HH24:Mi:SS')
AND TO_DATE ('31-AUG-2016 23:59:59',
'dd-mon-yyyy HH24:Mi:SS')) t2_1
where t1_1.id_=t2_1.id_(+)
Try this:
SELECT t1.id, t1.desc,
t1.create_time 'TimeStamp'
from t1
where
trunc(t1.create_time) BETWEEN TO_DATE ('25-AUG-2016','DD-MON-YYYY')
AND TO_DATE ('31-AUG-2016','DD-MON-YYYY')
union all
SELECT t1.id, t1.desc,
t1.update_time 'TimeStamp'
from t1
where
( trunc(t1.update_time)
BETWEEN TO_DATE ('25-AUG-2016','DD-MON-YYYY')
AND TO_DATE ('31-AUG-2016','DD-MON-YYYY')
)
Here is my code:
SELECT SRV_NAME, TOT_CPU, TOT_MEM, SNAP_DATE
FROM capacity.SRV_CAPACITY_SEV
WHERE SRV_NAME in ('absshs1p", "AA03server', 'AA02server', 'BA01server', 'BA03server', 'BC03server') AND SNAP_DATE BETWEEN to_date('10-jun-2012 00:00:00', 'dd-mon-yyyy hh24:mi:ss') AND to_date('12-jun-2012 00:00:00', 'dd-mon-yyyy hh24:mi:ss')
ORDER BY SRV_NAME desc, SNAP_DATE desc;
How would I query for servers that begin with certain characters? For example, how could I serach for servers that only begin with 'AA'?
I am using Oracle SQL btw.
You can do this
WHERE SRV_NAME LIKE 'AA%'