22,207
社区成员
发帖
与我相关
我的任务
分享
declare @begin datetime
declare @end datetime --结束时间
declare @stcd char(8)
set @begin='2010-5-20 15:15:54'
set @end='2010-7-20 15:16:13'
set @stcd='90800001'
--AS
--begin
select a.stcd,b.h1yp,convert(varchar(10),a.TM,20) datadate
from st_rain_s a inner join
(select stcd,max(p8) h1yp,convert(varchar(10),TM,20) dt1 from
(
select stcd,p8,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p9,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p10,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p11,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p12,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p13,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p14,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p15,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p16,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p17,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p18,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p19,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p20,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p21,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p22,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p23,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p0,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p1,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p2,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p3,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p4,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p5,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p6,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p7,convert(varchar(10),TM,20) dt1 from st_rain_s
) tb1 group by tb1.stcd
) b on b.stcd=a.stcd where a.stcd=@stcd and TM between @begin and @end
group by a.stcd,b.h1yp,convert(varchar(10),a.TM,20)
GO
--> 生成测试数据表: [st_rain_s]
IF OBJECT_ID('[st_rain_s]') IS NOT NULL
DROP TABLE [st_rain_s]
GO
create table st_rain_s
(
stcd char(8)
,TM datetime
,DYP float
,p8 float
,p9 float
,p10 float
,p11 float
)
insert st_rain_s
select '90800001','2010-7-8 21:02:58',10,1,2,3,4
union all
select '90800001','2010-7-9 21:02:58',20,10,3,3,4
-->SQL查询如下:
DECLARE @stcd VARCHAR(8)
DECLARE @begin DATETIME
DECLARE @end DATETIME
SET @stcd = '90800001'
SET @begin = '2010-7-7 20:07:44'
SET @end = '2010-7-9 21:07:52'
SELECT stcd, tm, (
SELECT MAX(p)
FROM (
SELECT p = p8 UNION SELECT p9 UNION SELECT p10 UNION SELECT p11
) a
) AS maxp
FROM (
SELECT stcd, CONVERT(VARCHAR(10), TM, 20) tm, MAX(p8) p8, MAX(p9) p9, MAX(p10) p10, MAX(p11) p11
FROM st_rain_s
WHERE stcd=@stcd AND TM BETWEEN @begin AND @end
GROUP BY stcd, CONVERT(VARCHAR(10), TM, 20)
) as t
/*
stcd tm maxp
-------- ---------- ----------------------
90800001 2010-07-08 4
90800001 2010-07-09 10
(2 行受影响)
*/
这样就不错了。我在2008下执行的。DECLARE @st_rain_s table
(
stcd char(8)
,TM datetime
,DYP float
,p8 float
,p9 float
,p10 float
,p11 float
)
select * from @st_rain_s
insert @st_rain_s
select '90800001','2010-7-8 21:02:58',10,1,2,3,4
union all
select '90800001','2010-7-9 21:02:58',20,10,3,3,4
--测试
declare @stcd char(8)
declare @begin datetime
declare @end datetime
set @stcd='90800001'
set @begin='2010-7-7 20:07:44'
set @end='2010-7-9 21:07:52'
SELECT stcd,TM,MAX(P8) FROM
(
SELECT stcd,TM,P8 FROM @st_rain_s
UNION ALL
SELECT stcd,TM,P9 FROM @st_rain_s
UNION ALL
SELECT stcd,TM,P10 FROM @st_rain_s
UNION ALL
SELECT stcd,TM,P11 FROM @st_rain_s
)T
GROUP BY STCD,TM
/*stcd TM DYP p8 p9 p10 p11
-------- ------------------------------------------------------ ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- -----------------------------------------------------
(所影响的行数为 0 行)
(所影响的行数为 2 行)
stcd TM
-------- ------------------------------------------------------ -----------------------------------------------------
90800001 2010-07-08 21:02:58.000 4.0
90800001 2010-07-09 21:02:58.000 10.0
(所影响的行数为 2 行)
--> 生成测试数据表: [st_rain_s]
IF OBJECT_ID('[st_rain_s]') IS NOT NULL
DROP TABLE [st_rain_s]
GO
create table st_rain_s
(
stcd char(8)
,TM datetime
,DYP float
,p8 float
,p9 float
,p10 float
,p11 float
)
insert st_rain_s
select '90800001','2010-7-8 21:02:58',10,1,2,3,4
union all
select '90800001','2010-7-9 21:02:58',20,10,3,3,4
-->SQL查询如下:
DECLARE @stcd VARCHAR(8)
DECLARE @begin DATETIME
DECLARE @end DATETIME
SET @stcd = '90800001'
SET @begin = '2010-7-7 20:07:44'
SET @end = '2010-7-9 21:07:52'
;WITH t AS
(
SELECT stcd, CONVERT(VARCHAR(10), TM, 20) tm, MAX(p8) p8, MAX(p9) p9, MAX(p10) p10, MAX(p11) p11
FROM st_rain_s
WHERE stcd=@stcd AND TM BETWEEN @begin AND @end
GROUP BY stcd, CONVERT(VARCHAR(10), TM, 20)
)
SELECT stcd, tm, (
SELECT MAX(p)
FROM (
SELECT p = p8 UNION SELECT p9 UNION SELECT p10 UNION SELECT p11
) a
) AS maxp
FROM t
/*
stcd tm maxp
-------- ---------- ----------------------
90800001 2010-07-08 4
90800001 2010-07-09 10
(2 行受影响)
*/
insert st_rain_s
select '90800001','2010-7-8 21:02:58',10,1,2,3,4
union all
select '90800001','2010-7-9 21:02:58',20,10,3,3,4
create database maxColumn
use maxColumn
create table st_rain_s
(
stcd char(8)
,TM datetime
,DYP float
,p8 float
,p9 float
,p10 float
,p11 float
)
select * from st_rain_s
insert st_rain_s
select '90800001','2010-7-8 21:02:58',10,1,2,3,4
union all
select '90800001','2010-7-9 21:02:58',20,10,3,3,4
--测试
declare @stcd char(8)
declare @begin datetime
declare @end datetime
set @stcd='90800001'
set @begin='2010-7-7 20:07:44'
set @end='2010-7-9 21:07:52'
select a.stcd,b.h1yp,convert(varchar(10),a.TM,20) datadate
from st_rain_s a inner join
(select stcd,max(p8) h1yp,convert(varchar(10), dt1 ,20) dt1 from
(
select stcd,p8,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p9,convert(varchar(10),TM,20) dt1 from st_rain_s union all select stcd,p10,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p11,convert(varchar(10),TM,20) dt1 from st_rain_s
) tb1 group by tb1.stcd,tb1.dt1
) b on b.stcd=a.stcd where a.stcd=@stcd and TM between @begin and @end
group by a.stcd,b.h1yp,convert(varchar(10),a.TM,20)
--数据显示如下:
4.0 2010-07-08 90800001
4.0 2010-07-09 90800001
10.0 2010-07-08 90800001
10.0 2010-07-09 90800001
--我需要下列数据
4.0 2010-07-08 90800001
10.0 2010-07-09 90800001
--> 生成测试数据表: [st_rain_s]
IF OBJECT_ID('[st_rain_s]') IS NOT NULL
DROP TABLE [st_rain_s]
GO
CREATE TABLE [st_rain_s] ([stcd] [nvarchar](10),[tm] [datetime],[dyp] [nvarchar](10),[p8] [int],[p9] [int],[p10] [int],[p5] [int],[p6] [int],[p7] [int])
INSERT INTO [st_rain_s]
SELECT 'a','20100101','a','1','2','3','4','5','3' UNION ALL
SELECT 'a','20100101','a','1','2','3','6','5','3' UNION ALL
SELECT 'b','20100103','b','11','22','3','4','5','3'
--SELECT * FROM [st_rain_s]
-->SQL查询如下:
;WITH t AS
(
SELECT stcd, CONVERT(VARCHAR(10), TM, 20) tm, MAX(p8) p8, MAX(p9) p9, MAX(p10) p10, MAX(p5) p5,
MAX(p6) p6, MAX(p7) p7
FROM st_rain_s
GROUP BY stcd, CONVERT(VARCHAR(10), TM, 20)
)
SELECT stcd, tm, (
SELECT MAX(p)
FROM (
SELECT p = p8 UNION SELECT p9 UNION SELECT p10 UNION SELECT p5 UNION SELECT p6
UNION SELECT p7
) a
) AS maxp
FROM t
/*
stcd tm maxp
---------- ---------- -----------
a 2010-01-01 6
b 2010-01-03 22
(2 行受影响)
*/
;with t as
(
select stcd,convert(varchar(10),a.TM,20) tm,max(p8) p8,max(p9) p9,max(p10) p10,max(p5) p5,max(p6) p6,max(p7) p7
from st_rain_s
group by stcd,convert(varchar(10),a.TM,20)
)
select stcd,tm,(select max(p) from (select p=p8 union select p9 union select p10 union select p5 union select p6 union select p7) a) as maxp
from t
create table st_rain_s
(
stcd char(8)
,TM datetime
,DYP float
,p8 float
.
.--这里字段为 p9,p10,....,p5,p6
.
,p7 float
)
declare @begin datetime
declare @end datetime --结束时间
declare @stcd char(8)
set @begin='2010-5-20 15:15:54'
set @end='2010-7-20 15:16:13'
set @stcd='90800001'
--AS
--begin
select a.stcd,b.h1yp,convert(varchar(10),a.TM,20) datadate
from st_rain_s a inner join
(
select stcd,max(p8) h1yp,TM from
(
select stcd,p8,convert(varchar(10),TM,20) TM from st_rain_s
union all select stcd,p9,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p10,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p11,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p12,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p13,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p14,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p15,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p16,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p17,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p18,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p19,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p20,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p21,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p22,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p23,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p0,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p1,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p2,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p3,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p4,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p5,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p6,convert(varchar(10),TM,20) dt1 from st_rain_s
union all select stcd,p7,convert(varchar(10),TM,20) dt1 from st_rain_s
) tb1
group by stcd,TM
) b
on b.stcd=a.stcd AND convert(varchar(10),a.TM,20)=b.TM
where a.stcd=@stcd and a.TM between @begin and @end
group by a.stcd,b.h1yp,convert(varchar(10),a.TM,20)
GO
语法这个应该是没问题的