0

日時タイプのフィールドを持つデータテーブルを取得し、毎分3〜5エントリのようなデータを取得しました。

23/08/2012 18:05:01,
23/08/2012 18:05:15,
23/08/2012 18:05:26,
23/08/2012 18:05:44,
23/08/2012 18:05:59

今、私は5分ごとに1つのレコードのデータを取得する必要があります

誰かが私を助けてくれますか私はこれをどのように行うことができますか

ありがとう

4

3 に答える 3

4

要件を完全に理解しているかどうかはわかりませんが、以下は、分が5の倍数である任意の値に対して1つのレコードを返します。

select *
from
(
    select *, 
       datepart(minute, yourDate) mn, 
       row_number() over(partition by datepart(minute, yourDate) order by yourDate) rn
    from yourTable
) x
where (mn % 5) = 0
    and rn = 1

SQL FiddlewithDemoを参照してください

于 2012-08-23T17:26:40.417 に答える
0

Given a table that looks something like this:

create table some_event_log_table
(
  id             int      not null identity(1,1) primary key clustered ,
  dateTimeLogged datetime not null ,
  ... some more columns here --
)

Then a query like the following (SqlFiddle at http://sqlfiddle.com/#!3/5ad56/1/0) should do the trick:

select dateLogged    = convert(date,dateTimeLogged),
       periodID      = datediff(minute,
                         convert(date,dateTimeLogged) ,
                         dateTimeLogged
                         ) / 5   ,
       period_start  = convert(time,
                         dateadd(minute,
                           datediff(minute,
                             convert(date,dateTimeLogged),
                             dateTimeLogged
                             ) ,
                           convert(datetime,convert(date,dateTimeLogged))
                           )
                         ) ,
       events_logged = count(*)
from some_event_log_table
group by convert(date,dateTimeLogged),
         datediff(minute,
           convert(date,dateTimeLogged) ,
           dateTimeLogged
           ) / 5   ,
         convert(time,
           dateadd(minute,
             datediff(minute,
               convert(date,dateTimeLogged),
               dateTimeLogged
               ) ,
             convert(datetime,convert(date,dateTimeLogged))
             )
           )

What we are 9doing here is the following.

  • First, we divide the day up into periods of 5 minutes each. There are 288 such 5 minute periods every day.

  • Then, for each row in the table, we

    • Compute the column dateLogged, the reporting date (sans time) for the row.

    • Compute the column periodID, a value from 0-287, indicating which of the 288 5-minute periods per day the row falls into.

    • Compute the column periodStart, the start time of the period the row falls into.

  • Finally, we

    • Summarize with group by, creating 1 group for each period within a day, grouping on the 3 values we just computed for each row.
    • Order the result set with order by on the columns dateLogged and periodID.

That's about all there is to it. Really quite simple, once you break it down.

于 2012-08-23T19:21:18.793 に答える