2017-08-03 3 views
0

最終日の1日を取得し、その日が変更されるまで別の列に表示しようとしています。以下は私が現在持っているデータのサンプルです。最後の期間を取得して別の列に表示する

DT       no_of_records 
2017-05-01 00:00:00.000  241 
2017-05-01 04:00:00.000  842 
2017-05-01 08:00:00.000  1049 
2017-05-01 12:00:00.000  1517 
2017-05-01 16:00:00.000  1627 
2017-05-01 20:00:00.000  2077 
2017-05-02 00:00:00.000  151 
2017-05-02 04:00:00.000  772 
2017-05-02 08:00:00.000  951 
2017-05-02 12:00:00.000  1114 
2017-05-02 16:00:00.000  1693 
2017-05-02 20:00:00.000  1992 

以下は私の出力です。

DT       no_of_records total 
2017-05-01 00:00:00.000  241    2077 
2017-05-01 04:00:00.000  842    2077 
2017-05-01 08:00:00.000  1049   2077 
2017-05-01 12:00:00.000  1517   2077 
2017-05-01 16:00:00.000  1627   2077 
2017-05-01 20:00:00.000  2077   2077 
2017-05-02 00:00:00.000  151    1992 
2017-05-02 04:00:00.000  772    1992 
2017-05-02 08:00:00.000  951    1992 
2017-05-02 12:00:00.000  1114   1992 
2017-05-02 16:00:00.000  1693   1992 
2017-05-02 20:00:00.000  1992   1992 

合計の列を取得する方法はありますか?

+0

なぜ合計? 1日あたり最大ではありませんか? –

+0

はい、私が取り組んでいるプロジェクトを除いて、私は自分の部分を理解しやすくするために名前を付けました。 – Faheera

+0

[もっと速い回答を得るために、どのような状況で私の質問に「緊急」や他の類似のフレーズを追加することができますか?](// meta.stackoverflow.com/q/326569) - 要約は、これはボランティアに対処する理想的な方法であり、おそらく回答を得ることは非生産的です。これをあなたの質問に追加しないでください。 – halfer

答えて

2

first_valueウィンドウ機能を使用できます。

select dt,no_of_records 
,first_value(no_of_records) over(partition by cast(dt as date) order by dt desc) as toal 
from tbl 
+0

ありがとうございました!これは私のために働く! – Faheera

0

CTEクエリを使用します。結合演算子の日付を整数に変換する必要があるかもしれません。また

with cte_SumPerday 
(
DateOfDay, 
TotalPerDay 
) 
as 
(
select 
cast(dt as date) as DateOfDay, 
max(no_of_records) as TotalPerDay 
group by 
cast(dt as date) 
from transactions 
) 
, 
cte_TransactionsAndDays 
(
dt, 
nr_of_records, 
DateOfDay 
) 
as 
(
select 
*, 
cast(DT as date) as DateOfDay 
from transactions 
) 

select 
* 
from cte_TransactionsAndDays T 
join cte_SumPerday S on 
S.DayofDate = T.DayOfDate 
0

、あなたはサブクエリをやって試すことができます:

select dt, no_of_records, total = (select max(no_of_records) from table1 
            where convert(date,dt) = convert(date,t1.dt)) 
from table1 as t1 
関連する問題