在选择语句中选择一个日期之间的总和

时间:2019-03-11 13:37:42

标签: database oracle

我有一个查询,可以选择每天的数据计数,我想修改查询,以便它可以从两个日期之间的日期获取数据

第一个查询如下:

SELECT ROW_NUMBER() OVER (ORDER BY q.english_Name DESC) as id,
COUNT(t.id) AS ticket,
  q.english_name queue_name,
  ts.code current_status,
  COUNT(t.assigned_to)               AS assigned,
  (COUNT(t.id)-COUNT(t.assigned_to)) AS not_assigned
 ,trunc(t.create_date) create_Date
FROM ticket t
INNER JOIN ref_queue q
ON (q.id = t.queue_id)
INNER JOIN ref_ticket_status ts
ON(ts.id=t.current_status_id)     
GROUP BY q.english_name,
  ts.code
 ,trunc(t.create_date)  

但是当我将其修改为:

SELECT ROW_NUMBER() OVER (ORDER BY q.english_Name DESC) as id,
COUNT(t.id) AS ticket,
  q.english_name queue_name,
  ts.code current_status,
  COUNT(t.assigned_to)               AS assigned,
  (COUNT(t.id)-COUNT(t.assigned_to)) AS not_assigned
 ,trunc(t.create_date) create_Date
FROM ticket t
INNER JOIN ref_queue q
ON (q.id = t.queue_id)
INNER JOIN ref_ticket_status ts
ON(ts.id=t.current_status_id) 
where t.create_date between '18-FEB-19' and '24-FEB-19'

GROUP BY q.english_name,
  ts.code
 ,trunc(t.create_date)

输出为

1   1   Technical Support Sec.  CLOSED  0   1   19-FEB-19
2   6   Technical Support Sec.  OPEN    4   2   18-FEB-19
3   1   Technical Support Sec.  OPEN    0   1   21-FEB-19
4   3   Network Sec.            OPEN    2   1   18-FEB-19
5   1   Network Sec.            OPEN    0   1   21-FEB-19

我怎样才能获得这些天的总产量,使得产量为:

1 7   Technical Support Sec.  OPEN    4   3   
2 4   Network Sec.            OPEN    2   2

1 个答案:

答案 0 :(得分:0)

GROUP BY中进行查询时,结果集将为GROUP BY列表中每组不同的值包含一行。例如,之所以在“技术支持秒”的OPEN记录中获得两行,是因为TRUNC(t.create_date)有两个不同的值,导致两组,因此结果集中有两行。

为避免这种情况,请停止按TRUNC(t.create_date)分组。

SELECT ROW_NUMBER() OVER (ORDER BY q.english_Name DESC) as id,
COUNT(t.id) AS ticket,
  q.english_name queue_name,
  ts.code current_status,
  COUNT(t.assigned_to)               AS assigned,
  (COUNT(t.id)-COUNT(t.assigned_to)) AS not_assigned
--     ,trunc(t.create_date) create_Date
FROM ticket t
INNER JOIN ref_queue q
ON (q.id = t.queue_id)
INNER JOIN ref_ticket_status ts
ON(ts.id=t.current_status_id) 
where t.create_date between '18-FEB-19' and '24-FEB-19'
GROUP BY q.english_name,
  ts.code
--     ,trunc(t.create_date)