修改SQL查询

时间:2014-12-17 08:05:09

标签: sql oracle

以前我想找

1-批次ID 2-上传时间 3-每批次面前的请求数

我写了以下查询,它完美地运作

SELECT REQUEST_BATCH_ID,To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),COUNT(*)
FROM bpdata.bulk_prov_detail B  
WHERE B.REQUEST_BATCH_ID
IN
(
SELECT REQUEST_BATCH_ID 
FROM bpdata.bulk_prov_master A
where A.START_TIME > to_DATE ('12/16/2014 00', 'MM/DD/YYYY HH24') 
)
GROUP BY To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),REQUEST_BATCH_ID
order by 1 desc,2 desc

我以下列格式获取数据

Batch_ID    Day/Date    Count(*)
257658  17/12/2014 11   5
257658  17/12/2014 12   4
257657  17/12/2014 12   4
257656  17/12/2014 12   2

但现在我想计算总计数()和最大计数(),我无法为此设计查询。在这方面,您的帮助和指导将受到赞赏

编辑: 所需输出的示例

Batch_ID    Day/Date    Count(*)  
257658  17/12/2014 11   5
257658  17/12/2014 12   4
257657  17/12/2014 12   4
257656  17/12/2014 12   2


Sum(Count(*))    Largest_request     Time
   15                   5              17/12/2014 11

Sum(Count(*))和Largest_request和time是我现在需要的

2 个答案:

答案 0 :(得分:1)

我还没有经过测试,但它应该是这样的:

SELECT SUM(x.total) AS grandtotal
FROM (
SELECT REQUEST_BATCH_ID,To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),COUNT(*) AS total
FROM bpdata.bulk_prov_detail B  
WHERE B.REQUEST_BATCH_ID
IN
(
SELECT REQUEST_BATCH_ID 
FROM bpdata.bulk_prov_master A
where A.START_TIME > to_DATE ('12/16/2014 00', 'MM/DD/YYYY HH24') 
)
GROUP BY To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),REQUEST_BATCH_ID
) x

FOR Max Count:

SELECT *
FROM (
SELECT REQUEST_BATCH_ID,To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),COUNT(*) AS total
FROM bpdata.bulk_prov_detail B  
WHERE B.REQUEST_BATCH_ID
IN
(
SELECT REQUEST_BATCH_ID 
FROM bpdata.bulk_prov_master A
where A.START_TIME > to_DATE ('12/16/2014 00', 'MM/DD/YYYY HH24') 
)
GROUP BY To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),REQUEST_BATCH_ID
) x ORDER BY x.total DESC LIMIT 0,1

要准确了解您的要求,请加入2个查询。 (也未经测试)

SELECT
(
SELECT SUM(x.total) AS grandtotal
FROM (
SELECT REQUEST_BATCH_ID,To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),COUNT(*) AS total
FROM bpdata.bulk_prov_detail B  
WHERE B.REQUEST_BATCH_ID
IN
(
SELECT REQUEST_BATCH_ID 
FROM bpdata.bulk_prov_master A
where A.START_TIME > to_DATE ('12/16/2014 00', 'MM/DD/YYYY HH24') 
)
GROUP BY To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),REQUEST_BATCH_ID
) x
) y,
(
SELECT *
FROM (
SELECT To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24') AS daydate,COUNT(*) AS total
FROM bpdata.bulk_prov_detail B  
WHERE B.REQUEST_BATCH_ID
IN
(
SELECT REQUEST_BATCH_ID 
FROM bpdata.bulk_prov_master A
where A.START_TIME > to_DATE ('12/16/2014 00', 'MM/DD/YYYY HH24') 
)
GROUP BY To_CHAR(B.UPDATE_STAMP , 'DD/MM/YYYY HH24'),REQUEST_BATCH_ID
) x ORDER BY x.total DESC LIMIT 0,1
) z

答案 1 :(得分:0)

您可以使用Subquery Factoring WITH Clause来实现相同的

with batch_query as 
(
SELECT REQUEST_BATCH_ID,
               To_CHAR(B.UPDATE_STAMP, 'DD/MM/YYYY HH24') upd_stmp,
               COUNT(*) cnt
          FROM bpdata.bulk_prov_detail B
         WHERE B.REQUEST_BATCH_ID IN
               (SELECT REQUEST_BATCH_ID
                  FROM bpdata.bulk_prov_master A
                 where A.START_TIME >
                       to_DATE('12/16/2014 00', 'MM/DD/YYYY HH24'))
         GROUP BY To_CHAR(B.UPDATE_STAMP, 'DD/MM/YYYY HH24'),
                  REQUEST_BATCH_ID)
,batch_agg_query as
(SELECT max(cnt) max_cnt,sum(cnt) sum_cnt from batch_query )
 select sum_cnt,max_cnt,batch_query.upd_stmp
  FROM batch_query,batch_agg_query 
  where batch_query.cnt=batch_agg_query.max_cnt