假设第二列除了 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 将具有随机值,可能需要使用某种游标来调整此查询以考虑动态列。如果我能够设计好,我会尝试发布。