我想查看最后记录的数据,但我无法弄清楚查询。
这是我现在的查询。它返回本周的所有数据:
SELECT DATENAME(dw, t.date) AS date, kwh AS kwh, time
FROM tbl_totalEnergy t
WHERE t.date > = DATEADD(wk, DATEDIFF(wk, 0, GETDATE() -1), 0)
答案 0 :(得分:0)
如果我理解正确,您可以查询如下:
Select * from (
SELECT DATENAME(dw,t.date) as date, kwh as kwh, time,
RowN = Row_Number() over(partition by Year(t.date), DatePart(wk,t.date), Day(t.date) order by t.date desc)
FROM tbl_totalEnergy t
) a
Where a.RowN = 1
and a.[date] between getdate() and dateadd(dd,-7,getdate())
答案 1 :(得分:0)
尝试如下(我假设Date columnn的类型为Date)
SELECT * FROM(
SELECT ROW_NUMBER() OVER(PARTITION BY t.DATE ORDER BY t.time DESC) RNO
, kwh as kwh, t.time
FROM tbl_totalEnergy t
WHERE t.date > = DATEADD(wk,DATEDIFF(wk,0, GETDATE() -1 ),0)
)A
WHERE RNO=1
答案 2 :(得分:0)
时间在一个单独的栏目中,所以:
SELECT DATENAME(dw, t.date) as date, kwh as kwh, time
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY DATENAME(dw, t.date) ORDER BY t.date DESC, t.time DESC) as seqnum
FROM tbl_totalEnergy t
WHERE t.date >= DATEADD(week, DATEDIFF(week, 0, GETDATE() -1 ), 0)
) t
WHERE seqnum = 1;