0

I have a DB of 1 table:

APP_ID|SETTING_NAME|SETTING_VALUE|ROW_LST_UPD_TS
1     |ab1         |111          |17:32 
1     |ab2         |112          |17:32
--------------------------------------- 
1     |ab1         |111          |14:31 
1     |ab2         |112          |14:31 
---------------------------------------
2     |ba1         |121          |18:44 
2     |ba2         |122          |18:44
--------------------------------------- 
2     |ba1         |121          |15:27 
2     |ba2         |122          |15:27
--------------------------------------- 

Using llblgen pro and LinqMetaAdapter I need to get ALL records with the latest ROW_LST_UPD_TS grouped by APP_ID

I came up with the oracle query:

SELECT distinct 
    configurationLog.SETTING_NAME, 
    configurationLog.SETTING_VALUE, 
    configurationLog.ROW_LST_UPD_TS, 
    configurationLog.ROW_LST_UPD_UID, 
    configurationLog.APP_ID
FROM 
    EVENT_MGT.CONFIGURATION_LOG configurationLog
INNER JOIN
(
    SELECT APP_ID, MAX(ROW_LST_UPD_TS) maxTimestamp 
    FROM EVENT_MGT.CONFIGURATION_LOG
    GROUP BY APP_ID
) latestSettingPerApp
ON 
    latestSettingPerApp.maxTimestamp <= configurationLog.ROW_LST_UPD_TS 
    and latestSettingPerApp.maxTimestamp >= configurationLog.ROW_LST_UPD_TS - 1/1440  
    and latestSettingPerApp.APP_ID = configurationLog.APP_ID
ORDER BY 
    configurationLog.APP_ID;

but having troubles to convert it to LINQ

1 Answers1

1

This is only indirectly related, as it's not a solution for your LINQ issue, but I think your query is much better off written using a windowing function. I think it's easier to read and maintain, and it's likely to be quite a bit more efficient for large datasets.

with max_vals as (
  SELECT
    SETTING_NAME, 
    SETTING_VALUE, 
    ROW_LST_UPD_TS, 
    ROW_LST_UPD_UID, 
    APP_ID,
    max (ROW_LST_UPD_TS) over (partition by app_id) as max_ts
  FROM 
    EVENT_MGT.CONFIGURATION_LOG configurationLog
)
select
  SETTING_NAME, SETTING_VALUE, ROW_LST_UPD_TS, ROW_LST_UPD_UID, APP_ID
from max_vals
where
  ROW_LST_UPD_TS = max_tx
order by
  APP_ID

The construct you use looks like what you would do for a DMBS where analytic functions weren't available.

Again, not a solution, but more than I could reasonably fit in a comment.

Hambone
  • 15,600
  • 8
  • 46
  • 69