I am trying to run a select query that will pull all meds scheduled at 2300 for a date range. Is there a way I can convert the scheduled date/time to just hour? This is what I have so far:
SELECT DISTINCT USERCODE,
TRANSACTIONID,
ACTION,
TRANSACTIONHOUR,
SOURCE,
RXNUMBER,
DESCRIPTION,
MASTERPATIENTID,
FACILITYCODE,
ADMINISTRATIONTIME
FROM ( ABC.TL TL
INNER JOIN
ABC.S_VIEW S_VIEW
ON (TL.RXNUMBER = S_VIEW.RXNUMBER))
INNER JOIN
ABC.PV PATIENTVISIT
ON (TL.MASTERPATIENTID = PV.MASTERPATIENTID)
WHERE (TL.USERCODE NOT IN ('ABC'))
AND (TL.ACTION IN ('A', 'DC'))
AND (TL.TRANSACTIONHOUR BETWEEN to_date('2011-07-01 00:00:00', 'yyyy/mm/dd hh24:mi:ss') AND to_date('2011-09-30 23:59:59', 'yyyy/mm/dd hh24:mi:ss')
I would like the query to include all dispense during the specified dates but only at 2300 time. Database is oracle 10g.
First, you have given the date string in a different format and the format in different format. Make it consistent.
Try this:
SELECT DISTINCT USERCODE,
TRANSACTIONID,
ACTION,
TRANSACTIONHOUR,
SOURCE,
RXNUMBER,
DESCRIPTION,
MASTERPATIENTID,
FACILITYCODE,
ADMINISTRATIONTIME
FROM ( ABC.TL TL
INNER JOIN
ABC.S_VIEW S_VIEW
ON (TL.RXNUMBER = S_VIEW.RXNUMBER))
INNER JOIN
ABC.PV PATIENTVISIT
ON (TL.MASTERPATIENTID = PV.MASTERPATIENTID)
WHERE (TL.USERCODE NOT IN ('ABC'))
AND (TL.ACTION IN ('A', 'DC'))
AND (TL.TRANSACTIONHOUR BETWEEN to_date('2011/07/01 00:00:00', 'yyyy/mm/dd hh24:mi:ss') AND to_date('2011/09/30 23:59:59', 'yyyy/mm/dd hh24:mi:ss')
AND TO_CHAR(TL.TRANSACTIONHOUR, 'HH24MI') = '2300' --THIS IS THE NEW CONDITION
Related
I wanted to select the rows whose date is greater than last processed date.
The last processed date is in hist table.
select id
from table1
where to_date(last_updated_date,'MM/DD/YYYY HH24:MI:SS')
> select to_date(nvl(max(last_updated_date),sysdate),'MM/DD/YYYY HH24:MI:SS')
from table1_hist;
This return ORA-01843: not a valid month error.
I had manually inserted row in table1 as TO_DATE('09/26/2019 14:37:49', 'MM/DD/YYYY HH24:MI:SS') while in table1_hist there are no rows.
But when I query the table using sql developer for table1 I get the value appearing as '26-SEP-19'. last_updated_date is a date field.
I wanted to to get the id's from table1 after the last executed time.
Thanks
SYSDATE is a DATE value. Using TO_DATE() on a value which is already a DATE is useless.
Try
where last_updated_date >
(select MAX(NVL(last_updated_date, SYSDATE)) from table1_hist);
I will suggest using an analytical function as following:
SELECT
ID
FROM
(
SELECT
T.ID,
T.LAST_UPDATED_DATE,
MAX(TH.LAST_UPDATED_DATE) OVER() AS M_LAST_UPDATED_DATE
FROM
TABLE1 T
JOIN TABLE1_HIST TH ON ( T.ID = TH.ID )
-- i am guessing that this is the join condition or you can use your own conition here
)
WHERE
LAST_UPDATED_DATE > COALESCE(M_LAST_UPDATED_DATE, SYSDATE)
Cheers!!
First of all, you don't need any to_date conversion for comparison.
If table1_hist has at least one non-null value, then using
select t.id
from table1 t
where t.last_updated_date > ( select max(th.last_updated_date) from table1_hist th )
is enough, but all those values are null then use a query such as this :
select t.id
from table1 t
where t.last_updated_date >
(select nvl(max(th.last_updated_date),t.last_updated_date-1) from table1_hist th)
Demo
I'm trying to query my Oracle script on Toad but got slow response, about 4-8 seconds.
The script query is about count, below is mine:
SELECT COUNT(*)
AS TOTALS
FROM(SELECT S.BADGEID_FK, S.SHIFT, S.STATUS, E.BADGEID, E.FIRSTNAME, E.LASTNAME
FROM WA_SEW_TBL_EMP_INFO S, WA_GA_TBL_EMPLOYEES E
WHERE S.BADGEID_FK = E.BADGEID AND S.STATUS = 'Attend' AND S.SHIFT = 'Morning'
AND S.BADGEID_FK NOT IN(SELECT EMPID
FROM WA_SEW_TBL_RESULTS
WHERE TO_CHAR(SYSTEM_DATE, 'YYYY-MM-DD') = '2017-08-30'
AND TO_CHAR(SYSTEM_DATE, 'HH24:MI') >= '07:00'
AND TO_CHAR(SYSTEM_DATE, 'HH24:MI') <= '19:29'))
I tried to add indexing to some column, but there is no effect.
Is there any way to do that query? or any trick?
This part:
WHERE TO_CHAR(SYSTEM_DATE, 'YYYY-MM-DD') = '2017-08-30'
AND TO_CHAR(SYSTEM_DATE, 'HH24:MI') >= '07:00'
AND TO_CHAR(SYSTEM_DATE, 'HH24:MI') <= '19:29'
Would be better rewritten as:
WHERE SYSTEM_DATE between to_date ('2017-08-30 07:00:00', 'YYYY-MM-DD HH24:MI:SS')
and to_date ('2017-08-30 19:29:59', 'YYYY-MM-DD HH24:MI:SS')
That will allow any index on SYSTEM_DATE to be used.
One obvious suspect is your date manipulation in the IN list. You should never, ever use functions around dates - that kills any ability of Oracle to use an index on the date column.
Instead:
where system_date >= to_date('2017-08-30 07:00', 'yyyy-mm-dd hh24:mi')
and system_date < to_date('2017-08-30 19:30', 'yyyy-mm-dd hh24:mi')
(the second inequality is strict, if you want to exclude 7:30pm sharp).
I was able to eliminate most of the subqueries but I'm not sure it will result in the performance gain w/o knowledge of table size and indexes. Posting the execution plan would help us understand where your bottleneck is.
SELECT count(*) as Totals
FROM WA_SEW_TBL_EMP_INFO S
INNER JOIN WA_GA_TBL_EMPLOYEES E
ON S.BADGEID_FK = E.BADGEID
LEFT JOIN WA_SEW_TBL_RESULTS R
ON S.BADGEID_FK =R.EMPID
-- Others already addressed what needs to happen here.
AND TO_CHAR(R.SYSTEM_DATE, 'YYYY-MM-DD') = '2017-08-30'
AND TO_CHAR(R.SYSTEM_DATE,'HH24:MI') >= '07:00'
AND TO_CHAR(R.SYSTEM_DATE,'HH24:MI') <= '19:29'
WHERE S.STATUS = 'Attend'
AND S.SHIFT = 'Morning'
AND R.EmpID is null
I am trying to get data for a specific date range, I do it like this:
select EntryID
min(dtUsedDate) dtFirstUsedDate,
max(dtUsedDate) dtLastUsedDate
from tblEntrance e
where e.dtUsedDate between to_date('2016-02-08 10:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND to_date('2016-02-08 10:15:59', 'yyyy-mm-dd hh24:mi:ss')
(dtFirstUsedDate and dtLastUsedDate are getting called in a outer select, so don't worry much about them for now)
What I get are the entrances (records) that are only between those dates/time, so dtFirstUsedDate and dtLastUsedDate, both toghether in between that date range. But what I need is those two to be independent, like the dtFirstUsed must be between that max and min date and dtLastUsed must be between that max and min date.
I hope my question is understandable and someone can help me.
I think you are looking for this..
SELECT e.EntryID
,MIN(e.dtUsedDate) dtFirstUsedDate
,MAX(e.dtUsedDate) dtLastUsedDate
FROM tblEntrance e
GROUP
BY e.EntryID
HAVING MIN(e.dtUsedDate) BETWEEN TO_DATE('2016-02-08 10:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND TO_DATE('2016-02-08 10:15:59', 'yyyy-mm-dd hh24:mi:ss')
AND MAX(e.dtUsedDate) BETWEEN TO_DATE('2016-02-08 10:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND TO_DATE('2016-02-08 10:15:59', 'yyyy-mm-dd hh24:mi:ss')
I want to fetch some records which should give results based on certain time.
That means from 2.30 Am to 6.00 AM.
I tried using the between function, but i am not getting.
MTRDCRE between (to_char(to_date('16-03-2016 02:50:00', 'dd-mm-yyyy hh24:mi:ss')) , to_char(to_date('16-03-2016 05:50:00', 'dd-mm-yyyy hh24:mi:ss')))
Try this:
select *
from yourtable
where MTRDCRE >= to_char(to_date('16-03-2016 02:50:00', 'dd-mm-yyyy hh24:mi:ss'))
and MTRDCRE <= to_char(to_date('16-03-2016 05:50:00', 'dd-mm-yyyy hh24:mi:ss'))
If your column MTRDCRE is date you don't need to_char().
i am facing problem kindly help me my code is
SELECT 'NULL' AS sender_mobile,
ci.str_identification AS receiver_mobile,
TO_CHAR (cc.dat_creation, 'YYYYMMDD') AS datee,
TO_CHAR (cc.dat_creation, 'hh24:Mi:ss') AS timee
FROM pbxhbl.agent_details ad
INNER JOIN pbxmob.customers_identifications ci
ON ad.id_customer = ci.id_customer
INNER JOIN pbxmob.customers_credentials cc
ON ci.id_customer = cc.id_customer
INNER JOIN pbxmob.credential_types ct
ON cc.id_credential_type = ct.id_credential_type
WHERE cc.dat_creation BETWEEN '21-August-2015 06:00:00 PM'
AND '21-August-2015 06:59:59 PM';
You'll be better off if you are explicit about your date format. Here is an example that does the same thing as your query:
where cc.DAT_CREATION
between to_date('2015-08-21 18:00:00', 'YYYY-MM-DD HH24:MI:SS')
and to_date('2015-08-21 18:59:59', 'YYYY-MM-DD HH24:MI:SS')