❶ sql語句獲取表中最新數據
我不知道你的表叫什麼,假如表名叫:A
select * from A where F_energyItemcode='DLEG000024' order by F_endHour DESC
通過上面時間倒序排列之後,將上面查詢結果當成一個表,然後通過rownum=1 去獲取,最新時間的F_Hourvalue值。
select F_Hourvalue from (select * from A where F_energyItemcode='DLEG000024' order by F_endHour DESC) where rownum=1;
❷ sql如何查詢分類最新數據
按照location進行分組group by
排序條件是時間
然後在select中使用資料庫的排名函數,比如rank(),dense_rank構建一個序號,獲取序號為1的就是你想要的記錄。
具體看使用的資料庫
自己試試吧
❸ SQL資料庫,把A表最新數據更新到B表中
這個是考察你的not EXISTS 是否熟練掌握。
正確的語句如下:select * from v_sale_DTL a where not EXISTS (select saleno,rowid from u_sale_dtl_sy where saleno=a.saleno and rowid=a.rowid)
注意:
①第一:from那裡定義了a表,not EXISTS 裡面的from不需要再引用a表,條件裡面可以直接使用a表。
②第二:not EXISTS 裡面,需要哪些條件select哪些條件就行,和正常的select用法一模一樣。寫好以後,意思就是查詢a表,除了這些數據以外。
❹ SQL查詢語句如何查詢最新的數據
--測試數據
declare @t table(id int ,DATA int ,[update] int)
insert into @t select
1, 12, 20080401 union all select
1, 13, 20100501 union all select
1, 15, 20090601 union all select
2, 13 , 20080401 union all select
2 , 4 , 20080904 union all select
3 , 4 , 20090405 union all select
3 , 1 , 20100105
--以下為語句:
select *
from @t a
where not exists (select * from @t b where a.id = b.id and b.[update] > a.[update])
--運行後結果如下
id data update
====================
1 13 20100501
2 4 20080904
3 1 20100105
❺ sql語句,獲取最新記錄。哪一種方式執行效率比較高
第一種方法更加高效,SELECT key, MAX(time) AS maxt FROM A group by key
第二種不足之處是:
你讓資料庫的數據列再做額外運算後輸出,如果data數據量大,經過轉換 ,字元串加減後再輸出,損耗性能及網路傳輸資源,增加伺服器壓力。
❻ sql多條件分組查詢,再求最新數據
--通過開窗函數獲取每個組合最新的記錄
selectdatetime,stationid,itemid,value
from(
selectdatetime,stationid,itemid,value,row_number()partitionby(stationid,itemidorderbydatetimedesc)asflag
from表名)a
whereflag=1
❼ SQL指令如何查詢數據表中最新版本號對應的明細內容
一、數據准備
以Microsoft SQL Server資料庫管理系統為例。
假設對應的數據表名稱為TestTable,表結構如下所示:
CREATETABLETestTable
(
[品牌] VARCHAR(20),
[版本號] INT,
[顏色] VARCHAR(10)
)
添加樣例數據的SQL代碼如下:
INSERTINTOTestTableVALUES('三星',1,'黑')
INSERTINTOTestTableVALUES('三星',1,'白')
INSERTINTOTestTableVALUES('三星',1,'灰')
INSERTINTOTestTableVALUES('三星',1,'藍')
INSERTINTOTestTableVALUES('三星',1,'紅')
INSERTINTOTestTableVALUES('蘋果',1,'黑')
INSERTINTOTestTableVALUES('蘋果',1,'白')
INSERTINTOTestTableVALUES('HTC',1,'黑')
INSERTINTOTestTableVALUES('HTC',1,'白')
INSERTINTOTestTableVALUES('HTC',1,'灰')
INSERTINTOTestTableVALUES('HTC',1,'藍')
INSERTINTOTestTableVALUES('HTC',1,'紅')
INSERTINTOTestTableVALUES('三星',2,'黑')
INSERTINTOTestTableVALUES('三星',2,'白')
INSERTINTOTestTableVALUES('HTC',2,'黑')
INSERTINTOTestTableVALUES('HTC',2,'白')
INSERTINTOTestTableVALUES('HTC',3,'黑')
INSERTINTOTestTableVALUES('HTC',3,'白')
INSERTINTOTestTableVALUES('HTC',3,'灰')
INSERTINTOTestTableVALUES('HTC',3,'藍')
INSERTINTOTestTableVALUES('HTC',3,'紅')
二、思路
查詢所有數據行,對結果集按「品牌」分組,比較分組後的每行的版本號是否是同一品牌的最新版本號,若不是則從結果集中剔除。
GROUPBY分組列
HAVING分組後的條件子句
三、實現步驟
完整的SQL代碼如下:
SELECT t1.[品牌],t1.[版本號],t1.[顏色]
FROM TestTablet1
GROUPBY t1.[品牌],t1.[版本號],t1.[顏色]
HAVING t1.[版本號]=( SELECT MAX(t2.[版本號])
FROM TestTablet2
WHERE t1.[品牌]=t2.[品牌])
四、運行測試
運行結果:
品牌版本號顏色
-----------------------------------------
蘋果1白
蘋果1黑
三星2白
三星2黑
HTC3白
HTC3黑
HTC3紅
HTC3灰
HTC3藍
(9行受影響)
❽ 取資料庫中表的最新的數據的SQL語句應該是怎麼樣的呢
如果有時間特徵的話
Select top 1 *
from ...
Order by datetimefield desc
❾ SQL server 更新表中最新數據的問題
寫觸發器寫了個,不知道好不好使,我仍然建議去後台寫代碼更好點。
create trigger tg_test on table
for insert
as
begin
declare @a1 datetime,@b1 datetime,@c1 datetime,@a2 datetime,@b2 datetime,@c2 datetime
set @a1=(select A1 from inserted)
set @b1=(select B1 from inserted)
set @c1=(select C1 from inserted)
set @a2=(select A2 from inserted)
set @b2=(select B2 from inserted)
set @c2=(select C2 from inserted)
declare @d1 datetime,@d2 datetime
set @d1=(
select top 1 f from(
select @a1 f
union all
select @b1 f
union all
select @c1 f
) a
order by f desc)
set @d2=(
select top 1 f from(
select @a2 f
union all
select @b2 f
union all
select @c2 f
) a
order by f desc)
update table set D1=@d1 where A1=@a1 and B1=@b1 and C1=@c1
update table set D2=@d2 where A2=@a2 and B2=@b2 and C2=@c2
end
❿ SQL怎麼取最新的一個季度數據
您好!這是我寫的SQL。因為沒有看到這個表的表名,所以我就用TEMPTABLE作為表名來寫的,你在使用時直接將其替換為這個表的實際表名,再執行一下,看看是否能滿足要求。
您好!因為這個平台可能有審查,我直接將SQL貼在這里無法發布。要麼麻煩您私信聯系我一下。我把SQL發給您。或者就是麻煩您按照上面的截圖自己手動打出來驗證一下。