0

我有 4 张桌子

表 1 - 图纸提交-DTS

SUBMISSION-ID, 
DRAWING-ID,
INWARD-ID,
REVISION NO,
COMMENT DATE,
COMMENTS FORWARDED TO WED,
STATUS OF DRWG,
RECVD AT FFM,
REMARKS,
Last,
Remarks2,
Drawing Sheet,

表 2 - 绘图-主-DTS

DRAWING-ID,
DRAWING CSD NO,
DRAWING TITLE,
ENGINEER INCHARGE,

表 3 - LI 项目

PROJECT TITLE,
LI PROJECT NO,

表 4 - 向内

INWARD-ID,
REFERENCE NO,
LI PROJECT NO,
TYPE OF INCOMING DOC,

所有表都与该表中的一个字段相关。

表 4 中的参考编号、表 2 中的图纸 CSD 编号和表 3 中的 LI 项目编号是唯一的。

当图纸 CSD 编号将具有新的修订编号时,将生成新的参考编号。在该特定图纸 CSD 编号的最后参考编号上检查图纸 CSD 编号是否延迟。

我想要表 4 中的最后一个参考编号,表 1 中 DRWG 和 Recvd 的状态,表 2 中的负责工程师和图纸标题作为结果。我们将只提供 LI Project No 作为输入。

4

1 回答 1

0

您可能可以使用简单的拖放“访问样式”UI 来完成此操作(尽管我很少建议它,除非完全是初学者)。我猜这[LI PROJECT NO]是一个整数字段...

CREATE PROC dbo.GetLIProjectInfo (@LIProjNo INT)
AS
-- I got this from stackoverflow.com, see http://stackoverflow.com/questions/12559023/sql-server-2008-subquery-with-multiple-tables
BEGIN
   SELECT TOP (1)
      i.[REFERENCE NO],
      ds.[STATUS OF DRWG],
      ds.[RECVD AT FFM],
      dm.[DRAWING TITLE],
      dm.[ENGINEER INCHARGE]
   FROM dbo.[DRAWING SUBMISSIONS-DTS] AS ds
   JOIN dbo.[DRAWING-MAIN-DTS] AS dm
     ON dm.[DRAWING-ID] = ds.[DRAWING-ID]
   JOIN dbo.[Inward] AS i
     ON i.[INWARD-ID] = ds.[INWARD-ID]
   JOIN dbo.[LI PROJECTS] AS p
     ON p.[LI PROJECT NO] = i.[LI PROJECT NO]
   WHERE i.[LI PROJECT NO] = @LIProjNo
   ORDER BY i.[REFERENCE NO] DESC;
END

编辑:

对于每行[DRAWING CSD NO],您需要决定是希望 LIProjNo 过滤器过滤图纸列表还是仅过滤项目列表。

CREATE PROC dbo.GetLIProjectInfo (@LIProjNo INT)
AS
-- I got this from stackoverflow.com, see http://stackoverflow.com/questions/12559023/sql-server-2008-subquery-with-multiple-tables
BEGIN
   SELECT *
   FROM 
   (SELECT DISTINCT [DRAWING CSD NO]
     FROM dbo.[DRAWING-MAIN-DTS]) AS csd
   OUTER APPLY --Could be CROSS APPLY if you need
   (SELECT TOP (1)
      i.[REFERENCE NO],
      ds.[STATUS OF DRWG],
      ds.[RECVD AT FFM],
      dm.[DRAWING TITLE],
      dm.[ENGINEER INCHARGE]
   FROM dbo.[DRAWING SUBMISSIONS-DTS] AS ds
   JOIN dbo.[DRAWING-MAIN-DTS] AS dm
     ON dm.[DRAWING-ID] = ds.[DRAWING-ID]
   JOIN dbo.[Inward] AS i
     ON i.[INWARD-ID] = ds.[INWARD-ID]
   JOIN dbo.[LI PROJECTS] AS p
     ON p.[LI PROJECT NO] = i.[LI PROJECT NO]
   WHERE i.[LI PROJECT NO] = @LIProjNo
   AND dm.[DRAWING CSD NO] = csd.[DRAWING CSD NO]
   ORDER BY i.[REFERENCE NO] DESC) as inf
   ORDER BY csd.[DRAWING CSD NO];
END

编辑:

因为您只需要与提供的项目相关的图纸:

CREATE PROC dbo.GetLIProjectInfo (@LIProjNo INT)
AS
-- I got this from stackoverflow.com, see http://stackoverflow.com/questions/12559023/sql-server-2008-subquery-with-multiple-tables
BEGIN
   WITH Numbered AS (
   SELECT 
      ROW_NUMBER() OVER (PARTITION BY dm.[DRAWING CSD NO] ORDER BY i.[REFERENCE NO] DESC) AS RowNum,
      i.[REFERENCE NO],
      ds.[STATUS OF DRWG],
      ds.[RECVD AT FFM],
      dm.[DRAWING TITLE],
      dm.[ENGINEER INCHARGE]
   FROM dbo.[DRAWING SUBMISSIONS-DTS] AS ds
   JOIN dbo.[DRAWING-MAIN-DTS] AS dm
     ON dm.[DRAWING-ID] = ds.[DRAWING-ID]
   JOIN dbo.[Inward] AS i
     ON i.[INWARD-ID] = ds.[INWARD-ID]
   JOIN dbo.[LI PROJECTS] AS p
     ON p.[LI PROJECT NO] = i.[LI PROJECT NO]
   WHERE i.[LI PROJECT NO] = @LIProjNo
   )
   SELECT *
   FROM Numbered
   WHERE RowNum = 1;
END
于 2012-09-24T05:10:15.857 回答