数据库如下
站号 时间 温度
58549 1111 26
58549 1112 27
58549 1113 28
58557 1111 21
58557 1112 25
58557 1113 32
58558 1111 25
58558 1112 19
58558 1113 12
想查询结果如下:
站号 时间 最高温度
58549 1113 28
58557 1113 32
58558 1111 25请问查询语句该怎么写?
站号 时间 温度
58549 1111 26
58549 1112 27
58549 1113 28
58557 1111 21
58557 1112 25
58557 1113 32
58558 1111 25
58558 1112 19
58558 1113 12
想查询结果如下:
站号 时间 最高温度
58549 1113 28
58557 1113 32
58558 1111 25请问查询语句该怎么写?
from table1 a
where not exists ( select 1
from table1
where 站号=a.站号
and 温度>a.温度)
站号 时间 最高温度select a,max(b),max(c) from 表 group by a
-- Author :SQL77(只为思齐老)
-- Date :2010-01-24 20:49:29
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([站号] int,[时间] int,[温度] int)
insert #TB
select 58549,1111,26 union all
select 58549,1112,27 union all
select 58549,1113,28 union all
select 58557,1111,21 union all
select 58557,1112,25 union all
select 58557,1113,32 union all
select 58558,1111,25 union all
select 58558,1112,19 union all
select 58558,1113,12
--------------开始查询--------------------------
SELECT T.* FROM #TB T,
(
SELECT 站号,MAX(温度)最高温度 FROM #TB GROUP BY 站号
) T1
WHERE T.站号=T1.站号 AND T1.最高温度=T.温度
----------------结果----------------------------
/* (所影响的行数为 9 行)站号 时间 温度
----------- ----------- -----------
58558 1111 25
58557 1113 32
58549 1113 28(所影响的行数为 3 行)
*/
from table1 a inner join (select 站号,max(温度) as 温度 from table1 group by 站号) b
on a.站号=b.站号 and a.温度=b.温度
-- Author :SQL77(只为思齐老)
-- Date :2010-01-24 20:49:29
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([站号] int,[时间] int,[温度] int)
insert #TB
select 58549,1111,26 union all
select 58549,1112,27 union all
select 58549,1113,28 union all
select 58557,1111,21 union all
select 58557,1112,25 union all
select 58557,1113,32 union all
select 58558,1111,25 union all
select 58558,1112,19 union all
select 58558,1113,12
--------------开始查询--------------------------
SELECT T.* FROM #TB T WHERE 温度=
(
SELECT MAX(温度)最高温度 FROM #TB
WHERE 站号=T.站号 )
----------------结果----------------------------
/* (所影响的行数为 9 行)站号 时间 温度
----------- ----------- -----------
58558 1111 25
58557 1113 32
58549 1113 28(所影响的行数为 3 行)
*/