根据原始列之一中的值对新值进行分组的一些数据进行逆透视

Unpivot some data grouping the new values based on the value in one of the original columns

在让 Unpivot 完全按照我的意愿行事时遇到了一些麻烦,不确定是否可以不使用另一个连接或联合(如果可能的话我想避免)任何帮助将不胜感激。 :)

select *
  from (select "Not Executed" + "Pass" + "Fail" + "Blocked" As "Total",
               "Pass" + "Fail" + "Blocked" As "Executed",
               "Pass",
               "Fail",
               "Blocked",
               "Not Executed",
               Priority        
          from (select TRS.NAME AS Status, TPS.Name as Priority
                  from TEST_RESULT TR
                  join (Select ID, NAME
                         from RESULT_STATUS
                        where "GROUP" = 'TEST_RESULT_STATUS') TRS
                    on TR.TEST_RESULT_STATUS_ID = TRS.ID
                  join TEST_RUN TRN
                    on TR.TEST_RUN_ID = TRN.ID
                  join TEST_CASE TC
                    on TC.ID = TR.TEST_CASE_ID
                  join (Select ID, NAME
                         from RESULT_STATUS
                        where "GROUP" = 'TEST_CASE_PRIORITY') TPS
                    on TC.PRIORITY_ID = TPS.ID
                --Add your test cycles that you want to report on here
                 where TRN.KEY IN ('PSD-C3')

                ) pivot(count(Status) for Status in('Not Executed' as
                                                    "Not Executed",
                                                    'Pass' as "Pass",
                                                    'Fail' as "Fail",
                                                    'Blocked' as "Blocked"))) p;

Returns:

Total   Executed    Pass    Fail    Blocked Not Executed    PRIORITY
37      36          18      11      7       1               High
32      26          18      7       1       6               Normal

我想要的是:

Status          High    Normal
Total           37      32
Executed        36      26
Pass            18      18
Fail            11      7
Blocked         7       1
Not Executed    1       6

首先应用unpivot,然后conditional aggregation通过ceiling函数将结果分组分成两组以便能够pivot作为接下来的两个额外列到 Status 列:

select Status as "Status", 
       max(case when ceil(rn/(cn/2))=1 then val end) as "High",
       max(case when ceil(rn/(cn/2))=2 then val end) as "Normal"
  from
  (
   select row_number() over (order by 1) as rn,
          count(*) over (order by 1) as cn,
          Status,val 
     from tab
  unpivot (val for Status in (Total, Executed, Pass, Fail, Blocked, Not_Executed))
  )
  group by status;

Demo