在SQL Server中选择顶部绑定不返回预期数据

时间:2017-01-07 06:35:51

标签: sql sql-server

我有一个名为NdtReports的表,其中包含以下数据:

enter image description here

另一个名为ndtreportdetails的表格,其中包含以下数据:

enter image description here

这两个表基于ndtreportdetails.reportidReport.id

加入

我执行此查询:

SELECT TOP 1 WITH TIES 
    NRD.NdtType, NRD.RequestNumber, NR.NdtReportNumber,
    NRD.ResponseReportDatetime, NRD.Defect,
    NRD.ResponseReportNumber, NRD.Remark
FROM   
    dbo.NdtReportDetails NRD
LEFT OUTER JOIN 
    NdtReports NR ON NRD.ReportId = NR.Id
WHERE  
    NRD.JointId = 1661632
    AND NRD.NdtType IN ('RT', 'PT', 'PWHT', 'MT', 'UT')
ORDER BY 
    NRD.Id DESC

返回此结果:

enter image description here

它返回最后记录的具有最大id的jointid,但我需要所有类型RT,PT,UT的最大id的最后一条记录。预期结果应为3条记录,ID为139189(PT),139193(UT),139192(RT)

文字资料: 报告:

Id  NdtReportNumber NdtReportDate   UserId  SubmitDateTime  Type
6292    IR-AFP-AG-PT-0001   2017-01-07 09:15:13.937 59  2017-01-07 09:15:13.917 PT
6293    IR-AFP-AG-RT-0001   2017-01-07 09:20:54.397 59  2017-01-07 09:20:54.367 UT
6294    IR-AFP-AG-RT-0001   2017-01-07 09:23:15.513 59  2017-01-07 09:23:15.487 RT
6295    IR-AFP-AG-RT-000    2017-01-07 09:41:21.807 59  2017-01-07 09:41:21.787 RT
6296    IR-AFP-AG-RT-000    2017-01-07 09:42:45.427 59  2017-01-07 09:42:45.400 UT

NDTreportdetail

Id  ReportId    State   Defect  SubmitDateTime  NdtType ResponseReportNumber    ResponseReportDatetime  Remark  JointId RequestNumber   RequestDatetime
139189  6292            2017-01-07 09:15:37.207 PT  pt psa res  2017-01-07 09:15:13.957     1661632 pt psa  NULL
139190  6293            2017-01-07 09:21:14.853 UT  ut result psa   2017-01-07 09:20:54.440     1661632 ut psa  NULL
139191  6294            2017-01-07 09:23:29.473 RT  dsad    2017-01-07 09:23:15.530     1661632 adasa   NULL
139192  6295    NULL        2017-01-07 09:41:21.820 RT  NULL    2017-01-07 09:41:21.820 NULL    1661632 NULL    NULL
139193  6296    NULL        2017-01-07 09:42:45.437 UT  NULL    2017-01-07 09:42:45.437 NULL    1661632 NULL    NULL

1 个答案:

答案 0 :(得分:1)

auto arr = new int[3][2];