为每个单元拉取历史文件的第一条记录

Pulling the first record of a history file for every unit

iSeries DB2 v6r1m0

我正在尝试为每个单元提取历史文件的第一条记录,然后将它们绑定到我的主 table,已经包含其他连接逻辑。

完整的示例历史文件(假设 table 已经按 UnitTime 排序(从最新到最旧)

UnitID  UnitStatus  UnitDate    UnitTime        
1       Asleep      1/8/2015    22:00
1       Awake       1/8/2015    8:00
2       Awake       1/8/2015    7:00
3       Asleep      1/8/2015    5:00
1       Asleep      1/8/2015    0:00
2       Asleep      1/7/2015    23:00
3       Awake       1/7/2015    13:00
1       Awake       1/7/2015    8:30
2       Awake       1/7/2015    6:50

求历史记录(仅first/latest条记录)

UnitID  UnitStatus  UnitDate    UnitTime
1       Asleep      1/08/2015   22:00
2       Awake       1/08/2015   7:00
3       Asleep      1/08/2015   5:00

想要的最终结果(一个非常大的查询,简化了,按 UnitLastHeardFrom 排序)

UnitID  UnitStatus  UnitDate    UnitTime    UnitGoingTo UnitComingFrom  UnitLastHeardFrom   UnitVehicle
2       Awake       1/08/2015   7:00        New York    Chicago         6:00                Unit20
1       Asleep      1/08/2015   22:00       Denver      Los Angelos     5:00                Unit10
3       Asleep      1/08/2015   5:00        London      Phoenix         4:00                Unit30

@Juan Carlos Oropeza 我仍在研究您的第一个解决方案。它给我一个错误。

WITH cteTbl AS (
    SELECT
         UnitID
         MAX( CAST(UnitDate AS CHAR(10) ) || ' ' || CAST (UnitTime as CHAR(10) ) ) AS cteMaxTime
    FROM
         myHistoryTableCTE
)
SELECT
    UnitID
    UnitStatus
    ,CAST(UnitDate AS CHAR(10)) || ' ' || CAST(UnitTime AS CHAR(10)) AS dattim
    FROM
        myHistoryTable
INNER JOIN cteTbl
    on myHistoryTable.UnitID = myHistoryTableCTE.UnitID
         AND myHistoryTable.dattim = myHistoryTableCTE.cteMaxTime -- error says column dattim does not exist in myHistoryTable

你的第二个解决方案可以独立运行,直到我将我的主要查询逻辑添加到它。我试了两种方法。

/* Main Select Logic  */
/* Main From Logic */
/* LEFT JOIN bunch of stuff */
LEFT JOIN 
(
    WITH history AS --ERROR: AS is not expected.
    (
        /* Insert second solution */
        where history.rowNumber = 1
    )
) as h
ON mainTableID = h.UnitID

WHERE

/*Main Where logic*/
/*Group By logic*/
/*Order By logic*/

然后我尝试了这个。

WITH history AS
    (
        /* Insert second solution */
    )
) as h

/* Main Select Logic  */
,SUM(CASE WHEN /* condition */ THEN 1 ELSE 0 END) AS IfLogic, SUM(CASE WHEN /* condition */ THEN 1 ELSE 0 END) AS IfMoreLogic --If I remove this line and its associated Left Join logic below, the query works.  There's nothing that stands out to me in this function.  I have a MAX function too and I don't need to remove that one.
/* Main From Logic */
/* LEFT JOIN bunch of stuff */

LEFT JOIN history
    ON mainTableID = h.UnitID

WHERE

/*Main Where logic*/

AND history.rowNumber = 1

/*Group By logic*/
/*Order By logic*/
--ERROR: Code 6 OLAP Functions are not supported.
  • 首先计算每个单元的最大条目数

  • 然后加入你的historytable看看当时的单位状态是什么

.

WITH lastTime as (
    SELECT UnitID, MAX(UnitTime) lTime
    FROM history 
    GROUP BY UnitID
)
SELECT h.UnitID,  h.UnitStatus,  h.UnitTime
FROM history h
inner join lastTime l
  on h.UnitID = l.UnitID
 and h.lTime = l.lTime

其他解决方案是使用 row_number() over partition,在这种情况下,每个 UnitID 都有一个从 1..n 开始的序列,最新的 UnitTime

WITH lastTime as (
    SELECT 
         UnitID, 
         UnitStatus,
         UnitTime,
         row_number() over(partition by UnitID order by UnitTime desc) as rn, 
    FROM history 
)
SELECT l.UnitID,  l.UnitStatus,  l.UnitTime
FROM lastTime l
WHERE l.rn = 1