注册 登录
编程论坛 VB6论坛

如何获取数据中的最大日期组数据

QQ342151559 发布于 2013-09-27 22:41, 601 次点击
有一组数据:
Date    Tool_Info    M_ID    Code
2013-9-12    D001    M001    2
2013-9-13    D002    M001    9
2013-9-14    D001    M001    0
2013-9-15    D004    M004    1
2013-9-16    D001    M001    0
2013-9-17    D001    M001    9
2013-9-18    D002    M002    1
2013-8-13    D002    M001    2
2013-8-19    D002    M001    0
2013-9-19    D003    M003    9


首先: Code=9 Tool_Info,M_ID的数据是唯一的
所以通过 Code=9可以找到 Tool_info,M_ID的 lists.
2013-9-13    D002    M001    9
2013-9-17    D001    M001    9
2013-9-19    D003    M003    9

然后来获取code<>9中日期最大值得数据 lists。
2013-8-19    D002    M001    0
2013-9-16    D001    M001    0

如何使用Max?发现只能用max(date),如果再加其他的field就出错.
(使用的是 Access)
9 回复
#2
九连阳2013-09-28 08:42
dim date1 as date,date2 as date

date1=2013-8-19
date2=2013-9-16
if date2>date1 then
    ......

end if

最好用FOR 或者DO 来排序
#3
QQ3421515592013-09-28 09:00
需求的是  搜索语句...
#4
QQ3421515592013-09-28 10:03
S1="select tool_info,M_Id from 表 where code='9'"

获取 code='9'的 tool_info/M-id的数据Arr;
S2=select (select max(date) from 表) as Last_Date,tool_info,M_Id fromwhere code<>'9'

获取所有 code<>'9'的 tool_info/M-id的数据Arr;

s3="select A.Last_Date,B.tool_info,B.M_Id from (" & S2 & ") A left JOIN (" & S1 & ") B on A.tool_info=B.tool_info and A.M_Id =B.M_Id "



对比 S1和S2,显示S2中tool_info,M-id包含在 S1中的最近日期数据(唯一)

感觉(select max(date) from 表) as Last_Date,未起到作用.
搜索不到 code<>9中日期为最大的数据.
不知道哪里出错
#5
bczgvip2013-09-28 14:42
排个序不就行了么?
#6
QQ3421515592013-09-28 15:33
不行啊,如果我再加个条件可能更好理解吧:
code='9' and date=now 搜索到 tool_info,M_ID数据组
然后再搜索该数据组中code<>'9',日期最大的那一组数据.
#7
lowxiong2013-09-28 20:25
这个用sql语句好像难以达到目地,用一小段程序则很容易解决。
#8
九连阳2013-09-28 22:23
Private Type 自定义数据

    日期 As Date
    Tool_Info As String
     M_ID  As String
     Code As Integer
End Type

Dim Arr(1 To 10) As 自定义数据, Arr1(1 To 10) As 自定义数据, Arr2(1 To 10) As 自定义数据

Dim i As Integer, p As Integer, q As Integer, j As Integer, 数据 As 自定义数据, 日期 As Date, 最大日 As Integer

    Arr(i).日期 = "2013-9-12"
    Arr(i).Tool_Info = ""
    Arr(i).M_ID = ""
    Arr(i).Code = ""
   
    ... ...


For i = 1 To 10
    If Arr(i).Code = 9 Then
        数据.日期 = Arr(i).日期
        数据.Tool_Info = Arr(i).Tool_Info
        数据.M_ID = Arr(i).M_ID
        数据.Code = Arr(i).Code
        p = p + 1
        Arr1(p) = 数据
   
    End If

Next

For i = 1 To 10
    If Arr(i).Code <> 9 Then
        数据.日期 = Arr(i).日期
        数据.Tool_Info = Arr(i).Tool_Info
        数据.M_ID = Arr(i).M_ID
        数据.Code = Arr(i).Code
        q = q + 1
        Arr2(q) = 数据
    End If

Next

IF q>0 THEN
日期 = Arr2(1).日期
最大日 = 1
END IF
IF q>1 THEN
For j = 2 To q
        
    If Arr2(j).日期 > 日期 Then
        日期 = Arr2(j).日期
        最大日 = j
   
    End If

Next

END IF
'Arr2(最大日).XXX  的数据可能是你需要的数据
如果不对,不要见笑
#9
风吹过b2013-09-29 10:54
按5楼的方法

SELECT TOP 1 Date, Tool_Info, M_ID, Code  FROM 表  WHERE Code<>9  ORDER BY Date DESC;


按日期排序,Date 字段是日期型的。
只返回第一条记录,看看是不是你所需要结果。
Access 2003 测试通过。

返回的结果是:
Date    Tool_Info    M_ID    Code
2013-9-18    D002    M002    1
#10
lowxiong2013-09-29 13:23
回复 9楼 风吹过b
他是首先要获取code=9的记录集,然后依次访问该记录集的Tool_Info、 M_ID数据,获取其code<>9的记录集,再依次判断最大日期,我觉得用下述代码可以完成:
dim rs1 as new recordset,rs2 as new recordset,a as string,f as boolean,d as date
a="select * from 表 where code<>9 order by date desc"    '按倒序排列数据,第一个数据日期最大(最近)
rs1.open a,conn
f=false
do while not rs1.eof
  a="select * from 表 where Tool_Info='" & rs1.fields("Tool_Info") & "' and M_ID='" & rs1.fields("M_ID") & "' and code=9"
  '如果有等于 Tool_Info 并且等于M_ID并且等于9的记录则该记录日期为最大日期
  rs2.open a,conn
  if not rs2.eof then
    f=true
    d=rs1.fields("date")
    exit do
  endif
  rs1.movenext              '没有符合条件的查找下一条记录
loop
rs1.close
rs2.close
if f then msgbox d       '如果符合条件则显示该日期
1