Oracle SQL - 将同一个表的多行组合到自己的行中

时间:2018-04-09 11:59:12

标签: sql oracle oracle10g pivot

我有一个类似于SQL Fiddle的表:http://sqlfiddle.com/#!4/0fabe0/2/0

该表包含12行。 6属于一个“集”,另外6个属于另一个“集”。它们按标识符列分组。实际上,我想要查询的表包含更多的行和列。

我想做一个选择,每个标识符给我一行。 然后它应该显示insertTime,然后为每个“step”显示“Yes”值,最后显示每个“step”值“No”值。

我认为我需要使用group by,distinct和join的组合来实现这一点,但我无法弄清楚如何。

以下是它的样子:

+------------+------------------+----------+----------+----------+---------+---------+---------+
| identifier |    insertTime    | yesStep3 | yesStep2 | yesStep1 | noStep1 | noStep2 | noStep3 |
+------------+------------------+----------+----------+----------+---------+---------+---------+
|        123 | 08.04.2018 13:37 | 13.3     | 12.2     | 11.1     | 13      | 13.3    | 14.1    |
|        124 | 08.04.2018 13:40 | 14.14    | 13.13.   | 12.12.   | 10.1    | 11.11   | 9.9     |
+------------+------------------+----------+----------+----------+---------+---------+---------+

1 个答案:

答案 0 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE testTable(
  identifier NUMBER(3,0),
  insertTime timestamp,
  yesNo      NUMBER(1,0),
  stepNumber NUMBER(1,0),
  value      NUMBER(5,2)
);

INSERT INTO testTable (identifier, insertTime, yesNo, stepNumber, value)
  SELECT 123, TIMESTAMP '2018-04-08 13:37:00', 0, 1, 13.0 FROM DUAL UNION ALL
  SELECT 123, TIMESTAMP '2018-04-08 13:37:00', 0, 2, 13.3 FROM DUAL UNION ALL
  SELECT 123, TIMESTAMP '2018-04-08 13:37:00', 0, 3, 14.1 FROM DUAL UNION ALL
  SELECT 123, TIMESTAMP '2018-04-08 13:37:00', 1, 1, 11.1 FROM DUAL UNION ALL
  SELECT 123, TIMESTAMP '2018-04-08 13:37:00', 1, 2, 12.2 FROM DUAL UNION ALL
  SELECT 123, TIMESTAMP '2018-04-08 13:37:00', 1, 3, 13.3 FROM DUAL UNION ALL
  SELECT 124, TIMESTAMP '2018-04-08 13:40:00', 0, 1,  9.9 FROM DUAL UNION ALL
  SELECT 124, TIMESTAMP '2018-04-08 13:40:00', 0, 2, 10.10 FROM DUAL UNION ALL
  SELECT 124, TIMESTAMP '2018-04-08 13:40:00', 0, 3, 11.11 FROM DUAL UNION ALL
  SELECT 124, TIMESTAMP '2018-04-08 13:40:00', 1, 1, 12.12 FROM DUAL UNION ALL
  SELECT 124, TIMESTAMP '2018-04-08 13:40:00', 1, 2, 13.13 FROM DUAL UNION ALL
  SELECT 124, TIMESTAMP '2018-04-08 13:40:00', 1, 3, 14.14 FROM DUAL;

查询1

SELECT identifier,
       insertTime,
       MAX( CASE WHEN yesNo = 1 AND stepNumber = 1 THEN value END ) AS yesStep1,
       MAX( CASE WHEN yesNo = 1 AND stepNumber = 2 THEN value END ) AS yesStep2,
       MAX( CASE WHEN yesNo = 1 AND stepNumber = 3 THEN value END ) AS yesStep3,
       MAX( CASE WHEN yesNo = 0 AND stepNumber = 1 THEN value END ) AS noStep1,
       MAX( CASE WHEN yesNo = 0 AND stepNumber = 2 THEN value END ) AS noStep2,
       MAX( CASE WHEN yesNo = 0 AND stepNumber = 3 THEN value END ) AS noStep3
FROM testTable
GROUP BY identifier, insertTime

<强> Results

| IDENTIFIER |            INSERTTIME | YESSTEP1 | YESSTEP2 | YESSTEP3 | NOSTEP1 | NOSTEP2 | NOSTEP3 |
|------------|-----------------------|----------|----------|----------|---------|---------|---------|
|        123 | 2018-04-08 13:37:00.0 |     11.1 |     12.2 |     13.3 |      13 |    13.3 |    14.1 |
|        124 | 2018-04-08 13:40:00.0 |    12.12 |    13.13 |    14.14 |     9.9 |    10.1 |   11.11 |