我有几张桌子:
1 table: Measure with column:[id], [idTEST], [Position], [idPARENT] ,[Pos_X],[Pos_Y],[Pos_Z], [Error_Comment], [Repair_Date]
2 table: Test with coulmns: [idTEST], [idKARD], [Panel_Time],[idTFILE]
3 table: Kard with columns: [idKARD], [Kard_Code], [idPANEL],[Kard_Nr]
4. table tstFile: [idTFILE],[Name],[Side],[Descr]
5. table Parent: [idPARENT], [Parent] ,[idMODL]
6. table Panel: idPANEL, PanelCode
我需要从 Measure 表中选择数据,这些数据将是:
1. with most old Repair_Date for each Kard_Code
2. and with most recent Panel_Time for each card :
使用这样的列:c.Kard_Code,m.Position,m.Pos_X, m.Pos_Y, m.Pos_Z,s.Name, pn.Parent, m.Repair_Date
我试过这样的事情:
SELECT c.KardCode,
m.Position,
m.Pos_X,
m.Pos_Y,
m.Pos_Z,
s.NAME,
pn.Parent,
max(m.Repair_Date) AS Repair_Date,
max(m.id),
PanelCode
FROM dbo.measure m
INNER JOIN dbo.TEST AS t
ON m.id_TEST = t.id_TEST
INNER JOIN dbo.tstFILE AS s
ON t.idTFILE = s.idTFILE
INNER JOIN dbo.KARD AS c
ON t.idKard = c.idKadr
INNER JOIN dbo.PANELS AS p
ON c.id_PANELS = p.id_PANELS
INNER JOIN dbo.PARENT AS pn
ON m.idPARENT = pn.idPARENT
WHERE m.Repair_Date = (
SELECT DISTINCT min(Repair_Date)
FROM Measure AS m2
WHERE m2.idTEST = m.idTEST
)
AND (
t.Panel_Time IN (
SELECT DISTINCT MAX(Panel_Time) AS Panel_Time
FROM TEST
GROUP BY id_CARDS
)
)
GROUP BY c.KardCode,
m.Position,
m.Pos_X,
m.Pos_Y,
m.Pos_Z,
s.NAME,
pn.Parent
ORDER BY m.idTEST,
c.KardCode
结果:我以重复 IN Repair_Date 结束,其中日期类似于 EG:
KardCode Position Pos_X Pos_Y Pos_Z Name Parent PanelCode Repair_Date id
a CX 0 0 0 Blue_Card SKY 2012-10-03 00:06:41.000 1514
a CY 0 0 0 Blue_Card SKY 2012-10-03 00:06:41.000 1515
...
我怎样才能在没有这种重复的情况下检索数据?