如何仅从SQL SERVER表中选择第一行

时间:2018-06-08 16:00:25

标签: sql-server sql-server-2014

我有一个SQL SERVER查询。我的任务是从表中选择第一行。我的计划是使用TOP 1。但由于查询太复杂,我无法弄清楚TOP 1的位置。请先检查我的查询:

SELECT 
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM AssetDocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.AssetPK = md.AssetPK
WHERE (WO.WOPK IN (SELECT
  WOPK
FROM WO WITH
(NOLOCK)
LEFT OUTER JOIN Asset WITH (NOLOCK)
  ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
  ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
UNION ALL
SELECT
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM LaborDocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER
JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.RequesterPK = md.LaborPK
WHERE (WO.WOPK IN (SELECT
  WOPK
FROM WO WITH (NOLOCK)
LEFT
OUTER JOIN Asset WITH (NOLOCK)
  ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
  ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
UNION ALL
SELECT
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM RepairCenterDocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.RepairCenterPK = md.RepairCenterPK
WHERE (WO.WOPK IN (SELECT
  WOPK
FROM WO WITH (NOLOCK)
LEFT OUTER JOIN Asset
WITH (NOLOCK)
  ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
  ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
UNION ALL
SELECT
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM ProjectDocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.ProjectPK = md.ProjectPK
WHERE (WO.WOPK IN (SELECT
  WOPK
FROM WO WITH (NOLOCK)
LEFT OUTER JOIN Asset WITH (NOLOCK)
  ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
  ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
UNION ALL
SELECT
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM WOdocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.WOPK = md.WOPK
WHERE (WO.WOPK IN (SELECT
  WOPK
FROM WO WITH (NOLOCK)
LEFT OUTER JOIN Asset WITH (NOLOCK)
  ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
  ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
ORDER BY WO.WOPK, md.ModuleID, d.DocumentID

现在请检查我的查询输出。有两排。但我必须只选择第一个。 enter image description here

请给我任何解决方案。提前谢谢。

2 个答案:

答案 0 :(得分:2)

你试过了吗?

SELECT TOP 1 * FROM (
     SELECT 
       WO.WOPK,
       md.PK,
       d.LocationType,
       d.DocumentID,
       d.DocumentName,
       md.ModuleID,
       d.DocumentTypeDesc,
       d.Location,
       md.PrintWithWO,
       md.SendWithEmail,
       md.DisplayLink,
       md.RowVersionDate,
       d.Photo,
       MCModule.TitleforDocumentList,
       d.DocumentText
     FROM AssetDocument md WITH (NOLOCK)
     LEFT OUTER JOIN Document d WITH (NOLOCK)
       ON md.DocumentPK = d.DocumentPK
     INNER JOIN MCModule WITH (NOLOCK)
       ON md.ModuleID = MCModule.ModuleID
     INNER JOIN WO WITH (NOLOCK)
       ON WO.AssetPK = md.AssetPK
     WHERE (WO.WOPK IN (SELECT
       WOPK
     FROM WO WITH
     (NOLOCK)
     LEFT OUTER JOIN Asset WITH (NOLOCK)
       ON Asset.AssetPK = WO.AssetPK
     LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
       ON AssetHierarchy.AssetPK = WO.AssetPK
     WHERE WO.WOPK = 10109)
     )
     AND (d.Active = 1)
     UNION ALL
     SELECT
       WO.WOPK,
       md.PK,
       d.LocationType,
       d.DocumentID,
       d.DocumentName,
       md.ModuleID,
       d.DocumentTypeDesc,
       d.Location,
       md.PrintWithWO,
       md.SendWithEmail,
       md.DisplayLink,
       md.RowVersionDate,
       d.Photo,
       MCModule.TitleforDocumentList,
       d.DocumentText
     FROM LaborDocument md WITH (NOLOCK)
     LEFT OUTER JOIN Document d WITH (NOLOCK)
       ON md.DocumentPK = d.DocumentPK
     INNER
     JOIN MCModule WITH (NOLOCK)
       ON md.ModuleID = MCModule.ModuleID
     INNER JOIN WO WITH (NOLOCK)
       ON WO.RequesterPK = md.LaborPK
     WHERE (WO.WOPK IN (SELECT
       WOPK
     FROM WO WITH (NOLOCK)
     LEFT
     OUTER JOIN Asset WITH (NOLOCK)
       ON Asset.AssetPK = WO.AssetPK
     LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
       ON AssetHierarchy.AssetPK = WO.AssetPK
     WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
UNION ALL
SELECT
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM RepairCenterDocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.RepairCenterPK = md.RepairCenterPK
WHERE (WO.WOPK IN (SELECT
  WOPK
FROM WO WITH (NOLOCK)
LEFT OUTER JOIN Asset
WITH (NOLOCK)
  ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
  ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10109)
)
AND (d.Active = 1)
UNION ALL
SELECT
  WO.WOPK,
  md.PK,
  d.LocationType,
  d.DocumentID,
  d.DocumentName,
  md.ModuleID,
  d.DocumentTypeDesc,
  d.Location,
  md.PrintWithWO,
  md.SendWithEmail,
  md.DisplayLink,
  md.RowVersionDate,
  d.Photo,
  MCModule.TitleforDocumentList,
  d.DocumentText
FROM ProjectDocument md WITH (NOLOCK)
LEFT OUTER JOIN Document d WITH (NOLOCK)
  ON md.DocumentPK = d.DocumentPK
INNER JOIN MCModule WITH (NOLOCK)
  ON md.ModuleID = MCModule.ModuleID
INNER JOIN WO WITH (NOLOCK)
  ON WO.ProjectPK = md.ProjectPK
WHERE (WO.WOPK IN (SELECT
  WOPK
     FROM WO WITH (NOLOCK)
     LEFT OUTER JOIN Asset WITH (NOLOCK)
       ON Asset.AssetPK = WO.AssetPK
     LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
       ON AssetHierarchy.AssetPK = WO.AssetPK
     WHERE WO.WOPK = 10109)
     )
     AND (d.Active = 1)
     UNION ALL
     SELECT
       WO.WOPK,
       md.PK,
       d.LocationType,
       d.DocumentID,
       d.DocumentName,
       md.ModuleID,
       d.DocumentTypeDesc,
       d.Location,
       md.PrintWithWO,
       md.SendWithEmail,
       md.DisplayLink,
       md.RowVersionDate,
       d.Photo,
       MCModule.TitleforDocumentList,
       d.DocumentText
     FROM WOdocument md WITH (NOLOCK)
     LEFT OUTER JOIN Document d WITH (NOLOCK)
       ON md.DocumentPK = d.DocumentPK
     INNER JOIN MCModule WITH (NOLOCK)
       ON md.ModuleID = MCModule.ModuleID
     INNER JOIN WO WITH (NOLOCK)
       ON WO.WOPK = md.WOPK
     WHERE (WO.WOPK IN (SELECT
       WOPK
     FROM WO WITH (NOLOCK)
     LEFT OUTER JOIN Asset WITH (NOLOCK)
       ON Asset.AssetPK = WO.AssetPK
     LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
       ON AssetHierarchy.AssetPK = WO.AssetPK
     WHERE WO.WOPK = 10109)
    )
     AND (d.Active = 1)
      ORDER BY WO.WOPK, md.ModuleID, d.DocumentID
    )
) sub

答案 1 :(得分:1)

If you are new to SQL it's very useful to set up simple re-runnable examples like this:

declare @t table (id int)

insert into @t (id)
select 1 union
select 2

select * from @t

select top 1 * from (
    select *
    from @t
) z

And as sgeddes said your solution is:

    select top 1 * from (
        SELECT 
          WO.WOPK,
          md.PK,
          d.LocationType,
          d.DocumentID,
          d.DocumentName,
          md.ModuleID,
          d.DocumentTypeDesc,
          d.Location,
          md.PrintWithWO,
          md.SendWithEmail,
          md.DisplayLink,
          md.RowVersionDate,
          d.Photo,
          MCModule.TitleforDocumentList,
          d.DocumentText
        FROM AssetDocument md WITH (NOLOCK)
        LEFT OUTER JOIN Document d WITH (NOLOCK)
          ON md.DocumentPK = d.DocumentPK
        INNER JOIN MCModule WITH (NOLOCK)
          ON md.ModuleID = MCModule.ModuleID
        INNER JOIN WO WITH (NOLOCK)
          ON WO.AssetPK = md.AssetPK
        WHERE (WO.WOPK IN (SELECT
          WOPK
        FROM WO WITH
        (NOLOCK)
        LEFT OUTER JOIN Asset WITH (NOLOCK)
          ON Asset.AssetPK = WO.AssetPK
        LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
          ON AssetHierarchy.AssetPK = WO.AssetPK
        WHERE WO.WOPK = 10109)
        )
        AND (d.Active = 1)
        UNION ALL
        SELECT
          WO.WOPK,
          md.PK,
          d.LocationType,
          d.DocumentID,
          d.DocumentName,
          md.ModuleID,
          d.DocumentTypeDesc,
          d.Location,
          md.PrintWithWO,
          md.SendWithEmail,
          md.DisplayLink,
          md.RowVersionDate,
          d.Photo,
          MCModule.TitleforDocumentList,
          d.DocumentText
        FROM LaborDocument md WITH (NOLOCK)
        LEFT OUTER JOIN Document d WITH (NOLOCK)
          ON md.DocumentPK = d.DocumentPK
        INNER
        JOIN MCModule WITH (NOLOCK)
          ON md.ModuleID = MCModule.ModuleID
        INNER JOIN WO WITH (NOLOCK)
          ON WO.RequesterPK = md.LaborPK
        WHERE (WO.WOPK IN (SELECT
          WOPK
        FROM WO WITH (NOLOCK)
        LEFT
        OUTER JOIN Asset WITH (NOLOCK)
          ON Asset.AssetPK = WO.AssetPK
        LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
          ON AssetHierarchy.AssetPK = WO.AssetPK
        WHERE WO.WOPK = 10109)
        )
        AND (d.Active = 1)
        UNION ALL
        SELECT
          WO.WOPK,
          md.PK,
          d.LocationType,
          d.DocumentID,
          d.DocumentName,
          md.ModuleID,
          d.DocumentTypeDesc,
          d.Location,
          md.PrintWithWO,
          md.SendWithEmail,
          md.DisplayLink,
          md.RowVersionDate,
          d.Photo,
          MCModule.TitleforDocumentList,
          d.DocumentText
        FROM RepairCenterDocument md WITH (NOLOCK)
        LEFT OUTER JOIN Document d WITH (NOLOCK)
          ON md.DocumentPK = d.DocumentPK
        INNER JOIN MCModule WITH (NOLOCK)
          ON md.ModuleID = MCModule.ModuleID
        INNER JOIN WO WITH (NOLOCK)
          ON WO.RepairCenterPK = md.RepairCenterPK
        WHERE (WO.WOPK IN (SELECT
          WOPK
        FROM WO WITH (NOLOCK)
        LEFT OUTER JOIN Asset
        WITH (NOLOCK)
          ON Asset.AssetPK = WO.AssetPK
        LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
          ON AssetHierarchy.AssetPK = WO.AssetPK
        WHERE WO.WOPK = 10109)
        )
        AND (d.Active = 1)
        UNION ALL
        SELECT
          WO.WOPK,
          md.PK,
          d.LocationType,
          d.DocumentID,
          d.DocumentName,
          md.ModuleID,
          d.DocumentTypeDesc,
          d.Location,
          md.PrintWithWO,
          md.SendWithEmail,
          md.DisplayLink,
          md.RowVersionDate,
          d.Photo,
          MCModule.TitleforDocumentList,
          d.DocumentText
        FROM ProjectDocument md WITH (NOLOCK)
        LEFT OUTER JOIN Document d WITH (NOLOCK)
          ON md.DocumentPK = d.DocumentPK
        INNER JOIN MCModule WITH (NOLOCK)
          ON md.ModuleID = MCModule.ModuleID
        INNER JOIN WO WITH (NOLOCK)
          ON WO.ProjectPK = md.ProjectPK
        WHERE (WO.WOPK IN (SELECT
          WOPK
        FROM WO WITH (NOLOCK)
        LEFT OUTER JOIN Asset WITH (NOLOCK)
          ON Asset.AssetPK = WO.AssetPK
        LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
          ON AssetHierarchy.AssetPK = WO.AssetPK
        WHERE WO.WOPK = 10109)
        )
        AND (d.Active = 1)
        UNION ALL
        SELECT
          WO.WOPK,
          md.PK,
          d.LocationType,
          d.DocumentID,
          d.DocumentName,
          md.ModuleID,
          d.DocumentTypeDesc,
          d.Location,
          md.PrintWithWO,
          md.SendWithEmail,
          md.DisplayLink,
          md.RowVersionDate,
          d.Photo,
          MCModule.TitleforDocumentList,
          d.DocumentText
        FROM WOdocument md WITH (NOLOCK)
        LEFT OUTER JOIN Document d WITH (NOLOCK)
          ON md.DocumentPK = d.DocumentPK
        INNER JOIN MCModule WITH (NOLOCK)
          ON md.ModuleID = MCModule.ModuleID
        INNER JOIN WO WITH (NOLOCK)
          ON WO.WOPK = md.WOPK
        WHERE (WO.WOPK IN (SELECT
          WOPK
        FROM WO WITH (NOLOCK)
        LEFT OUTER JOIN Asset WITH (NOLOCK)
          ON Asset.AssetPK = WO.AssetPK
        LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK)
          ON AssetHierarchy.AssetPK = WO.AssetPK
        WHERE WO.WOPK = 10109)
        )
        AND (d.Active = 1)

   ) z ORDER BY WOPK, ModuleID, DocumentID