我正在使用的查询是
select count(*) as count,
TO_CHAR(firstTable1.firstTable_date, 'DD-MON-YY') as dateAndTime
from (
select *
from firstTable
where firstTable_status = 'Open'
and firstTable_date > sysdate-10
) firstTable1
group by TO_CHAR(firstTable1.firstTable_date, 'DD-MON-YY')
这将使我在11-may-17上获得第1名
并为09-may-17计数1
样本表数据
firstTable
id firstTable_status firstTable_date
A-123 Open 11-may-17
B-123 Open 09-may-17
secondTable
secondTable_Id firstTable_id secondTable_Action secondTable_date
XX1 A-123 started 11-may-17
XX2 A-123 Assigned 11-may-17
XX3 A-123 action2 11-may-17
XX4 A-123 action3 11-may-17
XX5 A-123 action4 11-may-17
XX6 A-123 action5 11-may-17
XX7 A-123 closed 11-may-17
MN1 B-123 started 09-may-17
MN1 B-123 action1 10-may-17
MN1 B-123 closed 11-may-17
是否可以修改此查询,例如
如果secondTable_Action在同一日期关闭同一个id,那么查询不应返回该id的计数
例如,firstTable中的id A-123在11-may-17上打开并在相同时关闭 在第二个表中为此查询输入的日期应返回计数0表示11- 可-17 而对于身份B-123在不同的日子关闭,所以它应该返回 计算在09-may-17
答案 0 :(得分:0)
SELECT dt,
SUM( open_closed ) AS "count"
FROM (
SELECT f.id,
TRUNC( f.firstTable_date ) as dt,
COUNT( CASE s.secondtable_action WHEN 'started' THEN 1 END )
- COUNT( CASE s.secondtable_action WHEN 'closed' THEN 1 END ) AS open_closed
FROM firstTable f
INNER JOIN secondtable s
ON ( f.id = s.firsttable_id )
WHERE firstTable_date > SYSDATE - INTERVAL '10' DAY
GROUP BY f.id,
TRUNC( f.firstTable_date )
HAVING COUNT( CASE s.secondtable_action WHEN 'started' THEN 1 END ) > 0
)
GROUP BY dt
答案 1 :(得分:0)
您可以使用not exists
或类似内容删除关闭:
select count(*) as count,
to_char(t.firstTable_date, 'DD-MON-YY') as dateAndTime
from firstTable t
where t.firstTable_status = 'Open' and
t.firstTable_date > sysdate - 10 and
not exists (select 1
from secondTable t2
where t2.id = t.id and
t2.firstTable_date = t.firstTable_date and
t2.firstTable_status = 'Closed'
)
group by to_char(t.firstTable_date, 'DD-MON-YY')
order by min(firstTable_date);
注意:
firstTable_date
有时间组件,则相关子句应考虑到这一点:trunc(t2.firstTable_date) = trunc(t.firstTable_date)
。trunc()
代替to_char()
。如果您打算使用to_char()
,我建议您使用YYYY-MM-DD格式。这是ISO标准,并且可以进行各种操作。order by
,因此日期将按顺序排列。