2

我有几张桌子:

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
...

我怎样才能在没有这种重复的情况下检索数据?

4

2 回答 2

0

try something like this..Hope its helps..

with tbl as(
    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,
            RANK() over(partition by c.KardCode,
            m.Position,
            m.Pos_X,
            m.Pos_Y,
            m.Pos_Z,
            s.NAME,
            pn.Parent order by m.idTEST,
            c.KardCode) as r
    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
                            )
                    )
)select KardCode,
        Position,
        Pos_X,
        Pos_Y,
        Pos_Z,
        NAME,
        Parent,
        Repair_Date,
        id,
        PanelCode 
    from tbl
    where r=1
于 2012-10-21T16:15:22.537 回答
0

谢谢大家我解决了这个问题,因为你喜欢这样:

 SELECT m.[Repair_Date],c.idKArd,m.[Position],m.[error_table],m.[Pos_X],m.[Pos_Y]
  ,m.[Pos_Z],s.name, pn.Parent
 FROM dbo.MEASURE m      inner join dbo.TEST t on m.id_TEST=t.id_TEST

 inner JOIN    dbo.Kard AS c ON t.idKard = c.idKard 
   inner join dbo.tstfile AS s ON t.idTFILE = s.idTFILE INNER JOIN
           dbo.PANEL AS p ON c.idPANEL = p.idPANEL INNER JOIN
    dbo.PARENT AS pn ON m.idPARENT = pn.idPARENT INNER JOIN
 WHERE m.id = (SELECT TOP 1 m2.id FROM dbo.MEASURE m2 WHERE m2.idTEST = m.idTEST)  
 and  Error_Comment in (200,300)
 AND (t.Panel_Time IN (SELECT     MAX(Panel_Time) AS Panel_Time FROM dbo.TEST  GROUP BY
   idKARD))
 and t.idTest = (SELECT TOP 1 t2.idTest FROM dbo.test t2 WHERE t2.idKARD = c.idKARD)
 order by repair_Date, c.idKARD
于 2012-11-06T08:21:55.297 回答