请大侠们帮我看看下面这个语句有什么问题?

youzhj 2010-10-20 08:57:29
select Rdt,sHour=case 
when (a.Hours-(select max(Hours) from AirRecord where Kid=a.Kid and Rdt<a.Rdt))>24
then 24+(select max(Hours) from AirRecord where Kid=a.Kid and Rdt<a.Rdt)
else a.Hours end,a.Kid
from AirRecord as a
where Kid=110
and Rdt>'2010-8-31' and a.Rdt<'2010-10-1'
and datepart(hh,a.Rdt)=7
...全文
123 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 sqlcenter 的回复:]
这样left join不用子查询可能效率高点


SQL code
select a.Rdt,
(case when a.Hours-b.Hours>24 then b.Hours+24 else a.Hours end) as Hours, a.Kid
from # a left join # b on a.Kid=b.Kid and a.Rdt=b.Rdt+1
where……
[/Quote]太谢谢你了,这个我也测试测试去。
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
这样left join不用子查询可能效率高点

select a.Rdt,
(case when a.Hours-b.Hours>24 then b.Hours+24 else a.Hours end) as Hours, a.Kid
from # a left join # b on a.Kid=b.Kid and a.Rdt=b.Rdt+1
where a.Kid=110 and a.Rdt>'2010-8-31' and a.Rdt<'2010-10-1'
and datepart(hh,a.Rdt)=7
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 sqlcenter 的回复:]
SQL code
--这个是点对点的比较
select Rdt,sHour=case
when (a.Hours-(select Hours from # where Kid=a.Kid and Rdt+1=a.Rdt))>24
then 24+(select Hours from # where Kid=a.Kid and Rdt+1=a.Rdt)
else a.Hours end,……
[/Quote]看来是我没弄清楚点对点的概念,真是对不起。你给的点对点的语句是我想要的,谢谢你,马上结贴,这是高手啊。
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 sqlcenter 的回复:]
SQL code
--这个是点对点的比较
select Rdt,sHour=case
when (a.Hours-(select Hours from # where Kid=a.Kid and Rdt+1=a.Rdt))>24
then 24+(select Hours from # where Kid=a.Kid and Rdt+1=a.Rdt)
else a.Hours end,……
[/Quote]我试试看,真是谢谢你啊。
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
--这个是点对点的比较
select Rdt,sHour=case
when (a.Hours-(select Hours from # where Kid=a.Kid and Rdt+1=a.Rdt))>24
then 24+(select Hours from # where Kid=a.Kid and Rdt+1=a.Rdt)
else a.Hours end,a.Kid
from # as a
where Kid=110
and Rdt>'2010-8-31' and a.Rdt<'2010-10-1'
and datepart(hh,a.Rdt)=7

-- 这个是今天7点跟昨天最大的Hours值比较
select Rdt,sHour=case
when (a.Hours-(select max(Hours) from # where Kid=a.Kid and datediff(day,Rdt,a.Rdt)=1))>24
then 24+(select max(Hours) from # where Kid=a.Kid and datediff(day,Rdt,a.Rdt)=1)
else a.Hours end,a.Kid
from # as a
where Kid=110
and Rdt>'2010-8-31' and a.Rdt<'2010-10-1'
and datepart(hh,a.Rdt)=7
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 sqlcenter 的回复:]
确认一件事,你是今天的7点要跟昨天的7点相比较,还是?
[/Quote]是的,今天减去昨天的7点值之差不大于24。
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
确认一件事,你是今天的7点要跟昨天的7点相比较,还是?
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
哦,你的语句是逻辑有问题。
== 我写一个
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sqlcenter 的回复:]
没错的啊,我用你的语句跟我语句比较,双向 except 都是空集,证明产生的结果集完全相同。
[/Quote]老大,您在看看您6楼那个结果,Hours字段和newHours字段是不一样的啊,我就是想要newHours字段的结果。
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
没错的啊,我用你的语句跟我语句比较,双向 except 都是空集,证明产生的结果集完全相同。
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sqlcenter 的回复:]
2000你原来那条语句有什么问题?
效率?
[/Quote]原来那个得不到我要的结果,我要结果其实就是6楼的结果,但是你那是2005的语法,我这2000用不了。
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
2000你原来那条语句有什么问题?
效率?
youzhj 2010-10-20
  • 打赏
  • 举报
回复
我用的是SQL2000版本,帮帮忙啊,谢谢了。
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sqlcenter 的回复:]
SQL code
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(Rdt datetime, Hours float, Kid int)
insert into #
select '2010-08-31 07:00:00.000', 1095.2502440000001, 11……
[/Quote]能给个sql2000的句子么?
youzhj 2010-10-20
  • 打赏
  • 举报
回复

时间 Hours Kid 差值
2010-8-31 7:00 1095.250244 110
2010-9-1 7:00 1119.533569 110 24.283325
2010-9-2 7:00 1143.300293 110 23.766724
2010-9-3 7:00 1167.483643 110 24.18335
2010-9-4 7:00 1191.250366 110 23.766723
2010-9-5 7:00 1215.533691 110 24.283325
2010-9-6 7:00 1239.283691 110 23.75
2010-9-7 7:00 1263.467041 110 24.18335
2010-9-8 7:00 1287.233643 110 23.766602
2010-9-9 7:00 1311.516968 110 24.283325
2010-9-10 7:00 1335.283691 110 23.766723
2010-9-11 7:00 1359.467041 110 24.18335
2010-9-12 7:00 1383.233643 110 23.766602
2010-9-13 7:00 1407.516968 110 24.283325
2010-9-14 7:00 1431.266968 110 23.75
2010-9-15 7:00 1455.450317 110 24.183349
2010-9-16 7:00 1479.217041 110 23.766724
2010-9-17 7:00 1503.500366 110 24.283325
2010-9-18 7:00 1527.250366 110 23.75
2010-9-19 7:00 1551.450317 110 24.199951
2010-9-20 7:00 1575.217041 110 23.766724
2010-9-21 7:00 1599.500366 110 24.283325
2010-9-22 7:00 1623.233765 110 23.733399
2010-9-23 7:00 1647.450317 110 24.216552
2010-9-24 7:00 1671.217041 110 23.766724
2010-9-25 7:00 1695.500366 110 24.283325
2010-9-26 7:00 1719.233765 110 23.733399
2010-9-27 7:00 1743.450439 110 24.216674
2010-9-28 7:00 1767.217041 110 23.766602
2010-9-29 7:00 1791.500366 110 24.283325
2010-9-30 7:00 1815.217041 110 23.716675

我想让上面那些差值都小于等于24,该怎么处理呢?
SQLCenter 2010-10-20
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(Rdt datetime, Hours float, Kid int)
insert into #
select '2010-08-31 07:00:00.000', 1095.2502440000001, 110 union all
select '2010-09-01 07:00:00.000', 1119.5335689999999, 110 union all
select '2010-09-02 07:00:00.000', 1143.300293, 110 union all
select '2010-09-03 07:00:00.000', 1167.483643, 110 union all
select '2010-09-04 07:00:00.000', 1191.250366, 110 union all
select '2010-09-05 07:00:00.000', 1215.5336910000001, 110 union all
select '2010-09-06 07:00:00.000', 1239.2836910000001, 110 union all
select '2010-09-07 07:00:00.000', 1263.4670410000001, 110 union all
select '2010-09-08 07:00:00.000', 1287.233643, 110 union all
select '2010-09-09 07:00:00.000', 1311.5169679999999, 110 union all
select '2010-09-10 07:00:00.000', 1335.2836910000001, 110 union all
select '2010-09-11 07:00:00.000', 1359.4670410000001, 110 union all
select '2010-09-12 07:00:00.000', 1383.233643, 110 union all
select '2010-09-13 07:00:00.000', 1407.5169679999999, 110 union all
select '2010-09-14 07:00:00.000', 1431.2669679999999, 110 union all
select '2010-09-15 07:00:00.000', 1455.450317, 110 union all
select '2010-09-16 07:00:00.000', 1479.2170410000001, 110 union all
select '2010-09-17 07:00:00.000', 1503.500366, 110 union all
select '2010-09-18 07:00:00.000', 1527.250366, 110 union all
select '2010-09-19 07:00:00.000', 1551.450317, 110 union all
select '2010-09-20 07:00:00.000', 1575.2170410000001, 110 union all
select '2010-09-21 07:00:00.000', 1599.500366, 110 union all
select '2010-09-22 07:00:00.000', 1623.2337649999999, 110 union all
select '2010-09-23 07:00:00.000', 1647.450317, 110 union all
select '2010-09-24 07:00:00.000', 1671.2170410000001, 110 union all
select '2010-09-25 07:00:00.000', 1695.500366, 110 union all
select '2010-09-26 07:00:00.000', 1719.2337649999999, 110 union all
select '2010-09-27 07:00:00.000', 1743.450439, 110 union all
select '2010-09-28 07:00:00.000', 1767.2170410000001, 110 union all
select '2010-09-29 07:00:00.000', 1791.500366, 110 union all
select '2010-09-30 07:00:00.000', 1815.2170410000001, 110

;with cte as
(
select id=row_number()over(order by Rdt),* from #
)
select a.*,
newhours = case when a.Hours-b.Hours>24 then b.Hours+24 else a.Hours end
from cte a left join cte b on a.id=b.id+1
/*
id Rdt Hours Kid newhours
-------------------- ----------------------- ---------------------- ----------- ----------------------
1 2010-08-31 07:00:00.000 1095.250244 110 1095.250244
2 2010-09-01 07:00:00.000 1119.533569 110 1119.250244
3 2010-09-02 07:00:00.000 1143.300293 110 1143.300293
4 2010-09-03 07:00:00.000 1167.483643 110 1167.300293
5 2010-09-04 07:00:00.000 1191.250366 110 1191.250366
6 2010-09-05 07:00:00.000 1215.533691 110 1215.250366
7 2010-09-06 07:00:00.000 1239.283691 110 1239.283691
8 2010-09-07 07:00:00.000 1263.467041 110 1263.283691
9 2010-09-08 07:00:00.000 1287.233643 110 1287.233643
10 2010-09-09 07:00:00.000 1311.516968 110 1311.233643
11 2010-09-10 07:00:00.000 1335.283691 110 1335.283691
12 2010-09-11 07:00:00.000 1359.467041 110 1359.283691
13 2010-09-12 07:00:00.000 1383.233643 110 1383.233643
14 2010-09-13 07:00:00.000 1407.516968 110 1407.233643
15 2010-09-14 07:00:00.000 1431.266968 110 1431.266968
16 2010-09-15 07:00:00.000 1455.450317 110 1455.266968
17 2010-09-16 07:00:00.000 1479.217041 110 1479.217041
18 2010-09-17 07:00:00.000 1503.500366 110 1503.217041
19 2010-09-18 07:00:00.000 1527.250366 110 1527.250366
20 2010-09-19 07:00:00.000 1551.450317 110 1551.250366
21 2010-09-20 07:00:00.000 1575.217041 110 1575.217041
22 2010-09-21 07:00:00.000 1599.500366 110 1599.217041
23 2010-09-22 07:00:00.000 1623.233765 110 1623.233765
24 2010-09-23 07:00:00.000 1647.450317 110 1647.233765
25 2010-09-24 07:00:00.000 1671.217041 110 1671.217041
26 2010-09-25 07:00:00.000 1695.500366 110 1695.217041
27 2010-09-26 07:00:00.000 1719.233765 110 1719.233765
28 2010-09-27 07:00:00.000 1743.450439 110 1743.233765
29 2010-09-28 07:00:00.000 1767.217041 110 1767.217041
30 2010-09-29 07:00:00.000 1791.500366 110 1791.217041
31 2010-09-30 07:00:00.000 1815.217041 110 1815.217041
*/
youzhj 2010-10-20
  • 打赏
  • 举报
回复
表AirRecord(Rdt,Hours,Kid),其中Hours字段是float型的,我想查询每天7点的数据,并且须保证所有相邻两天的Hours值之差不大于24。如果差值大于24的话,就让后一日的Hours=前一日Hours+24。
原始数据:
2010-08-31 07:00:00.000	1095.2502440000001	110
2010-09-01 07:00:00.000 1119.5335689999999 110
2010-09-02 07:00:00.000 1143.300293 110
2010-09-03 07:00:00.000 1167.483643 110
2010-09-04 07:00:00.000 1191.250366 110
2010-09-05 07:00:00.000 1215.5336910000001 110
2010-09-06 07:00:00.000 1239.2836910000001 110
2010-09-07 07:00:00.000 1263.4670410000001 110
2010-09-08 07:00:00.000 1287.233643 110
2010-09-09 07:00:00.000 1311.5169679999999 110
2010-09-10 07:00:00.000 1335.2836910000001 110
2010-09-11 07:00:00.000 1359.4670410000001 110
2010-09-12 07:00:00.000 1383.233643 110
2010-09-13 07:00:00.000 1407.5169679999999 110
2010-09-14 07:00:00.000 1431.2669679999999 110
2010-09-15 07:00:00.000 1455.450317 110
2010-09-16 07:00:00.000 1479.2170410000001 110
2010-09-17 07:00:00.000 1503.500366 110
2010-09-18 07:00:00.000 1527.250366 110
2010-09-19 07:00:00.000 1551.450317 110
2010-09-20 07:00:00.000 1575.2170410000001 110
2010-09-21 07:00:00.000 1599.500366 110
2010-09-22 07:00:00.000 1623.2337649999999 110
2010-09-23 07:00:00.000 1647.450317 110
2010-09-24 07:00:00.000 1671.2170410000001 110
2010-09-25 07:00:00.000 1695.500366 110
2010-09-26 07:00:00.000 1719.2337649999999 110
2010-09-27 07:00:00.000 1743.450439 110
2010-09-28 07:00:00.000 1767.2170410000001 110
2010-09-29 07:00:00.000 1791.500366 110
2010-09-30 07:00:00.000 1815.2170410000001 110
abuying 2010-10-20
  • 打赏
  • 举报
回复
--isnull ,使用别名
select a.Rdt,sHour=case
when (a.Hours-isnull((select max(Hours) from AirRecord b where b.Kid=a.Kid and Rdt<a.Rdt),0))>24
then 24+isnull((select max(Hours) from AirRecord c where c.Kid=a.Kid and Rdt<a.Rdt),0)
else a.Hours end,a.Kid
from AirRecord as a
where Kid=110
and a.Rdt>'2010-8-31' and a.Rdt<'2010-10-1'
and datepart(hh,a.Rdt)=7
youzhj 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
SQL code
select Rdt,sHour=(
case
when (a.Hours-(select max(Hours) from AirRecord where Kid=a.Kid and Rdt<a.Rdt))>24
then 24+(select max(Hours) from AirRecord where Kid=a.Kid and Rdt<a.Rdt)
else ……
[/Quote]谢谢老大提醒啊,我其实知道发帖规矩,只是对这个问题迫不及待,所以先发上来了,由于数据有点多,现在正在整理,等一下就发上数据和想要的结果来。
dawugui 2010-10-20
  • 打赏
  • 举报
回复
select Rdt,sHour=(
case
when (a.Hours-(select max(Hours) from AirRecord where Kid=a.Kid and Rdt<a.Rdt))>24
then 24+(select max(Hours) from AirRecord where Kid=a.Kid and Rdt<a.Rdt)
else a.Hours end),
a.Kid
from AirRecord as a
where Kid=110
and Rdt>'2010-8-31' and a.Rdt<'2010-10-1'
and datepart(hh,a.Rdt)=7


最好给出完整的表结构,测试数据,计算方法和正确结果.


发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281

加载更多回复(1)
JAVA相关基础知识 1、面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。 2.继承: 继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。 3.封装: 封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。 4. 多态性: 多态性是指允许不同类的对象对同一消息作出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。 2、String是最基本的数据类型吗? 基本数据类型包括byte、int、char、long、float、double、boolean和short。 java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。 原始类型封装类 booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble 引用类型和原始类型的行为完全不同,并且它们具有不同的语义。引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始类型实例变量的缺省值与它们的类型有关。 4、String 和StringBuffer的区别 JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据。这个String类提供了数值不可改变的字符串。而这个StringBuffer类提供的字符串进行修改。当你知道字符数据要改变的时候你就可以使用StringBuffer。典型地,你可以使用StringBuffers来动态构造字符数据。 5、运行时异常与一般异常有何异同? 异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误。java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 6、说出Servlet的生命周期,并说出Servlet和CGI的区别。 Servlet被服务器实例化后,容器运行其init方法,求到达时运行其service方法,service方法自动派遣运行与求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。 与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个求,并且其实例一般不会销毁,而CGI对每个求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。 7、说出ArrayList,Vector, LinkedList的存储性能和特性 ArrayList和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢,Vector由于使用了synchronized方法(线程安全),通常性能上较ArrayList差,而LinkedList使用双向链表实现存储,按序号索引数据需要进行前向或后向遍历,但是插入数据时只需要记录本项的前后项即可,所以插入速度较快。 8、EJB是基于哪些技术实现的?并说出SessionBean和EntityBean的区别,StatefulBean和StatelessBean的区别。 EJB包括Session Bean、Entity Bean、Message Driven Bea

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧