0

このコードから、locationid 5,6,7,8 が続くのではなく、2 行目にある可能性はありますか?

SELECT
    MAX(case when locationid = '1' then floatvalue end) Flow,
    MAX(case when locationid = '2' then floatvalue end) Level,
    MAX(case when locationid = '3' then floatvalue end) Pressure,
    MAX(case when locationid = '4' then floatvalue end) MR,  
    MAX(case when locationid = '5' then floatvalue end) Flow,
    MAX(case when locationid = '6' then floatvalue end) Level,
    MAX(case when locationid = '7' then floatvalue end) Pressure,
    MAX(case when locationid = '8' then floatvalue end) MR
FROM table

したがって、これの代わりに:

Flow | Level | Pressure | MR | Flow | Level | Pressure | MR  |
10   |  20   |   30     | 40 | 100  |  200  |   300    | 400 |

それはこのようになります:

Flow | Level | Pressure | MR  |
 10  |  20   |   30     | 40  |
 100 |  200  |   300    | 400 |
4

1 に答える 1

1

このようなものをお探しですか?

SELECT MAX(CASE WHEN locationid = 1 THEN floatvalue END) Flow,
       MAX(CASE WHEN locationid = 2 THEN floatvalue END) Level,
       MAX(CASE WHEN locationid = 3 THEN floatvalue END) Pressure,
       MAX(CASE WHEN locationid = 4 THEN floatvalue END) MR
  FROM table1
 WHERE locationid IN(1, 2, 3, 4)
UNION ALL
SELECT MAX(CASE WHEN locationid = 5 THEN floatvalue END),
       MAX(CASE WHEN locationid = 6 THEN floatvalue END),
       MAX(CASE WHEN locationid = 7 THEN floatvalue END),
       MAX(CASE WHEN locationid = 8 THEN floatvalue END)
  FROM table1
 WHERE locationid IN(5, 6, 7, 8) 

これがSQLFiddleのデモです

于 2013-11-14T22:18:38.580 に答える