TSQL根据上次日期选择没有重复的记录

时间:2012-10-21 15:24:29

标签: tsql

我有几张桌子:

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

结果: 我结束了在Repair_Date中的重复,其中日期类似于E.G:

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

如何在没有这些副本的情况下检索数据?

2 个答案:

答案 0 :(得分:0)

尝试类似这样的事情。希望它的帮助..

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

答案 1 :(得分: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