好的,我有一张表(MyTable),如下所示:
value1 | value2 | value3 | value4 | value5 | date
---------------------------------------------------------
1 | 4 | 6 | 25 | 35 | 2011-10-07
---------------------------------------------------------
2 | 3 | 6 | 25 | 14 | 2011-10-01
---------------------------------------------------------
我的最终目标是获得一个列,其中包含每个唯一编号和最新的ascciated日期,如:
combinedValues | date
----------------------------
1 | 2011-10-07
----------------------------
4 | 2011-10-07
----------------------------
6 | 2011-10-07
----------------------------
25 | 2011-10-07
----------------------------
35 | 2011-10-07
----------------------------
2 | 2011-10-01
----------------------------
3 | 2011-10-01
----------------------------
14 | 2011-10-01
----------------------------
到目前为止,我已经使用它来处理此查询,但只有一列:
SELECT t.value1, t.date
FROM MyTable t
INNER JOIN (
SELECT value1, MAX( date ) AS LatestDate
FROM MyTable
GROUP BY value1
)tm ON t.value1= tm.value1
AND t.date = tm.LatestDate
GROUP BY value1
ORDER BY date DESC
那么我将如何使用combinedValues完成此表?它甚至可能吗?
答案 0 :(得分:2)
尝试此查询:
Select val,Max(date) date From
(Select value1 as val,date From MyTable
Union All
Select value2 as val,date From MyTable
Union All
Select value3 as val,date From MyTable
Union All
Select value4 as val,date From MyTable
Union All
Select value5 as val,date From MyTable)tab
Group by val order by date desc,val;