有沒有一種方法,我怎麼可以查詢以前的行值,併合併到當前行,這裏是我的示例表場景:查詢以前的行值,併合併到當前行
+-------------------------------------+
| ColA | ColB | ColValue | Date |
|------|------|----------|------------|
| AAA | 111 | 5 | 2017-04-23 |
| AAA | 111 | 4 | 2017-04-22 |
| AAA | 111 | 3 | 2017-04-21 |
| BBB | 222 | 5 | 2017-04-30 |
| BBB | 222 | 4 | 2017-04-29 |
+-------------------------------------+
而我預期的結果應該是這只是想獲得以前和當前值,並按選定的列和日期進行分組。
+--------------------------------------------------+
| ColA | ColB | PreValue | CurValue | Date |
|------|------|----------|-------------------------|
| AAA | 111 | 4 | 5 | 2017-04-23 |
| AAA | 111 | 3 | 4 | 2017-04-22 |
| AAA | 111 | N/A | 3 | 2017-04-21 |
| BBB | 222 | 4 | 5 | 2017-04-30 |
| BBB | 222 | N/A | 4 | 2017-04-29 |
+--------------------------------------------------+
任何建議或解決方案,在此先感謝
下面是從我的實際數據作爲參考我的實際查詢:
SELECT ai.APName, tbap.Value , tbap.DateTime, tbap.Comment, tbap.ModifiedBy, tbap.ToolName, d.Name as Strategy FROM (SELECT dt.*,ins.Value as ToolName FROM (SELECT av.*,ai.DocumentID,ai.IndexID FROM ControlAutomation.appartitionindexes ai
JOIN (SELECT * FROM ControlAutomation.appartitionvalues
where DateTime > '2017-04-22 23:17:13' and DateTime < '2017-04-26 23:18:28') av
ON ai.APPartitionID = av.APPartitionID) dt INNER JOIN factory.indexes ins ON ins.ID = dt.IndexID
where dt.comment like '%updateAdjustableParameter%'
group by dt.ID) tbap
INNER JOIN ControlAutomation.documents d ON d.ID = tbap.DocumentID
INNER JOIN appartitionindexes ai ON ai.APPartitionID = tbap.APPartitionID
GROUP BY tbap.ID
ORDER BY tbap.ToolName DESC, d.Name, tbap.DateTime DESC
LIMIT 100
表中有主鍵嗎?或者換句話說要知道哪個是以前的價值。由於給定的示例表AAA具有全部第23個日期,因此無法知道哪個是第一個 –
爲什麼需要此輸出,這可能需要使用會話變量? –
實際上,在我的真實數據中,日期列是唯一的,我將它用作我的主要數據,所以這就是爲什麼它需要先按日期進行分組,然後是colA然後colB –