我有下表
+----+----------+------+
|D_ID|date |value |
+----+----------+------+
|0 |1992-02-01|119940|
+----+----------+------+
|1 |1992-02-01|119941|
+----+----------+------+
|1 |1992-02-02|119942|
+----+----------+------+
|2 |1992-02-01|119943|
+----+----------+------+
|0 |1992-02-02|119944|
+----+----------+------+
|0 |1992-02-03|119945|
+----+----------+------+
|2 |1992-02-02|119946|
+----+----------+------+
|1 |1992-02-04|119947|
+----+----------+------+
|1 |1992-02-05|119948|
+----+----------+------+
|2 |1992-02-02|119949|
+----+----------+------+
|2 |1992-02-03|119940|
+----+----------+------+
|0 |1992-02-04|119940|
+----+----------+------+
有没有办法获得以下结果?我想得到每天的D_ID值。
+----------+------+------+------+
|date |D_ID_0|D_ID_1|D_ID_2|
+----------+------+------+------+
|1992-02-01|119940|119941|119943|
+----------+------+------+------+
|1992-02-02|119944|119942|119949|
+----------+------+------+------+
|1992-02-03|119945| |119940|
+----------+------+------+------+
|1992-02-04|119940|119947| |
+----------+------+------+------+
|1992-02-05| |119948| |
+----------+------+------+------+
答案 0 :(得分:2)
希望这有帮助!
SELECT date ,
MAX(DECODE(D_ID,0,value,NULL)) as D_ID_0,
MAX(DECODE(D_ID,1,value,NULL)) as D_ID_1,
MAX(DECODE(D_ID,2,value,NULL)) as D_ID_2
FROM
your_table
GROUP BY date ;
SELECT date ,
MAX(IF(D_ID=0,value,NULL)) as `D_ID_0`,
MAX(IF(D_ID=1,value,NULL)) as `D_ID_1`,
MAX(IF(D_ID=2,value,NULL)) as `D_ID_2`
FROM
your_table
GROUP BY date ;
OR
SELECT date ,
MAX((CASE WHEN (d_id = 0) THEN value ELSE NULL end)) AS `D_ID_0`,
MAX((CASE WHEN (d_id = 1) THEN value ELSE NULL end)) AS `D_ID_1`,
MAX((CASE WHEN (d_id = 2) THEN value ELSE NULL end)) AS `D_ID_2`
FROM
your_table
GROUP BY date ;
答案 1 :(得分:1)
使用Pivot
WITH tab(D_ID,d_date,d_value) AS
(SELECT 0 , '1992-02-01', 119940 FROM dual UNION ALL
SELECT 1, '1992-02-01', 119941 FROM dual UNION ALL
SELECT 1, '1992-02-02', 119942 FROM dual UNION ALL
SELECT 2, '1992-02-01', 119943 FROM dual UNION ALL
SELECT 0, '1992-02-02', 119944 FROM dual UNION ALL
SELECT 0, '1992-02-03', 119945 FROM dual UNION ALL
SELECT 2, '1992-02-02', 119946 FROM dual UNION ALL
SELECT 1, '1992-02-04', 119947 FROM dual UNION ALL
SELECT 1, '1992-02-05', 119948 FROM dual UNION ALL
SELECT 2, '1992-02-02', 119949 FROM dual UNION ALL
SELECT 2, '1992-02-03', 119940 FROM dual UNION ALL
SELECT 0, '1992-02-04', 119940 FROM dual)
-------
--End of Data
-------
SELECT * FROM tab
pivot (min(d_VALUE) AS dd_value FOR d_id IN (0 ,1 ,2));
输出:
| D_DATE | 0_DD_VALUE | 1_DD_VALUE | 2_DD_VALUE |
|------------|------------|------------|------------|
| 1992-02-04 | 119940 | 119947 | (null) |
| 1992-02-03 | 119945 | (null) | 119940 |
| 1992-02-02 | 119944 | 119942 | 119946 |
| 1992-02-05 | (null) | 119948 | (null) |
| 1992-02-01 | 119940 | 119941 | 119943 |
答案 2 :(得分:0)
将原始表格单元格值存储在双数组中。在唯一数组中对D_ID的值进行排序。使用for循环或在jquery“each”中创建新的表格单元格。在for循环中,D_ID应该找到匹配项。将第1列的原始标题([n [[0])与唯一的行值连接起来,并将其作为新表的位置。存储和排序匹配并放置的行。
有关javascript中数组的信息