计算特定列的值并按月和部分显示

Count values of a particular column and display it monthwise and partwise

CREATION_DATE   REJECTED_REASON                        PART_NAME                                             REJECTED_QTY                                        
03-03-2014  Metal chips in port face            PEGEOUT 1.8 CYLINDER HEAD CASTING H29                              15
03-03-2014  Angular hole Shrinkage              PEGEOUT 1.8 CYLINDER HEAD CASTING H29                              7
01-05-2014  5th  cap side dross                 CYL.HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA                   23
01-05-2014  Casting broken                      CYL.HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA                   3
01-05-2014  Bend in dand                        CYL . HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA                 11           
01-05-2014  Bend in casting                     CYL . HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA                 17
07-05-2014  Angular hole Shrinkage              EATON REAR HOUSING H-99                                            10
08-05-2014   Unclean                            CASTING CYLINDER HEAD 01 OF KOHLER H-185                           9
08-05-2014  Angular hole Shrinkage              CASTING CYLINDER HEAD 01 OF KOHLER H-185                           1
08-05-2014  Bend                                CASTING CYLINDER HEAD 01 OF KOHLER H-185                           20
12-11-2014  Shrinkage on top face               GEAR BOX HOUSING ITL CLUTCH - ITL                                  15
12-11-2014  Casting damage                      GEAR BOX HOUSING ITL CLUTCH - ITL                                  5 
13-11-2014  1st Exhaust Port Core Shift         PIAGGIO 3 VALVE CYLINDER HEAD CASTING                              22
06-01-2015   Shrinkage in spark plug            G-30 CYLINDER HEAD REAR CASTING                                    14
08-01-2015  1st cap side dross                  G-30 CYLINDER HEAD REAR CASTING                                    6
08-01-2015   Unclean                            G-30 CYLINDER HEAD REAR CASTING                                    2 
08-01-2015   Shrinkage in spark plug            G-30 CYLINDER HEAD REAR CASTING                                    12 
13-01-2015  1st cap side dross                  G-30 CYLINDER HEAD REAR CASTING                                    1
13-01-2015  1st Exhaust Port Core Shift         G-30 CYLINDER HEAD REAR CASTING                                    9
13-01-2015   Shrinkage in spark plug            G-30 CYLINDER HEAD REAR CASTING                                    10

假设我在一个特定的值中有以上值 table.Now 我需要汇总拒绝数量并将拒绝原因显示为列,拒绝数量显示为按月和按月计算的值 假设我需要 5 月 2014.The 的数据,上述值列表的报告输出应该是:-

PART NAME                                         5th cap side dross    Casting Broken  Bend in Casting   Bend in Dand  Angular Hole Shrinkage  Unclean Bend
CYL . HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA         23                   3             17               11                   0               0   0
CASTING CYLINDER HEAD 01 OF KOHLER H-185                   0                    0             0                0                    1               9   20
EATON REAR HOUSING H-99                                    0                    0             0                0                    10              0   0

请帮助我并指导我通过 sql 或 pl sql 在 oracle 中实现此任务。

试试这个:

SELECT * 
FROM your_table_name
PIVOT (COUNT(REJECTED_REASON)
FOR REJECTED_REASON IN('reason1', 'reason2', 'reason3' ....))
;

最好的方法是像 Aramillo 所说的那样使用动态枢轴。 Oracle 有类似的东西,它是 pivot xml 子句,但我有一些查询要问你,它动态地完成工作而无需 xml 操作。正如您所说,您只需要显示报告,因此经过一些格式化后,以下查询的输出将适合您的情况:

            select part_name , 
rtrim(xmlagg(xmlelement(e,rejected_reason,',').extract('//text()')),',') 
|| chr(10) /*new line char */ ||
rtrim(xmlagg(xmlelement(e,cnt,',').extract('//text()')),',') 
from (
  select part_name, 
         rejected_reason,
         row_number() over (partition by part_name, rejected_reason order by rejected_reason) as rn,
         count(*) over (partition by part_name, rejected_reason order by rejected_reason) as cnt
  from your_table
  order by part_name, 
         rejected_reason
)
where data_column = 'MAY 2014'
and rn = 1 -- to avoid duplicates in groups
group by part_name;

请copy/paste查看结果。

SELECT part_name    
 , sum(decode(reject_reason, 'Metal chips in port face', 1, 0)) "Metal chips in port face"
 , sum(decode(reject_reason, 'Angular hole Shrinkage', 1, 0)) "Angular hole Shrinkage"
 , sum(decode(reject_reason, '5th cap side dross', 1, 0)) "5th cap side dross"
 , sum(decode(reject_reason, 'Casting broken', 1, 0)) "Casting broken"
 , sum(decode(reject_reason, 'Bend in dand', 1, 0)) "Bend in dand"
 , sum(decode(reject_reason, 'Bend in casting', 1, 0)) "Bend in casting"
FROM
(
SELECT '03-03-2014' create_date, 'Metal chips in port face'  reject_reason,'PEGEOUT 1.8 CYLINDER HEAD CASTING H29' part_name FROM dual
UNION ALL
SELECT '03-03-2014', 'Angular hole Shrinkage', 'PEGEOUT 1.8 CYLINDER HEAD CASTING H29' FROM dual
UNION ALL
SELECT '01-05-2014', '5th cap side dross', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', '5th cap side dross', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', 'Casting broken'    , 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', 'Bend in dand', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', 'Bend in casting', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
)
GROUP BY part_name
ORDER BY 1, 3
/

SELECT part_name
 , MAX(CASE WHEN reject_reason = 'Metal chips in port face' THEN cnt END) "Metal chips in port face"
 , MAX(CASE WHEN reject_reason = 'Angular hole Shrinkage' THEN cnt END) "Angular hole Shrinkage"
 , MAX(CASE WHEN reject_reason = '5th cap side dross' THEN cnt END) "5th cap side dross"
 , MAX(CASE WHEN reject_reason = 'Casting broken' THEN cnt END) "Casting broken"
 , MAX(CASE WHEN reject_reason = 'Bend in dand' THEN cnt END) "Bend in dand"
 , MAX(CASE WHEN reject_reason = 'Bend in casting' THEN cnt END) "Bend in casting"
FROM (
  SELECT part_name
   , reject_reason
   , count(*) over (partition by part_name, reject_reason order by reject_reason) as cnt
 FROM (
SELECT '03-03-2014' create_date, 'Metal chips in port face'  reject_reason, 'PEGEOUT 1.8 CYLINDER HEAD CASTING H29' part_name FROM dual
UNION ALL
SELECT '03-03-2014', 'Angular hole Shrinkage', 'PEGEOUT 1.8 CYLINDER HEAD CASTING H29' FROM dual
UNION ALL
SELECT '01-05-2014', '5th cap side dross', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', '5th cap side dross', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', 'Casting broken'    , 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', 'Bend in dand', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
UNION ALL
SELECT '01-05-2014', 'Bend in casting', 'CYL. HEAD VM MOTORI-4 CYL TESTA CILINDRI LAVORATA' FROM dual
))
GROUP BY part_name
ORDER BY 1
/