UNION TABLE WITH VIEW没有重复行

时间:2014-07-19 07:52:41

标签: sql-server-2008

我正在尝试使用View加入一个没有任何重复行的表。但我在尝试这样做时遇到了问题。获取重复行(例如,如果视图包含列DeptManPower的空白,并且表中的相同行包含DeptManPower的值)。在这种情况下,我想从表中获取行而不是从视图。

 SELECT * FROM(
    (SELECT 
           CONVERT(NVARCHAR(50),[BOOKEDDATE],103) AS LogDate
          --,[ROWNUMBER] AS SlNo
          , ROW_NUMBER() over (order by ROWNUMBER) SlNo
          ,[STAGEID] AS TypeOfJob
          ,[DESCRIPTION] AS JobPlanning
          ,'' AS DeptManPower
          ,'' AS ExtManPower
          ,'' AS JobCompliance
          ,[PTWNO] AS PTWNo
          ,CONVERT(VARCHAR,CONVERT(DATETIME,[LASTMODIFIEDBYDATE],103),103) AS TakenDate
          ,SUBSTRING(CAST(CAST( [LASTMODIFIEDBYDATE] as time) as VARCHAR),0,6)  AS TakenTime
          ,'' AS ReturndDate
          ,'' AS ReturndTime
          ,[MaterialReq] AS SparesUsed
      FROM [EDMS_CPL].[dbo].[VW_ALL_DEFECTS]
      WHERE CONVERT(NVARCHAR(50),[BOOKEDDATE],103) ='18/07/2014' AND (STAGEID='4' OR STAGEID='21') 
     ) )A
      UNION
      SELECT* FROM
      (
      (
      SELECT 
               CONVERT(nvarchar(50),LogDate,103) AS LogDate
              --,Shift
              ,SlNo
              ,TypeOfJob 
              --,CASE TypeOfJob 
              --WHEN '4' THEN 'PTW Prepared - Without Isolation' 
              --                WHEN '21' THEN 'PTW Prepared - With Isolation'
                    --        WHEN '0' THEN 'N/A'
              --END AS TypeOfJob


              ,JobPlanning
              ,DeptManPower
              ,ExtManPower
              ,JobCompliance
              ,PTWNo
              ,CONVERT(NVARCHAR(50),TakenDate,103) AS TakenDate
              ,TakenTime
              ,CONVERT(NVARCHAR(50),ReturndDate,103) AS ReturndDate
              ,ReturndTime
              ,SparesUsed
        FROM tblMEIDLogEntry
        WHERE LogDate=CONVERT(DATETIME,'18/07/2014',103)
      ))B
    ORDER BY PTWNo


 Guide me how to do.

1 个答案:

答案 0 :(得分:0)

而是使用ORDER BY PTWNo

使用 GROUP BY

在您不想要重复数据的列上的子句。