sql select query的问题

时间:2011-11-29 01:32:56

标签: sql sql-server tsql union

我有以下查询

SELECT DISTINCT dr.Revision 
FROM tblActionHeader ah 
    INNER JOIN tblActionType at ON at.ActionTypeID = ah.ActionTypeID 
    INNER JOIN tblDocumentRevisionActionHeader drah ON drah.ActionHeaderID = ah.ActionHeaderID 
    INNER JOIN tblDocumentRevision dr on dr.DocumentRevisionID = drah.DocumentRevisionID 
    INNER JOIN tblDocumentHeader dh on dh.DocumentHeaderID = dr.DocumentHeaderID 
WHERE at.ActionTypeID=2 
UNION SELECT '(All)' 
      FROM tblActionHeader ah

返回以下结果集

-
(All)
0
0a
1

我需要'(全部)'取得结果集的顶部我该怎么做?

4 个答案:

答案 0 :(得分:5)

两种方式 - 首先是将“All”放在UNION的顶部:

SELECT '(All)' AS revision
UNION ALL
SELECT DISTINCT dr.Revision 
  FROM tblActionHeader ah 
  JOIN tblActionType at ON at.ActionTypeID = ah.ActionTypeID 
  JOIN tblDocumentRevisionActionHeader drah ON drah.ActionHeaderID = ah.ActionHeaderID   
  JOIN tblDocumentRevision dr ON dr.DocumentRevisionID = drah.DocumentRevisionID 
  JOIN tblDocumentHeader dh ON dh.DocumentHeaderID = dr.DocumentHeaderID 
 WHERE at.ActionTypeID = 2 

第二种是通过以下方式为订购分配任意值:

SELECT x.revision
  FROM (SELECT '(All)' AS revision, 1 AS sort_order 
         UNION ALL
        SELECT DISTINCT dr.Revision, 2 AS sort_order 
          FROM tblActionHeader ah 
          JOIN tblActionType at ON at.ActionTypeID = ah.ActionTypeID 
          JOIN tblDocumentRevisionActionHeader drah ON drah.ActionHeaderID = ah.ActionHeaderID   
          JOIN tblDocumentRevision dr ON dr.DocumentRevisionID = drah.DocumentRevisionID 
          JOIN tblDocumentHeader dh ON dh.DocumentHeaderID = dr.DocumentHeaderID 
         WHERE at.ActionTypeID = 2) x
ORDER BY x.sort_order, x.revision

我在此示例中使用了派生表方法,以确保只有revision列位于最终结果集中。

如果您知道值将是不同/唯一的,则使用UNION ALL - UNION会删除重复项,并且由于这个原因速度较慢。

答案 1 :(得分:3)

尝试

SELECT '(All)' from tblActionHeader ah
union all
select distinct dr.Revision from tblActionHeader ah inner join tblActionType at 
       on at.ActionTypeID = ah.ActionTypeID 
   inner join tblDocumentRevisionActionHeader drah 
   on drah.ActionHeaderID = ah.ActionHeaderID inner join tblDocumentRevision dr 
   on dr.DocumentRevisionID = drah.DocumentRevisionID inner join tblDocumentHeader dh 
   on dh.DocumentHeaderID = dr.DocumentHeaderID where at.ActionTypeID=2 

答案 2 :(得分:1)

SELECT Revision
FROM( 
    SELECT DISTINCT dr.Revision 
    FROM tblActionHeader ah 
        INNER JOIN tblActionType at ON at.ActionTypeID = ah.ActionTypeID 
        INNER JOIN tblDocumentRevisionActionHeader drah ON drah.ActionHeaderID = ah.ActionHeaderID 
        INNER JOIN tblDocumentRevision dr on dr.DocumentRevisionID = drah.DocumentRevisionID 
        INNER JOIN tblDocumentHeader dh on dh.DocumentHeaderID = dr.DocumentHeaderID 
    WHERE at.ActionTypeID=2 
    UNION SELECT '(All)' AS Revision
    ) tempT
ORDER BY Revision DESC

答案 3 :(得分:0)

不确定你是否在寻找这个?

select top 1 as All from 
(
select distinct dr.Revision from tblActionHeader ah inner join tblActionType at 
on at.ActionTypeID = ah.ActionTypeID 
 inner join tblDocumentRevisionActionHeader drah 
 on drah.ActionHeaderID = ah.ActionHeaderID inner join tblDocumentRevision dr 
on dr.DocumentRevisionID = drah.DocumentRevisionID inner join tblDocumentHeader dh 
on dh.DocumentHeaderID = dr.DocumentHeaderID where at.ActionTypeID=2
)