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%'
Related
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;
I am going nuts trying to fix these small issues. I'm trying to have the DATE_TIME be in order and want the MISTI to be in order. The screenshot shows the MISTI in order but not the dates. Is there any way I can remove the 2019 date out of the query too? THANK YOU IN ADVANCE!
select To_Char(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS') AS date_time, PARM, TRK_ID as MISTI,
num_value AS PM_NUMBER
from trk_id_parm
Where trk_id IN ('IMV01','IMV02','IMV03', 'IMV04', 'IMV05')
and state = 'SMM'
and PARM = '4440'
order by MISTI ASC, DATE_TIME DESC;
As it appears that your state_out_dttm column is actually text, and not a date, you should be using TO_DATE, not TO_CHAR, to first convert to a bona fide date before trying to sort:
SELECT
TO_DATE(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS') AS date_time,
PARM,
TRK_ID AS MISTI,
num_value AS PM_NUMBER
FROM trk_id_parm
WHERE
trk_id IN ('IMV01','IMV02','IMV03', 'IMV04', 'IMV05') AND
state = 'SMM' AND
PARM = '4440' AND
EXTRACT(YEAR FROM TO_DATE(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS')) <> 2019
ORDER BY
MISTI,
TO_DATE(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS') DESC;
I have a table with point features in Oracle Spatial and I am trying to select distinct features by combining its name and date and counting its occurrences during a period.
The problem is that I can't get the spatial information (sdo_geometry type) in the output.
select
a.feature_name,
count(distinct(a.feature_name||trunc(a.date,'DD'))) as number_of_days,
first(geometry) -- < --- the problem is here!
from table a
where
a.date >=to_date('01/01/2016 00:00:00', 'DD/MM/YYYY HH24:MI:SS')
and a.date<to_date('02/01/2016 00:00:00', 'DD/MM/YYYY HH24:MI:SS')
group by a.feature_name;
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')
)
In the below query condition is failing but logically it should pass. Is something wrong with to_char??
SELECT data
FROM table1
WHERE
TO_CHAR(TO_DATE(value1, 'DD/MM/RRRR HH24:MI:SS'), 'DD/MM/RRRR HH24:MI:SS') <=
TO_CHAR( SYSDATE, 'DD/MM/RRRR HH24:MI:SS')
AND TO_CHAR(TO_DATE(value2, 'DD/MM/RRRR HH24:MI:SS'), 'DD/MM/RRRR HH24:MI:SS') >=
TO_CHAR( SYSDATE, 'DD/MM/RRRR HH24:MI:SS');
value1='02/07/2014 12:30:10'
value2='06/08/2015 09:57:33'
in both the conditions it is only checking the dates i.e.,02<=07 (7th is todays date).First condition is getting satisfied regardless of month and year.if i change value1 to '15/08/2014 12:30:10' it is failing. Same with second condition.
Why are you comparing dates as strings? This also begs the question of why you would store dates as strings in the first place. You should store date/times using the built-in types.
Try this instead:
SELECT data
FROM table1
WHERE TO_DATE(value1, 'DD/MM/RRRR HH24:MI:SS') <= sysdate AND
TO_DATE(value2, 'DD/MM/RRRR HH24:MI:SS') >= sysdate;
Your problem is presumably that you are comparing strings, rather than dates. And the format you are using DD/MM/YYYY doesn't do comparisons the same way. This is, in fact, why you should just use the ISO format of YYYY-MM-DD whenever you are storing date/time values in strings (which I don't recommend in most cases anyway).
If your values are already stored in proper types, then you can just do:
SELECT data
FROM table1
WHERE value1 <= sysdate AND
value2 >= sysdate;
If these are timestamps with time zone, then you can use SYSTIMESTAMP instead of SYSDATE.
Finally after lot of googling i got the solution. Below query is working for me :) :)
SELECT MESSAGE
FROM TABLE1
WHERE TO_TIMESTAMP(value1, 'DD/MM/RRRR HH24:MI:SS') <= CAST(SYSDATE AS TIMESTAMP)
AND TO_TIMESTAMP(value2, 'DD/MM/RRRR HH24:MI:SS') >= CAST(SYSDATE AS TIMESTAMP);
value1='02/07/2014 12:30:10'
value2='06/08/2015 09:57:33'
You have to use TO_TIMESTAMP instead of TO_DATE:
SELECT data
FROM table1
WHERE
TO_CHAR(TO_TIMESTAMP(value1, 'DD/MM/RRRR HH24:MI:SS'), 'DD/MM/RRRR HH24:MI:SS') <=
TO_CHAR( SYSDATE, 'DD/MM/RRRR HH24:MI:SS')
AND TO_CHAR(TO_TIMESTAMP(value2, 'DD/MM/RRRR HH24:MI:SS'), 'DD/MM/RRRR HH24:MI:SS') >=
TO_CHAR( SYSDATE, 'DD/MM/RRRR HH24:MI:SS');