简体   繁体   中英

Oracle SQL regexp sum within repeating sequence grouped by

First time here, hope someone can help

What's the Oracle SQL select stmt for the XML below stored in an Oracle CLOB field so the following is returned (there will be as many as 96 intvColl blocks per day).

Basically the numeric values in the intvColl blocks between the 2nd and 3rd commas need to be summed and grouped by the date before the first comma and also by the varchar after the 3rd comma.

I'm guessing regexp_substr / but can't quite get there. The first record is the sum of the 1st and 2nd intvColl blocks The second record is the sum of the 3rd intvColl block The third record is the sum of the 4th and 5th

MeterChannelID           Date         Sum     Quality   Count_of_records
6103044759-40011200-Q1   14/03/2016   1,387   A         2   
6103044759-40011200-Q1   14/03/2016     694   S         1 
6103044759-40011200-Q1   15/03/2016   1,433   A         2  

<uploadRegData>
    <intervalDataBlock>
        <setDateTime>16/03/2016-19:30:01</setDateTime>
        <intervalMinute>15</intervalMinute>
        <meterChannelID>6103044759-40011200-Q1</meterChannelID>
        <intvColl><intvData>14/03/2016,1,700,A</intvData></intvColl>
        <intvColl><intvData>14/03/2016,2,687,A</intvData></intvColl>
        <intvColl><intvData>14/03/2016,3,694,S</intvData></intvColl>
        <intvColl><intvData>15/03/2016,4,724,A</intvData></intvColl>
        <intvColl><intvData>15/03/2016,5,709,A</intvData></intvColl>
    </intervalDataBlock>
</uploadRegData>
SELECT MeterChannelID,
       "Date",
       SUM( value ) AS "Sum",
       Quality,
       COUNT(1) AS Count_of_Records
FROM   (
  SELECT MeterChannelID,
         TO_DATE( SUBSTR( data, 1, 10 ), 'DD/MM/YYYY' ) AS "Date",
         TO_NUMBER( SUBSTR(
                      data,
                      INSTR( data, ',', 1, 2 ),
                      LENGTH( data ) - INSTR( data, ',', 1, 2 ) - 2
                  ) ) AS value,
         SUBSTR( data, -1 ) AS Quality
  FROM   (
    SELECT EXTRACTVALUE( xml, '/uploadRegData/intervalDataBlock/meterChannelId' )
             AS MeterChannelID,
           EXTRACTVALUE( d.COLUMN_VALUE, '/intvData' ) AS data
    FROM   ( SELECT XMLType( column_name ) AS xml FROM table_name ) x,
           TABLE(
             XMLSequence(
               EXTRACT(
                 x.xml,
                 '/uploadRegData/intervalDataBlock/intvCol1/intvData'
               )
             )
           ) d
  )
)
GROUP BY MeterChannelID, "Date", Quality
ORDER BY MeterChannelID, "Date", Quality;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM