0

顧客がビデオを見る時間を節約するテーブルがあります。最も多くの動画が再生されるピーク時間帯とそのピーク数を知りたいです。データベース内のすべての日について、このデータが必要です。どうすればこれを達成できますか?

max および count 関数と内部結合を使用して 1 つのレコードしか取得できませんが、毎日のピーク ボリュームと時間数が必要です。

4

2 に答える 2

2

構文はデータベースによって異なります。また、あなたの質問は漠然としています。「特定の日のピーク時」ではなく、「日を合わせたピーク時」と解釈しています。

以下はほぼ ANSI 構文です。

select *
from (select extract(hour from ViewTime) as hr, count(*) as cnt
      from VideoViews
      group by extract(hour from ViewTime)
     ) t
order by cnt desc
limit 1

一部のデータベースでは、日付から時間を取得するために「datepart()」または「to_char()」を使用している場合があります。一部のデータベースでは、「limit 1」ではなく「top (1)」または「rownum = 1」を使用する場合があります。ただし、全体的な考え方は同じです。集計して目的の結果を取得し、order by を使用して 1 つの行を選択して最大値を選択します。

于 2012-08-31T22:33:54.153 に答える
0

次のようなテーブル スキーマがあるとします。

create table customer_view_log
(
  customer_id int      not null ,
  dt_viewed   datetime not null ,
  video_id     int     not null ,

  constraint customer_view_log_PK   primary key nonclustered ( customer_id , dt_viewed   ) ,
  constraint customer_view_log_AK01 unique      nonclustered ( dt_viewed   , customer_id ) ,

  constraint customer_view_log_FK01 foreign key ( customer_id ) references customer ( id ) ,
  constraint customer_view_log_FK01 foreign key ( video_id    ) references video    ( id ) ,

)

このようなクエリ

select top 1
       Hour     = datepart(hour,dt_viewed) ,
       Viewings = count(*)
from customer_view_log
group by datepart(hour,dt_viewed)
order by 2 desc

トリックを行う必要があります。上記は SQL Server です。詳細は実装によって異なる場合があります。日付/時刻関連のものは、実装によって大きく異なります。

于 2012-08-31T23:58:06.543 に答える