4

我有选择语句,例如:

select trim(time), type, count(1) from table                             
group by trim(time),type

结果是:

02.10.13    REZ1    1

02.10.13    REZ2    5

02.10.13    REZ3    3

是否可以使用某些 Oracle 函数创建一些 select 语句以获得以下结果:

REZ1    REZ2    REZ3

1       5       3

因此,一列的结果是另一条语句中的列名,例如:

select ?SOMETHING? 
from (
select trim(time), type, count(1) 
from table                             
group by trim(time),type) s
4

4 回答 4

1
WITH t(l_date, val, l_count)
AS
(SELECT to_date('02.10.13', 'dd.mm.yy'), 'REZ1'   , 1 FROM dual UNION
SELECT to_date('02.10.13', 'dd.mm.yy'), 'REZ2'   , 5 FROM dual UNION
SELECT to_date('02.10.13', 'dd.mm.yy'), 'REZ3'   , 3 FROM dual 
)

SELECT *
FROM
     ( SELECT  val, l_count FROM   t
     ) PIVOT (MAX(l_count) FOR (val) IN ('REZ1' REZ1,'REZ2' REZ2 ,'REZ3' REZ3));
于 2013-10-30T10:39:30.920 回答
0

假设第二列除了 REZ1..3 之外没有任何值,那么下面的解决方案将转置表数据并通过将值相加来处理重复。

WITH t
 AS (SELECT TO_DATE ('02.10.13', 'dd.mm.yy') AS dt,
            'REZ1' AS typ,
            1 AS cnt
       FROM DUAL
     UNION
     SELECT TO_DATE ('02.10.13', 'dd.mm.yy') AS dt,
            'REZ2' AS typ,
            5 AS cnt
       FROM DUAL
     UNION
     SELECT TO_DATE ('02.10.13', 'dd.mm.yy') AS dt,
            'REZ3' AS typ,
            3 AS cnt
       FROM DUAL
     UNION
     SELECT TO_DATE ('03.10.13', 'dd.mm.yy') AS dt,
            'REZ1' AS typ,
            7 AS cnt
       FROM DUAL
     UNION
     SELECT TO_DATE ('03.10.13', 'dd.mm.yy') AS dt,
            'REZ2' AS typ,
            2 AS cnt
       FROM DUAL)
  SELECT dt,
     SUM (CASE WHEN typ = 'REZ1' THEN cnt ELSE 0 END) AS "REZ1",
     SUM (CASE WHEN typ = 'REZ2' THEN cnt ELSE 0 END) AS "REZ2",
     SUM (CASE WHEN typ = 'REZ3' THEN cnt ELSE 0 END) AS "REZ3"
FROM t
GROUP BY dt;


DT              REZ1       REZ2       REZ3
--------- ---------- ---------- ----------
02-OCT-13          1          5          3
03-OCT-13          7          2          0

2 rows selected.

Incase column#2 将具有随机值,可能需要使用某种游标来调整此查询以考虑动态列。如果我能够设计好,我会尝试发布。

于 2013-11-08T20:29:35.170 回答
0
select trim(time), 
       count(decode(type, 'REZ1', 1)) AS REZ1,
       count(decode(type, 'REZ2', 1)) AS REZ2,
       count(decode(type, 'REZ3', 1)) AS REZ3
  from table                             
 group by trim(time)
于 2013-12-11T16:57:11.543 回答
0

也许您可以为此尝试使用 SQL 替代方法:

SELECT MAX(CASE type
                WHEN 'REZ1' THEN cnt
                ELSE NULL
           END) AS REZ1,
       MAX(CASE type
                WHEN 'REZ2' THEN cnt
                ELSE NULL
           END) AS REZ2,
       MAX(CASE type
                WHEN 'REZ3' THEN cnt
                ELSE NULL
           END) AS REZ3
  FROM (SELECT trim(time), type, count(1) as cnt FROM table
        GROUP BY trim(time), type);

甚至更好:

SELECT COUNT(CASE type
                 WHEN 'REZ1' THEN 1
                 ELSE NULL
             END) AS REZ1,
       COUNT(CASE type
                 WHEN 'REZ2' THEN 1
                 ELSE NULL
             END) AS REZ2,
       COUNT(CASE type
                 WHEN 'REZ3' THEN 1
                 ELSE NULL
             END) AS REZ3
  FROM table;

输出:

REZ1 | REZ2 | REZ3
1    | 5    | 3

当您知道列中的值时,这适用type。Even pivot(在 11g 中可用)要求您知道列中的值,以便按照 ajmalmhd04 的回答中所述进行旋转。

于 2013-10-30T10:39:15.673 回答