查询从更新的日期时间戳获取最新数据

时间:2018-09-21 07:21:19

标签: sql json postgresql

我只想根据我更新的timstamp数据从表中获取最新记录,因为我想在crontab中运行此查询,我怎么能始终从查询中获取最新的更新数据,请帮助,这是我的查询

select 
    x.updated as updated,
    x.data_a->>'custom1' as mch_group,
    x.data_a->>'custom2' as mch_name,
    x.data_a->>'custom3' as mch_id,
    x.data_a->'simAuxFieldsDTO'->>'custom4' as terminal_id,
    x.data_a->>'msisdn' as msisdn, 
    x.data_a->>'simId' as sim_id,
    x.data_a->>'acctName' as acct_name, 
    x.data_a->>'activationDate' as activation_date,
    x.data_a->>'inSession' as in_session, 
    x.data_a->>'sessionType' as session_type,
    x.data_a->>'statusNameDisplay' as status_name_display, 
    x.data_a->>'monthToDateDataUsageMB' as month_todate_data_usage, 
    x.data_a->>'linePayStatus' as line_pay_status, 
    x.data_a->>'iccid' as iccid, 
    x.data_a->>'monthToDateSmsUsage' as month_todate_sms_usage, 
    x.data_a->>'monthToDateVoiceUsage' as month_todate_voice_usage, 
    x.data_a->>'overageLimitReached' as overage_limit_reached, 
    x.data_a->>'termStartDate' as term_startdate, 
    x.data_a->>'termEndDate' as term_enddate, 
    x.data_a->>'migratedSim' as migrated_sim 
 from (
   select jsonb_array_elements(a.data_a) as data_a, a.updated as updated 
   from (
     select updated, json_data -> 'data' as data_a 
     from tcash_edc_json 
     where updated > TO_TIMESTAMP('2018-09-21 03:02:00','YYYY-MM-DD HH:MI:SS')
    )a
) x;

感谢您的关注和感谢

-海妖。

2 个答案:

答案 0 :(得分:1)

尝试用order by和limit替换where条件: 代替这个:

where updated > TO_TIMESTAMP('2018-09-21 03:02:00','YYYY-MM-DD HH:MI:SS') 

此:

order by updated desc limit 1

答案 1 :(得分:0)

您可以尝试获取max(updated),然后在“ updated” =“ max(updated)”处进行选择