如何解析时间戳和状态? (新手)

时间:2015-04-27 20:20:20

标签: sql amazon-redshift

我不知道如何解决这个问题,但这是一个示例表:

╔════════════╦════════╦═══════════╗
║ Company_ID ║ Status ║ Timestamp ║
╠════════════╬════════╬═══════════╣
║       1234 ║ Test   ║ 4/1/15    ║
║       1234 ║ Live   ║ 3/30/15   ║
║       3456 ║ Live   ║ 1/30/15   ║
║       4567 ║ Test   ║ 2/12/15   ║
║       3456 ║ Test   ║ 4/15/15   ║
╚════════════╩════════╩═══════════╝

我想提取最新的时间戳,所以我只想要:

╔════════════╦════════╦═══════════╗
║ Company_ID ║ Status ║ Timestamp ║
╠════════════╬════════╬═══════════╣
║       1234 ║ Test   ║ 4/1/15    ║
║       4567 ║ Test   ║ 2/12/15   ║
║       3456 ║ Test   ║ 4/15/15   ║
╚════════════╩════════╩═══════════╝

到目前为止,我想试试这个,但我担心它只是拉出最大的时间戳,而不是相关的状态。这是对的吗?

select
    company_id,
    status,
    max(timestamp)
from
    sample
group by 1, 2
编辑:它在Redshift(PostgreSQL)上。

4 个答案:

答案 0 :(得分:0)

这应该适用于任何现代RDBMS ..我在Oracle中测试过..但是应该在SQL * Server等中工作。

  with w_data as (
        select 1234 company_id, 'Test' status, to_date('01-apr-2015','dd-mon-yyyy') ctime from dual union all
        select 1234 company_id, 'Live' status, to_date('30-mar-2015','dd-mon-yyyy') ctime from dual union all
        select 3456 company_id, 'Live' status, to_date('30-jan-2015','dd-mon-yyyy') ctime from dual union all
        select 4567 company_id, 'Test' status, to_date('12-feb-2015','dd-mon-yyyy') ctime from dual union all
        select 3456 company_id, 'Test' status, to_date('15-apr-2015','dd-mon-yyyy') ctime from dual
        ),
     w_sub as (
        select company_id, status, ctime,
                 row_number() over (partition by company_id order by ctime desc)  rnum
          from w_data
        )
  Select company_id, status, ctime
    from w_sub
   where rnum = 1
  /

结果:

  COMPANY_ID STAT CTIME
  ---------- ---- --------------------
        1234 Test 01-apr-2015 00:00:00
        3456 Test 15-apr-2015 00:00:00
        4567 Test 12-feb-2015 00:00:00

  3 rows selected.

答案 1 :(得分:0)

试试这个:

select
 s1.company_id,
 s1.status,
 s1.timestamp
from
 sample s1
JOIN (
 SELECT company_id, max(timestamp) as timestamp
 FROM sample
 GROUP BY company_id) s2
ON s1.company_id = s2.company_id
  AND s1.timestamp= s2.timestamp

答案 2 :(得分:0)

如果您的RDMS支持分析功能(例如Oracle,Postgres,Vertica,......),您可以使用以下内容:

select company_id, status, timestamp from (
    select
    company_id,
    status,
    timestamp,
    row_number() over (partition by company_id order by timestamp desc) as 'n'
    from
    sample
) a where n = 1 

分析函数row_number()根据时间戳(在这种情况下按降序排列)公司明智地对行进行编号,即最新的' timestamp得到1.我们然后使用外部查询并仅获取row_number为1的行。

如果使用mySQL,可以将group_concat()函数与substring_index()

结合使用
select company_id, 
substring_index(group_concat(status order by timestamp desc separator ','), ',', 1), 
substring_index(group_concat(timestamp order by timestamp desc separator ','), ',', 1) 
from sample 
group by company_id

(确保您的RDMS正确处理您的日期格式)

答案 3 :(得分:0)

使用具有first / last_value的Window函数的稍微简单的查询:

SELECT 
company_id, 
last_value(status) OVER (partition by company_id ORDER BY timestamp),
last_value(timestamp) OVER (partition by company_id ORDER BY timestamp)
FROM sample