[英]Does the 'SELECT TOP 1 WITH TIES *', work within java sql connector?
[英]Select top ties in SQL Server doesn't return expected data
我有一個名為NdtReports
的表, NdtReports
包含以下數據:
另一個名為ndtreportdetails
表包含此數據:
這兩個表基於ndtreportdetails.reportid
和Report.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
它返回以下結果:
它返回最大id的關節ID的最后記錄,但是對於所有RT,PT,UT
類型RT,PT,UT
我都需要最大ID的最后記錄。 預期結果應為ID為139189(PT),139139(UT),139192(RT)的3條記錄
文字數據:報告:
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
無損檢測報告
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
SELECT t.NdtType,
t.RequestNumber,
t.NdtReportNumber,
t.ResponseReportDatetime,
t.Defect,
t.ResponseReportNumber,
t.Remark
FROM
(
SELECT NRD.NdtType,
NRD.Id,
NRD.RequestNumber,
NR.NdtReportNumber,
NRD.ResponseReportDatetime,
NRD.Defect,
NRD.ResponseReportNumber,
NRD.Remark,
ROW_NUMBER() OVER(PARTITION BY NRD.NdtType ORDER BY NRD.Id DESC) AS rn
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')
) t
WHERE t.rn = 1
ORDER BY t.Id DESC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.