Oracle
 sql >> डेटाबेस >  >> RDS >> Oracle

Oracle SQL समूह द्वारा if

SQL Fiddle

Oracle 11g R2 स्कीमा सेटअप :

CREATE TABLE FILE_USAGE_LOG (TESTID, SITE, LATEST_READ, READ_COUNT, FILE_ORIGIN_ID ) AS
          SELECT 'File1', 'Site1', DATE '2013-05-02', 2, 1 FROM DUAL
UNION ALL SELECT 'File1', 'Site2', DATE '2014-01-22', 3, 2 FROM DUAL
UNION ALL SELECT 'File2', 'Site1', DATE '2014-06-02', 8, 0 FROM DUAL
UNION ALL SELECT 'File3', 'Site1', DATE '2014-09-19', 17, 0 FROM DUAL
UNION ALL SELECT 'File4', 'Site2', DATE '2014-09-19', 14, 2 FROM DUAL
UNION ALL SELECT 'File4', 'Site2', DATE '2014-09-19', 34, 1 FROM DUAL
UNION ALL SELECT 'File4', 'Site3', DATE '2014-09-19', 10, 0 FROM DUAL
UNION ALL SELECT 'File5', 'Site2', DATE '2014-09-19', 44, 2 FROM DUAL
UNION ALL SELECT 'File5', 'Site3', DATE '2014-09-19', 1, 2 FROM DUAL;

क्वेरी 1 :

SELECT  TESTID,
        REGEXP_REPLACE( 
          LISTAGG( SITE, ', ' )
            WITHIN GROUP( ORDER BY SITE ),
          '([^, ]+)(, \1)+($|, )',
          '\1\3'
        ) AS SITES, 
        MAX( LATEST_READ ) AS LATEST_READ,
        SUM(READ_COUNT) AS Total_Read_Count
FROM    FILE_USAGE_LOG 
GROUP BY
        TESTID
HAVING  COUNT( CASE FILE_ORIGIN_ID WHEN 2 THEN NULL ELSE 1 END ) > 0
UNION ALL
SELECT  TESTID,
        SITE,
        LATEST_READ,
        READ_COUNT
FROM    FILE_USAGE_LOG l
WHERE   FILE_ORIGIN_ID = 2
AND     NOT EXISTS ( SELECT 'X'
                     FROM   FILE_USAGE_LOG x
                     WHERE  x.TESTID      = l.TESTID
                     AND    x.FILE_ORIGIN_ID <> 2
                   )
ORDER BY 1,2

परिणाम :

| TESTID |        SITES |                 LATEST_READ | TOTAL_READ_COUNT |
|--------|--------------|-----------------------------|------------------|
|  File1 | Site1, Site2 |   January, 22 2014 00:00:00 |                5 |
|  File2 |        Site1 |      June, 02 2014 00:00:00 |                8 |
|  File3 |        Site1 | September, 19 2014 00:00:00 |               17 |
|  File4 | Site2, Site3 | September, 19 2014 00:00:00 |               58 |
|  File5 |        Site2 | September, 19 2014 00:00:00 |               44 |
|  File5 |        Site3 | September, 19 2014 00:00:00 |                1 |

प्रश्न 2 :

SELECT  TESTID,
        REGEXP_REPLACE( 
          LISTAGG( SITE, ', ' )
            WITHIN GROUP( ORDER BY SITE ),
          '([^, ]+)(, \1)+($|, )',
          '\1\3'
        ) AS SITES, 
        MAX( LATEST_READ ) AS LATEST_READ,
        SUM(READ_COUNT) AS Total_Read_Count
FROM    FILE_USAGE_LOG 
WHERE   TESTID NOT LIKE 'this%'
AND     LATEST_READ BETWEEN DATE '2014-01-01' AND DATE '2014-12-31'
GROUP BY
        TESTID
HAVING  COUNT( CASE FILE_ORIGIN_ID WHEN 2 THEN NULL ELSE 1 END ) > 0
UNION ALL
SELECT  TESTID,
        SITE,
        LATEST_READ,
        READ_COUNT
FROM    FILE_USAGE_LOG l
WHERE   FILE_ORIGIN_ID = 2
AND     NOT EXISTS ( SELECT 'X'
                     FROM   FILE_USAGE_LOG x
                     WHERE  x.TESTID      = l.TESTID
                     AND    x.FILE_ORIGIN_ID <> 2
                     AND    TESTID NOT LIKE 'this%'
                     AND    LATEST_READ BETWEEN DATE '2014-01-01' AND DATE '2014-12-31'
                   )
AND     TESTID NOT LIKE 'this%'
AND     LATEST_READ BETWEEN DATE '2014-01-01' AND DATE '2014-12-31'
ORDER BY 1,2

परिणाम :

| TESTID |        SITES |                 LATEST_READ | TOTAL_READ_COUNT |
|--------|--------------|-----------------------------|------------------|
|  File1 |        Site2 |   January, 22 2014 00:00:00 |                3 |
|  File2 |        Site1 |      June, 02 2014 00:00:00 |                8 |
|  File3 |        Site1 | September, 19 2014 00:00:00 |               17 |
|  File4 | Site2, Site3 | September, 19 2014 00:00:00 |               58 |
|  File5 |        Site2 | September, 19 2014 00:00:00 |               44 |
|  File5 |        Site3 | September, 19 2014 00:00:00 |                1 |


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. मुझे कैसे याद होगा कि CONNECT BY प्रश्नों में PRIOR को किस दौर में जाना चाहिए

  2. SUM (TO_NUMBER (varchar2 फ़ील्ड)) नहीं कर सकता:ORA 01722 [ORACLE]

  3. Oracle SQL डेवलपर में डेटाबेस का नाम कैसे क्वेरी करें?

  4. रनटाइम पर कोई ऑनमैसेज इवेंट नहीं (ओरेकल क्यू पर जावा जेएमएस मैसेज लिस्टनर)

  5. SQL रिपोर्टिंग के साथ Oracle समस्या