Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Please help me with first day of month value

$
0
0
Hallo,I have a table as below.Timestamp SourceId ChannelId Value2015-11-01 16:45:59.953 1 1 223,49232015-11-01 16:46:00.127 1 2 224,9357 *2015-11-01 16:46:00.327 1 3 227,4183 *2015-11-01 16:46:00.527 1 4 221,025 *2015-11-01 13:17:14.177 1 1 223,0304 *I only want the first value per channel in a new table ( the values marked with *)I tried with sql below without succes.set nocount on;declare @today date; set @today = CURRENT_TIMESTAMP;Select * into new_table from ( select @today as 'Now', dateadd(day, -(day(@today)) + 1, @today) as 'FirstOfMonth, Timestamp, SourceId, ChannelId, Unit, Value, DeltaValue, SourceTag, ChannelTag';if day(@today) = 1 select Top 10 * from RecordedValues where Timestamp >= DATEADD(month, -1, @today) and Timestamp < @todayorder by Timestamp;elseselect Top 10 * from RecordedValues where Timestamp >= dateadd(day, -(day(@today)) + 1, @today) and Timestamp < @today order by Timestamp;) as MaandtabelBest Regards Erwin

Viewing all articles
Browse latest Browse all 3145

Trending Articles