select
d.FK_SOCRD_ID,
d.SUBJECT_NUMBER,
c.PTLASTNAME || ', ' || c.PTFIRSTNAME as "Patient name",
min(b.created) as Data Entry 1,
max(b.created) as Data Entry 2,
min(b.created) - max(b.created)
|| ' days and '
|| TO_CHAR(to_date('01/01/2000', 'MM-DD-YYYY')
+ (b.created - b.created), 'HH24:MI:SS' ) AS Diff
FROM
CR_MDT a
full outer join CR_MDT_VERIFY b
on a.CR_MDT_ID = b.FK_CR_MDT_ID
left join patient c
on a.FK_SOCRD_ID = c.SOCRD_ID
left join PT_STUDY d
on c.SOCRD_ID = d.FK_SOCRD_ID
where
a.CR_MDT_DT between TO_DATE('01/01/2017', 'mm/dd/yyyy')
and TO_DATE('12/31/2017', 'mm/dd/yyyy')
group by
d.FK_SOCRD_ID,
d.SUBJECT_NUMBER,
c.PTLASTNAME,
c.PTFIRSTNAME,
b.created
How can I subtract the max(b.created) from min(b.created)? I'm stuck on this part:
|| TO_CHAR(to_date('01/01/2000', 'MM-DD-YYYY')
+ (b.created - b.created), 'HH24:MI:SS' ) AS Diff
I need help on how to write the subtraction part since b.created max and min are from the same column.
You can put that query in a subquery and do the subtraction outside:
SELECT *
, max_created - min_created as date_difference
FROM
(
SELECT d.FK_SOCRD_ID
, d.SUBJECT_NUMBER
, c.PTLASTNAME || ', ' || c.PTFIRSTNAME AS "Patient name"
, min(b.created) AS min_created
, max(b.created) AS max_created
FROM CR_MDT a
FULL JOIN CR_MDT_VERIFY b
ON a.CR_MDT_ID = b.FK_CR_MDT_ID
LEFT JOIN patient c
ON c.SOCRD_ID = a.FK_SOCRD_ID
LEFT JOIN PT_STUDY d
ON d.FK_SOCRD_ID = c.SOCRD_ID
WHERE a.CR_MDT_DT BETWEEN TO_DATE('01/01/2017', 'mm/dd/yyyy') AND TO_DATE('12/31/2017', 'mm/dd/yyyy')
GROUP BY d.FK_SOCRD_ID
, d.SUBJECT_NUMBER
, c.PTLASTNAME
, c.PTFIRSTNAME
) subq
You might have to change the datediff function depending on the dbms you are using.
EDIT: I updated the date subtraction part now that i know you use Oracle.
You can use floor(), leave only fractional part of difference and convert it to hh24:mi:ss, like here:
select id, mx, mn, floor(diff) ||' days and '||
to_char(trunc(sysdate) + diff - floor(diff), 'hh24:mi:ss') diff
from (select id, max(created) mx, min(created) mn,
max(created) - min(created) diff
from test group by id)
Test data:
create table test (id number(3), created date);
insert into test values (1, timestamp '2017-09-12 01:00:00');
insert into test values (1, timestamp '2017-11-05 11:24:17');
insert into test values (1, timestamp '2017-12-15 13:42:05');
insert into test values (2, timestamp '2017-01-05 11:00:00');
insert into test values (2, timestamp '2017-06-05 23:12:56');
Result:
ID MX MN DIFF
---- ------------------- ------------------- ---------------------
1 2017-12-15 13:42:05 2017-09-12 01:00:00 94 days and 12:42:05
2 2017-06-05 23:12:56 2017-01-05 11:00:00 151 days and 12:12:56
Here is what gave me the desired result:
SELECT d.FK_SOCRD_ID,
d.SUBJECT_NUMBER,
c.PTLASTNAME || ', ' || c.PTFIRSTNAME AS "Patient name",
min(b.created) AS min_created,
max(b.created) AS max_created,
trunc(max(b.created)) - trunc(min(b.created)) || ' days' AS Diff
FROM CR_MDT a
FULL JOIN CR_MDT_VERIFY b
ON a.CR_MDT_ID = b.FK_CR_MDT_ID
LEFT JOIN patient c
ON c.SOCRD_ID = a.FK_SOCRD_ID
LEFT JOIN PT_STUDY d
ON d.FK_SOCRD_ID = c.SOCRD_ID
WHERE a.CR_MDT_DT BETWEEN TO_DATE('01/01/2017', 'mm/dd/yyyy') AND TO_DATE('12/31/2017', 'mm/dd/yyyy')
GROUP BY d.FK_SOCRD_ID,
d.SUBJECT_NUMBER,
c.PTLASTNAME,
c.PTFIRSTNAME
Related
I am a little bit confusing and have no idea how to solve this problem. I have column in table Quantity which store Time value.
I want to create a following story. If I have for example
Quantity
8:00
8:00
It needs to SUM() this two and in output I need to get 16 HOURS
Second think, it needs to take last two number :00 and add to HOURS.
This is what I do so far
SELECT
(SUM(SUBSTR(A.Quantity, ':', 1)) + TRUNC((SUM(SUBSTR(A.Quantity, ':', -1)) / 60),0)), ':' ,
MOD(SUM(SUBSTR(A.Quantity, ':' , -1)), 60)
AS TOTAL_SUM FROM (
SELECT
ata.ATAID AS AtaId, ata.ProjectID, ata.StartDate, ataAW.Quantity
FROM
ata
INNER JOIN
weekly_report
ON
weekly_report.ataId = ata.ATAID
INNER JOIN
ata_articles ataAW
ON
ataAW.wrId = weekly_report.id
WHERE
ata.ATAID = 10987
AND
ataAW.type = 1
OR
ataAW.type = 2
OR
ataAW.type = 3
AND
(weekly_report.status != 3 AND weekly_report.status != 4)
AND
(
weekly_report.year < (SELECT year FROM weekly_report WHERE id = 89)
OR
(
weekly_report.year <= (SELECT year FROM weekly_report WHERE id = 89)
AND
weekly_report.week <= (SELECT week FROM weekly_report WHERE id = 89)
)
)
) A
group by A.AtaId
So far better explanation, when I run first part of query I need to get something like
SELECT
CONCAT(
-- extract hours froAm time and add minutes converted to hours
(SUM(SUBSTRING_INDEX(aa.Quantity, ':', 1)) + TRUNCATE((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) / 60),0))
-- , ':',
-- extract minutes from time and find reminder (modulo)*/
-- LPAD((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) % 60), 2, 0)
) AS W_TOTAL_SUM
FROM
ata_articles aa
INNER JOIN
weekly_report wr
ON
aa.wrId = wr.id
WHERE
aa.wrId = 69
AND
aa.type = 1
TOTAL_SUM
16
And when I run second part
SELECT
CONCAT(
-- extract hours froAm time and add minutes converted to hours
-- (SUM(SUBSTRING_INDEX(aa.Quantity, ':', 1)) + TRUNCATE((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) / 60),0))
-- , ':',
-- extract minutes from time and find reminder (modulo)*/
LPAD((SUM(SUBSTRING_INDEX(aa.Quantity, ':', -1)) % 60), 2, 0)
) AS W_TOTAL_SUM
FROM
ata_articles aa
INNER JOIN
weekly_report wr
ON
aa.wrId = wr.id
WHERE
aa.wrId = 69
AND
aa.type = 1
I get output
TOTAL_SUM
00
Can someone guide me and tell me how to solve this issue since I try every solution but unfortunetlly doesn't work. And here is what I try so far, but I always get message
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number
SELECT
(SUM(SUBSTR(A.Quantity, ':', 1)) + TRUNC((SUM(SUBSTR(A.Quantity, ':', -1)) / 60),0)), ':' ,
MOD(SUM(SUBSTR(A.Quantity, ':' , -1)), 60)
AS TOTAL_SUM FROM (
SELECT
ata.ATAID AS AtaId, ata.ProjectID, ata.StartDate, ataAW.Quantity
FROM
ata
INNER JOIN
weekly_report
ON
weekly_report.ataId = ata.ATAID
INNER JOIN
ata_articles ataAW
ON
ataAW.wrId = weekly_report.id
WHERE
ata.ATAID = 10987
AND
ataAW.type = 1
OR
ataAW.type = 2
OR
ataAW.type = 3
AND
(weekly_report.status != 3 AND weekly_report.status != 4)
AND
(
weekly_report.year < (SELECT year FROM weekly_report WHERE id = 89)
OR
(
weekly_report.year <= (SELECT year FROM weekly_report WHERE id = 89)
AND
weekly_report.week <= (SELECT week FROM weekly_report WHERE id = 89)
)
)
) A
group by A.AtaId
UPDATE
I get output error message
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 267 Column: 19
SELECT ( EXTRACT( DAY FROM duration ) * 24 + EXTRACT( HOUR FROM duration ) )
|| ':'
|| TO_CHAR( EXTRACT( MINUTE FROM DURATION ), 'FM00' )
|| ' HOURS' AS duration
FROM (
SELECT NUMTODSINTERVAL(SUM( SUBSTR( quantity, 1, INSTR( quantity, ':' ) - 1 ) ),'HOUR')
+ NUMTODSINTERVAL(SUM( SUBSTR( quantity, INSTR( quantity, ':' ) + 1 ) ), 'MINUTE' )
AS duration
FROM (
SELECT ata.ATAID AS AtaId, ata.ProjectID, ata.StartDate, ataAW.Quantity
FROM ata
INNER JOIN weekly_report
ON weekly_report.ataId = ata.ATAID
INNER JOIN ata_articles ataAW
ON ataAW.wrId = weekly_report.id
INNER JOIN (SELECT week, year FROM weekly_report WHERE id = 89 ) b
ON ( weekly_report.year < b.year OR ( weekly_report.year = b.year AND weekly_report.week <= b.week ))
WHERE ata.ATAID = 10987
AND ataAW.type IN ( 1, 2, 3 )
AND weekly_report.status NOT IN ( 3, 4 )
))
group by A.AtaId
Here is what I get as output when I execute following code
DURATION
:HOURS
If you have the (slightly more complicated) sample data:
CREATE TABLE table_name ( Quantity ) AS
SELECT '8:00' FROM DUAL UNION ALL
SELECT '7:30' FROM DUAL UNION ALL
SELECT '0:30' FROM DUAL;
Then you can use string functions to get the hour and minute parts and sum those and then convert the totals to an interval (so you don't end up with 15:60 HOURS) and then format the output:
SELECT ( EXTRACT( DAY FROM duration ) * 24
+ EXTRACT( HOUR FROM duration )
)
|| ':'
|| TO_CHAR( EXTRACT( MINUTE FROM DURATION ), 'FM00' )
|| ' HOURS' AS duration
FROM (
SELECT NUMTODSINTERVAL(
SUM( SUBSTR( quantity, 1, INSTR( quantity, ':' ) - 1 ) ),
'HOUR'
)
+
NUMTODSINTERVAL(
SUM( SUBSTR( quantity, INSTR( quantity, ':' ) + 1 ) ),
'MINUTE'
) AS duration
FROM table_name
);
Which outputs:
| DURATION |
| :---------- |
| 16:00 HOURS |
db<>fiddle here
I Have Query
SELECT * FROM PPI_CD WHERE PATIENT_NO = 14683
Results from this query display some of the same data based on PATIENT_NO
How to order only the latest data out? only One
Full Query
SELECT
PPI_ID,
co.order_no,
p.RM_NO,
co.patient_name,
EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM p.DOB) as Age,
p.sex,
co.order_date as TGL_MASUK,
NVL(il.NAME, '0') as ICD,
NVL(ppi.HPA_UC, '0') AS UC,
NVL(ppi.HPA_IVL, '0') AS IVL,
NVL(ppi.HPA_CVL, '0') AS CVL,
NVL(ppi.HPA_ETT, '0') AS ETT,
NVL(ppi.IRS_VAP, '0') AS VAP,
NVL(ppi.IRS_PLEB, '0') AS PLEB,
NVL(ppi.IRS_ISK, '0') AS ISK,
NVL(ppi.IRS_IAD, '0') AS IAD,
NVL(ppi.TB, '0') AS TB,
NVL(ppi.HK, '0') AS HK,
NVL(ppi.AB, '0') AS AB,
NVL(ppi.ANTIBIOTIK , '0') AS ANTIBIOTIK,
NVL(ppi.DEKU, '0') AS DEKU,
NVL(ppi.JK_DARAH , '0') AS DARAH,
NVL(ppi.JK_SWAB , '0') AS SWAB,
NVL(ppi.JK_SPUTUM , '0') AS SPUTUM,
NVL(ppi.JK_URINE , '0') AS URINE,
NVL(ppi.TEMP, 'N/A') AS TEMP
FROM case_orders co
LEFT JOIN PPI_CD cd
ON cd.patient_no = co.patient_no
LEFT JOIN illness_lists il
ON il.illness_no = cd.illness_no
LEFT JOIN patients p
ON p.contact_no = co.patient_no
LEFT JOIN PPI ppi
ON ppi.RM_NO = p.RM_NO
WHERE CO.status_no=5
ORDER BY CO.ORDER_DATE ASC;
the problem is with this
out 2 data which should be only one data
For just one patient, you can order by and a row limiting clause:
select *
from ppi_cd
where patient_no = 14683
order by posted_date desc
fetch first row only
If you want the latest record per patient_no, you can do:
select c.*
from ppi_cd c
where c.posted_date = (
select max(c1.posted_date) from ppi_cd c1 where c1.patient_no = c.patient_no
)
Use row_number() :
select pc.*
from (select pc.*, row_number() over (partition by PATIENT_NO order by pc.post_date DESC) as seq
from PPI_CD pc
) pc
where pc.seq = 1;
I am trying to add two tables and get the total days from two dates. But having the following simple issue: not a single-group group function.
This is what I've tried so far:
SELECT COUNT(status) AS "Present Days",
(SELECT TRUNC(TO_DATE('01/10/2018', 'MM/DD/YYYY') - TO_DATE(k.JOINING_DATE, 'MM/DD/YYYY'))
FROM attendance m
INNER JOIN EMP_OFFICIAL k
ON k.EMPNO = m.EMPNO
WHERE m.empno='EMP00254'
AND m.status='P') AS "Total Days"
FROM attendance
WHERE empno = 'EMP00254'
AND status = 'P';
Can I get the days without using the DUAL?
First off, that subquery is most likely not scalar (it looks like you're assuming multiple rows can come from the attendance table) and second, it contains an unnecessary join..
What I would do instead is something like:
select count(*) as "Present Days",
to_date('01/10/2018', 'mm/dd/yyyy') - k.joining_date "Total Days"
from attendance a
inner join emp_official k on a.empno = k.empno
where a.empno = 'EMP00254'
and a.status = 'P'
group by to_date('01/10/2018', 'mm/dd/yyyy') - k.joining_date;
This does assume that the k.joining_date column is of DATE datatype and that 'k.empno' is a unique column.
If there is only one row in the EMP_OFFICIAL table for each employee then you could do:
SELECT COUNT(a.status) AS "Present Days",
TRUNC( SYSDATE ) - TRUNC( MIN( k.joining_date ) ) AS "Total Days"
FROM attendance a
RIGHT OUTER JOIN EMP_OFFICIAL k
ON ( k.EMPNO = a.EMPNO )
WHERE a.empno = 'EMP00254'
WHERE a.status = 'P';
or you could do the aggregation in a sub-query:
SELECT a."Present Days",
TRUNC( SYSDATE ) - TRUNC( k.joining_date ) AS "Total Days"
FROM (
SELECT EMPNO,
COUNT( status ) AS "Present Days"
FROM attendance
WHERE EMPNO = 'EMP00254'
AND status = 'P'
GROUP BY EMPNO
) a
RIGHT OUTER JOIN EMP_OFFICIAL k
ON ( k.EMPNO = a.EMPNO )
WHERE k.EMPNO = 'EMP00254';
Or you could use UNION ALL to query the two tables and have the result in two rows (rather than two columns):
SELECT 'Present Days' As type,
COUNT(status) AS Days
FROM attendance
WHERE empno = 'EMP00254'
WHERE status = 'P'
UNION ALL
SELECT 'Total Days',
TRUNC( SYSDATE ) - TRUNC( joining_date )
FROM EMP_OFFICIAL
WHERE empno = 'EMP00254';
Either remove COUNT(status) AS "Present Days" part and SELECT inside paranthesis :
SELECT (TRUNC(TO_DATE('01/10/2018', 'MM/DD/YYYY') - TO_DATE(k.JOINING_DATE, 'MM/DD/YYYY')) "time difference" FROM attendance m
INNER JOIN EMP_OFFICIAL k ON k.EMPNO = m.EMPNO where m.empno='EMP00254' and m.status='P') AS "Total Days"
FROM attendance where empno = 'EMP00254' and status = 'P';
OR
use the following (add GROUP BY expression at the end of sql):
SELECT COUNT(status) AS "Present Days",
(TRUNC(TO_DATE('01/10/2018', 'MM/DD/YYYY') - TO_DATE(k.JOINING_DATE, 'MM/DD/YYYY')) "time difference" FROM attendance m
INNER JOIN EMP_OFFICIAL k ON k.EMPNO = m.EMPNO where m.empno='EMP00254' and m.status='P') AS "Total Days"
FROM attendance where empno = 'EMP00254' and status = 'P'
GROUP BY (TRUNC(TO_DATE('01/10/2018', 'MM/DD/YYYY') - TO_DATE(k.JOINING_DATE, 'MM/DD/YYYY'));
Since , groupped and non-groupped items can not be used together.
By the way, you don't need to use dual in your inline select statement like ( SELECT TRUNC(TO_DATE('01/10/2018', 'MM/DD/YYYY') - TO_DATE(k.JOINING_DATE, 'MM/DD/YYYY') FROM DUAL ), just ( TRUNC(TO_DATE('01/10/2018', 'MM/DD/YYYY') - TO_DATE(k.JOINING_DATE, 'MM/DD/YYYY')) is enough.
Problem: I have dated salary information stored in a table. I need to display one result per year. For each year, I want to display the max dated record from the year prior. The problem is that some years don’t have data (salary didn’t change). I need these rows to contain the max record prior to that year (it may be from 2 years before or even 3).
My query right now would work if each row has data… but it doesn’t account for years where there is no data. How can I update this sql to pull the desired results below:
Example of data:
sch_sal_svc.beg_date -------sch_sal_svc.beg_date.per_plan_data
1/1/2007---100
6/1/2007---200
1/1/2008---300
1/1/2011---400
8/1/2011---500
9/1/2012---600
Current Results
1/1/2008---200
1/1/2011---300
1/1/2012---500
Desired Results
1/1/2008---200
1/1/2009---300
1/1/2010---300
1/1/2011---300
1/1/2012---500
SQL:
SELECT
years.control_id,
years.ssn,
ebe.plan_id,
to_number(to_char(years.sal_date,'yyyy')),
null as per_plan_salary,
null as per_vest_hours,
null as per_credsvc_hours,
LEAST(s.rate_1,cl.comp_genl),
null as salary_1,
null as per_comm,
null as per_overtime,
null as per_ncr,
null as salary_2
FROM
sch_sal_svc s
, (select distinct ssn, control_id, TRUNC(beg_date,'YEAR') as sal_date from sch_sal_svc where beg_date > to_date('12/31/1900', 'mm/dd/yyyy')) years
, employee_benefit_elig ebe, compliance_limits cl
WHERE
years.ssn = ebe.ssn
and years.control_id = ebe.control_id
and to_number(to_char(years.sal_date,'yyyy')) = cl.limit_year
and to_number(to_char(years.sal_date,'yyyy')) <= to_number(to_char(sysdate,'yyyy'))
and s.beg_date = (
select max(s2.beg_date) from sch_sal_svc s2
where s2.ssn = years.ssn and s2.control_id = years.control_id
and s2.beg_date <= years.sal_date
)
and s.ssn = years.ssn
and s.control_id = years.control_id
and ebe.benefit_id = 'DB'
and ebe.control_id = 'CLIENT'
and ebe.plan_id in ('100', '200')
CREATE TABLE sch_sal_svc
(
beg_date DATE
, per_plan_data NUMBER
);
INSERT INTO sch_sal_svc VALUES(TO_DATE('01/01/2007', 'DD/MM/YYYY'), 100);
INSERT INTO sch_sal_svc VALUES(TO_DATE('06/01/2007', 'DD/MM/YYYY'), 200);
INSERT INTO sch_sal_svc VALUES(TO_DATE('01/01/2008', 'DD/MM/YYYY'), 300);
INSERT INTO sch_sal_svc VALUES(TO_DATE('01/01/2011', 'DD/MM/YYYY'), 400);
INSERT INTO sch_sal_svc VALUES(TO_DATE('08/01/2011', 'DD/MM/YYYY'), 500);
INSERT INTO sch_sal_svc VALUES(TO_DATE('09/01/2012', 'DD/MM/YYYY'), 600);
SELECT MIN(beg_date) FROM sch_sal_svc;
-- 2007-01-01 00:00:00
SELECT d.r_level + NUMTOYMINTERVAL(1, 'YEAR') AS d_date
, NVL -- the salary must be updated at least once in three years
(
NVL
(
NVL
(
s.per_plan_data
, LAG(s.per_plan_data, 1) OVER (PARTITION BY 1 ORDER BY d.r_level)
)
, LAG(s.per_plan_data, 2) OVER (PARTITION BY 1 ORDER BY d.r_level)
)
, LAG(s.per_plan_data, 3) OVER (PARTITION BY 1 ORDER BY d.r_level)
) AS lag_per_plan_data
FROM
(
SELECT DATE'2006-01-01' + NUMTOYMINTERVAL(LEVEL, 'YEAR') AS r_level -- min beg_date minus 1
FROM DUAL
CONNECT BY
LEVEL < (SELECT TO_CHAR(MAX(beg_date), 'YYYY') - TO_CHAR(MIN(beg_date), 'YYYY') + 2 FROM sch_sal_svc)
) d
LEFT JOIN
(
SELECT beg_date
, per_plan_data
FROM sch_sal_svc
WHERE (beg_date) IN
(
SELECT MAX(beg_date)
FROM sch_sal_svc
GROUP BY
TRUNC(beg_date, 'YYYY')
)
) s
ON d.r_level = TRUNC(s.beg_date, 'YYYY')
;
/*
2008-01-01 00:00:00 200
2009-01-01 00:00:00 300
2010-01-01 00:00:00 300
2011-01-01 00:00:00 300
2012-01-01 00:00:00 500
2013-01-01 00:00:00 600
*/
I have problem with following query where in which the nested query should be
converted to normal query:
select
count(*) as count,
TO_CHAR(RH.updated_datetime,'DD-MM-YYYY HH:MI:SS') as date,
SUM(
extract (
epoch from (
RH.updated_datetime - PRI.procedure_performed_datetime
)
)/60
)::integer/count(*) as diff
from
procedure_runtime_information PRI,
study S,
report R,
report_history RH
where
RH.report_fk = R.pk AND
R.study_fk = S.pk AND
S.procedure_runtime_fk = PRI.pk AND
RH.old_status_fk = 21 AND
RH.revision = (select max(revision) from report_history where RH.report_fk = RH.report_fk) AND
RH.updated_datetime > TO_DATE('22-01-2013 00:00:00', 'DD-MM-YYYY HH24:MI:SS') AND RH.updated_datetime < TO_DATE('22-01-2014 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
group by date order by date asc;
Assuming this
(select max(revision) from report_history where RH.report_fk = RH.report_fk)
should really be:
(select max(revision) from report_history x where x.report_fk = RH.report_fk)
You could transform the nested (correlated) subquery into a plain subquery like this (one way of many):
SELECT count(*) AS ct
,to_char(rh.updated_datetime,'DD-MM-YYYY HH:MI:SS') AS date -- HH24?
,sum(extract(epoch FROM (RH.updated_datetime
- PRI.procedure_performed_datetime))
/ 60)::int / count(*) AS diff
FROM procedure_runtime_information PRI
JOIN study S ON S.procedure_runtime_fk = PRI.pk
JOIN report R ON R.study_fk = S.pk
JOIN report_history RH ON RH.report_fk = R.pk
JOIN (
SELECT report_fk, max(revision) AS revision
FROM report_history RH1
GROUP BY 1
) RH1 ON RH1.report_fk = RH.report_fk
AND RH1.revision = RH.revision
WHERE RH.old_status_fk = 21
AND RH.updated_datetime > to_date('22-01-2013', 'DD-MM-YYYY') -- >= ?
AND RH.updated_datetime < to_date('22-01-2014', 'DD-MM-YYYY') -- to_timestamp?
GROUP BY date -- where does date come from?
ORDER BY date;