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

Oracle SQL PIVOT तालिका

आप ऐसा कुछ कर सकते हैं:

एसक्यूएल फिडल

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

CREATE TABLE tbl ( "mod", "floor", "Remaining Counts", CountType ) AS
          SELECT 'dz-P-1A', 1,    37, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-1D', 1,   321, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-1T', 1,    16, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-2A', 2,    25, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-R-1T', 1,  3318, 'CycleCount' FROM DUAL
UNION ALL SELECT 'dz-P-1A', 1,  6351, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-P-1D', 1,   121, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-P-2A', 2, 12638, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-R-1F', 1,    68, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-R-1O', 1,    47, 'SimpleBinCount' FROM DUAL
UNION ALL SELECT 'dz-R-1T', 1,  2051, 'SimpleBinCount' FROM DUAL;

क्वेरी 1 :

SUM( CASE ... ) . का उपयोग करके PIVOT करें बयान:

SELECT CountType,
       SUM( CASE "mod" WHEN 'dz-P-1A' THEN "Remaining Counts" END ) AS "P-1-A",
       SUM( CASE "mod" WHEN 'dz-P-2A' THEN "Remaining Counts" END ) AS "P-2-A",
       SUM( CASE "mod" WHEN 'dz-R-1T' THEN "Remaining Counts" END ) AS "R-1-T",
       SUM( CASE "mod" WHEN 'dz-R-1F' THEN "Remaining Counts" END ) AS "R-1-F",
       SUM( CASE "mod" WHEN 'dz-R-1O' THEN "Remaining Counts" END ) AS "R-1-O",
       SUM( CASE "mod" WHEN 'dz-P-1B' THEN "Remaining Counts" END ) AS "P-1-B",
       SUM( CASE "mod" WHEN 'dz-P-1D' THEN "Remaining Counts" END ) AS "P-1-D"
FROM   tbl
GROUP BY CountType

परिणाम :

|      COUNTTYPE | P-1-A | P-2-A | R-1-T |  R-1-F |  R-1-O |  P-1-B | P-1-D |
|----------------|-------|-------|-------|--------|--------|--------|-------|
| SimpleBinCount |  6351 | 12638 |  2051 |     68 |     47 | (null) |   121 |
|     CycleCount |    37 |    25 |  3318 | (null) | (null) | (null) |   321 |

प्रश्न 2 :

PIVOT ट्रांसपोज़िशन का उपयोग करना:

SELECT * FROM (
   SELECT SUBSTR( "mod", 4 ) AS "mod",
          "Remaining Counts",
          CountType
   FROM   tbl t
)
PIVOT
(
   SUM("Remaining Counts")
   FOR "mod" IN ( 'P-1A', 'P-2A', 'R-1T', 'R-1F', 'R-1O', 'P-1B', 'P-1D' )
)

परिणाम :

|      COUNTTYPE | 'P-1A' | 'P-2A' | 'R-1T' | 'R-1F' | 'R-1O' | 'P-1B' | 'P-1D' |
|----------------|--------|--------|--------|--------|--------|--------|--------|
| SimpleBinCount |   6351 |  12638 |   2051 |     68 |     47 | (null) |    121 |
|     CycleCount |     37 |     25 |   3318 | (null) | (null) | (null) |    321 |

संपादित करें - अपनी क्वेरी रैपिंग:

WITH qry AS (
  select drop_zone_id as "Mod", 
      bin_level as "Floor",
      icqa_process_properties.icqa_process_property_value as "Count Type",
      count(*) as "Remaining Counts",    
      concat(drop_zone_id, icqa_process_properties.icqa_process_property_value) as "Unique",
      to_char(sysdate,'hh:mi:ssam') as "Time Last Updated",
      to_char(sysdate, 'MM-DD-YYYY') as "Date Last Updated"
  from icqa_process_locations 
      inner join icqa_processes on icqa_processes.icqa_process_id = icqa_process_locations.icqa_process_id
      inner join icqa_process_properties on icqa_processes.icqa_process_id = icqa_process_properties.icqa_process_id
      inner join bins on bins.bin_id = icqa_process_locations.scannable_id
  where icqa_count_attempt_id is NULL and icqa_processes.process_status = ('Active')
      and drop_zone_id not like 'dz-R-1B' and drop_zone_id not like 'dz-P-1Z' and drop_zone_id not like 'dz-P-EACH_1'
      and icqa_process_properties.icqa_process_property_value in ('CycleCount', 'SimpleBinCount') 
  group by icqa_process_properties.icqa_process_property_value, bin_level, drop_zone_id
  order by icqa_process_properties.icqa_process_property_value, drop_zone_id
)
SELECT "Count Type",
       SUM( CASE "Mod" WHEN 'dz-P-1A' THEN "Remaining Counts" END ) AS "P-1-A",
       SUM( CASE "Mod" WHEN 'dz-P-2A' THEN "Remaining Counts" END ) AS "P-2-A",
       SUM( CASE "Mod" WHEN 'dz-R-1T' THEN "Remaining Counts" END ) AS "R-1-T",
       SUM( CASE "Mod" WHEN 'dz-R-1F' THEN "Remaining Counts" END ) AS "R-1-F",
       SUM( CASE "Mod" WHEN 'dz-R-1O' THEN "Remaining Counts" END ) AS "R-1-O",
       SUM( CASE "Mod" WHEN 'dz-P-1B' THEN "Remaining Counts" END ) AS "P-1-B",
       SUM( CASE "Mod" WHEN 'dz-P-1D' THEN "Remaining Counts" END ) AS "P-1-D"
FROM   qry
GROUP BY "Count Type";


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Oracle D2k फ़ॉर्म में PLSQL कोड प्रदर्शन को ट्यून या टेस्ट कैसे करें

  2. मैन्युअल रूप से संभव क्वेरी के भीतर पीएल/एसक्यूएल अपर्याप्त विशेषाधिकार

  3. Oracle SQL के लिए पार्सर

  4. Oracle sql एकल कॉलम में विभिन्न मानों के उदाहरणों की गणना करने के लिए

  5. बैचअपडेट अपवाद:बैच समाप्त नहीं होगा