计算SQL中返回的总行数

时间:2013-03-05 01:04:01

标签: oracle

这可能听起来有些小便,但我试图计算查询中返回行的总数。我试过COUNT()无济于事。到目前为止,我有这个查询:

SELECT DISTINCT
    a.*  
    , LEVEL AS n
    , count(wipdatavalue) as total
FROM
(
  SELECT
    WIPDATAVALUE
    , CONTAINERNAME
    , una
    , dulo
    , (dulo - una) + 1 AS m
  FROM
  (
    SELECT 
      WIPDATAVALUE
      , CONTAINERNAME
      , CASE
          WHEN INSTR(wipdatavalue, '-') = 0 THEN
            wipdatavalue
          ELSE
            SUBSTR(wipdatavalue, 1, INSTR(wipdatavalue, '-')-1) 
         END AS una
      , CASE
          WHEN INSTR(wipdatavalue, '-') = 0 THEN
            wipdatavalue
          ELSE
            SUBSTR(wipdatavalue, INSTR(wipdatavalue, '-') + 1)
         END AS dulo
    FROM trprinting
    WHERE containername = '8PB842613'
  )
)A CONNECT BY LEVEL <= m
GROUP BY
  WIPDATAVALUE
    , CONTAINERNAME
    , una
    , dulo
    , m
    , LEVEL
ORDER BY N;

enter image description here

我也试图将N字段设置为1到8,因为它应该是正确的输出。提前致谢。我知道你们会有所帮助:)

1 个答案:

答案 0 :(得分:0)

你想做的事情如下:

with data (wipdatavalue, containername, una, dulo, m, n)
     as (select wipdatavalue,containername,una,dulo,( dulo - una ) + 1 as m,una n
           from (select wipdatavalue,containername,
                        case
                          when Instr(wipdatavalue, '-') = 0
                          then
                            to_number(wipdatavalue)
                          else
                            to_number(Substr(wipdatavalue, 1, Instr(wipdatavalue, '-') - 1))
                          end as una,
                          case
                            when Instr(wipdatavalue, '-') = 0 
                            then
                              to_number(wipdatavalue)
                            else
                              to_number(Substr(wipdatavalue, Instr(wipdatavalue, '-') + 1))
                          end as dulo
                   from trprinting
                  where containername = '8PB842613')
         union all
         select wipdatavalue,containername,una,dulo,m,n + 1
           from data
          where n + 1 <= dulo)
select wipdatavalue,containername,una,dulo,m,n,
       count(*) over () total_rows
  from data
 order by wipdatavalue,n; 

小提琴:http://sqlfiddle.com/#!4/11082/2