我正在寻找以下问题的答案
我有一个包含3个以下表格的数据库:
TB-001_ISB
TB-002_ISDB
TI-003_ISBDB
表TB-001_ISB下的数据是
ISB_No ISB_Ad
121XS9060301 ANKARA CANKAYA
122CV9061501 ANKARA YILDIZ
123BN9062081 ANKARA KIZILAY
124GH8709034 ANKARA MAMAK
TB-002_ISDB下的数据是
ISDB_ID ISDB_Sit
F-10-01-MM Factory Production
F-20-01-MG Factory Quality
F-30-01-FM Site
TI-003_ISBDB下的数据
ISBDB_No ISBDB_ISDB_ID ISBDB_Pers ISBDB_Date
121XS9060301 F-10-01-MM 1234 01.01.2010
121XS9060301 F-20-01-MG 1234 02.01.2010
121XS9060301 F-30-01-FM 4321 03.01.2010
121XS9060301 F-20-01-MG 6785 04.01.2010
122CV9061501 F-10-01-MM 1234 01.03.2010
122CV9061501 F-20-01-MG 1234 02.03.2010
123BN9062081 F-20-01-MG 4321 03.10.2010
123BN9062081 F-30-01-FM 6785 04.10.2010
我需要一个查询来获得以下输出:
ISBDB_No ISB_Ad ISDB_Sit ISBDB_Pers ISBDB_Date
121XS9060301 ANKARA CANKAYA Factory Quality 6785 04.01.2010
122CV9061501 ANKARA YILDIZ Factory Quality 1234 02.03.2010
123BN9062081 ANKARA KIZILAY Site 6785 04.10.2010
基本上,我希望结果按ISBDB_Date排序并取最大值(ISBDB_Date) 为此,我写了以下查询
SELECT [ISB_No], [ISB_Ad], [ISDB_Sit], [ISBDB_Pers],
(SELECT (Max([ISBDB_Date])
FROM [TI-003_ISBDB]
GROUP BY [ISBDB_No])
FROM (([TB-001_ISB] INNER JOIN [TI-003_ISBDB] ON [ISB_No]=[ISBDB_No])
INNER JOIN [TB-002_ISDB] ON [ISDB_ID]=[ISBDB_ISDB_ID])
但是,此查询存在问题。有谁能够帮我?所有的努力都受到高度赞赏......
答案 0 :(得分:1)
尝试这样的事情:
WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY [ISBDB_No] ORDER BY [ISBDB_Date] DESC) Corr
FROM [TI-003_ISBDB]
)
SELECT B.*, [ISB_Ad], [ISDB_Sit]
FROM [TB-001_ISB] A
INNER JOIN (SELECT * FROM CTE WHERE corr = 1) B
ON A.[ISB_No] = B.[ISBDB_No]
INNER JOIN [TB-002_ISDB] C
ON C.[ISDB_ID] = B.[ISBDB_ISDB_ID]
答案 1 :(得分:0)
SELECT
ISB_No
,ISB_Ad
,ISDB_Sit
,ISBDB_Pers
,ISBDB_Date
FROM [TB-001_ISB] AS isb
INNER JOIN [TI-003_ISBDB] AS isdbd ON isb.ISB_No=isdbd.ISBDB_No
INNER JOIN [TB-002_ISDB] AS isdb ON isdb.ISDB_ID=isdbd.ISBDB_ISDB_ID
WHERE isdbd.ISBDB_Date = (select MAX(isdbd1.ISBDB_Date) FROM [TI-003_ISBDB] AS isdbd1
WHERE isdbd.ISBDB_No = isdbd1.ISBDB_No)
ORDER BY isdbd.ISBDB_Date DESC