按最新日期取记录的问题(200分)

  • 主题发起人 少爷的拐杖
  • 开始时间

少爷的拐杖

Unregistered / Unconfirmed
GUEST, unregistred user!
select cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.安装日期,cable_terminal.终端头型号
from
cable_offset_box,cable,cable_terminal
where ((cable.运行范围始属性='分支箱' and cable.运行范围始id=cable_offset_box.id)
or (cable.运行范围终属性='分支箱' and cable.运行范围终id=cable_offset_box.id))
and cable_terminal.所属电缆=cable.id

得到

分支箱名称 电缆名称 安装日期 终端头型号
-------------------- -------------------- --------------------------- ----------
#1分支箱 出箱1 2000-12-23 17:58:23.000 WD-76
#1分支箱 出箱1 2000-12-30 17:58:23.000 出1
#1分支箱 出箱2 1999-02-02 00:00:00.000 NULL
#1分支箱 出箱2 2000-02-02 00:00:00.000 出2
#1分支箱 出箱3 2001-01-01 00:00:00.000 NULL
#1分支箱 出箱3 2001-02-02 00:00:00.000 出3
#1分支箱 进箱1 2000-02-02 00:00:00.000 NULL
#1分支箱 进箱1 2002-02-02 00:00:00.000 出4
#2分支箱 in1 1999-05-05 00:00:00.000 NULL
#2分支箱 in1 2000-01-01 00:00:00.000 NULL
#2分支箱 in2 2000-02-06 00:00:00.000 NULL
#2分支箱 in2 2000-03-03 00:00:00.000 NULL
#2分支箱 out1 2000-02-04 00:00:00.000 NULL
#2分支箱 out1 2000-03-04 00:00:00.000 NULL
#2分支箱 out3 2000-02-02 00:00:00.000 NULL
#2分支箱 out3 2000-03-07 00:00:00.000 NULL
#2分支箱 out2 2000-02-03 00:00:00.000 NULL
#2分支箱 out2 2000-03-06 00:00:00.000 NULL

实际上我想得到:
#1分支箱 出箱1 2000-12-30 17:58:23.000 出1
#1分支箱 出箱2 2000-02-02 00:00:00.000 出2
#1分支箱 出箱3 2001-02-02 00:00:00.000 出3
#1分支箱 进箱1 2002-02-02 00:00:00.000 出4
#2分支箱 in1 2000-01-01 00:00:00.000 NULL
#2分支箱 in2 2000-03-03 00:00:00.000 NULL
#2分支箱 out1 2000-03-04 00:00:00.000 NULL
#2分支箱 out3 2000-03-07 00:00:00.000 NULL
#2分支箱 out2 2000-03-06 00:00:00.000 NULL

也就是根据 cable_terminal.安装日期 来取最近的一条出来,加Max(**)总失败
 
select cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.安装日期,cable_terminal.终端头型号
from
cable_offset_box,cable,cable_terminal
where ((cable.运行范围始属性='分支箱' and cable.运行范围始id=cable_offset_box.id)
or (cable.运行范围终属性='分支箱' and cable.运行范围终id=cable_offset_box.id))
and cable_terminal.所属电缆=cable.id
group by cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.终端头型号
 
to ugvanxk:
Server: Msg 8120, Level 16, State 1, Line 1
Column 'cable_terminal.安装日期' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.
 
我先站个位子,等我看完
 
那就快看,答出来再送你100,俺等着答案要用。
 
ugvanxk的差一点儿,看下面:
select cable_offset_box.分支箱名称,cable.电缆名称,max(cable_terminal.安装日期) azrq,cable_terminal.终端头型号
from
cable_offset_box,cable,cable_terminal
where ((cable.运行范围始属性='分支箱' and cable.运行范围始id=cable_offset_box.id)
or (cable.运行范围终属性='分支箱' and cable.运行范围终id=cable_offset_box.id))
and cable_terminal.所属电缆=cable.id
group by cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.终端头型号
 
select cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.安装日期,cable_terminal.终端头型号
from
cable_offset_box,cable,cable_terminal
where ((cable.运行范围始属性='分支箱' and cable.运行范围始id=cable_offset_box.id)
or (cable.运行范围终属性='分支箱' and cable.运行范围终id=cable_offset_box.id))
and cable_terminal.所属电缆=cable.id

and cable.id in
(
select test.id from cable test
where 安装时间 in (select max(安装时间) from cable group by 所属ID )
and 安装时间 not in (select min(安装时间) from cable group by 所属id)
)

这样
 
玩颜啊:
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name '安装时间'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name '安装时间'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name '所属ID'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name '安装时间'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name '安装时间'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name '所属id'.

李衍智:
结果还是没有按照安装日期取出最新的来.
 
各个表格牵涉到的字段说明如下:
分支箱表:分支箱名称
电缆表:id(唯一的编号)
运行范围始属性(标记电缆受电端连接的设备类型)
运行范围始id(标记电缆受电端设备的标号,如果属性字段内容是'分支箱'
则记录分支箱编号,如属性为环网柜,则记录环网柜编号)
运行范围终类似...
终端头安装记录表:所属电缆(安装所在的那根电缆的id)
安装时间()
 
你改一下嘛,
安装时间-安装日期


那么终端头表里就记录:
id 所属id 安装日期
2 23 1999-1-1
3 23 1999-3-3
4 23 2000-7-4

里面不是有个"所属id"?他不是和‘安装日期’是同一个表的字段啊
 
好好的使用Inner Join, Left Join, Right Join,和Join,你可以很容易作出来,没
必要写这么多的where条件,把所有库的字段贴出,并指明其关联子段,
 
呵用UNION就不出现这种问题:)
select cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.安装日期,cable_terminal.终端头型号
from
cable_offset_box,cable,cable_terminal
where ((cable.运行范围始属性='分支箱' and cable.运行范围始id=cable_offset_box.id)
union
select cable_offset_box.分支箱名称,cable.电缆名称,cable_terminal.安装日期,cable_terminal.终端头型号
from
cable_offset_box,cable,cable_terminal
(cable.运行范围终属性='分支箱' and cable.运行范围终id=cable_offset_box.id))
and cable_terminal.所属电缆=cable.id
呵呵呵呵呵呵呵呵呵呵~~~~~~~~~~~~`
:D
 
以上都没能打到要求:(
这样吧,我放弃一步到位的想法,能只在一个表中按日期来查最新的就行,
我用视图来连.
只查询cable_terminal表.
要求返回的结果包含以下三个字段,并且'所属电缆'字段内容相同的记录只取
安装日期中最近的一个.
OK了就分分了!

表结构说明如下
cable_terminal(终端头安装记录表)
字段:
所属电缆(记录电缆id,用于和电缆表关联)
终端头型号(字符)
安装日期(日期型)
.....
 
SQL语句实在太长了,没法儿看。我从你已经得到的表的基础上取出记录。
select cable_offset_box.分支箱名称,cable.电缆名称,max(cable_terminal.安装日期)
from YourTableName
group by cable_offset_box.分支箱名称,cable.电缆名称
 
放弃了.另想办法.
 
完颜康的答案是最合理的,少爷的拐杖,重要的是人家SQL语句的写法,而不是里面的错误、
他的句子里面错了你不会改么?人家这种写法肯定能实现的
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
594
import
I
I
回复
0
查看
759
import
I
I
回复
0
查看
637
import
I
顶部