2011-11-29 122 views
1

我有以下查詢問題與SQL SELECT查詢

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 

我需要「(全部)」取於結果的頂部設置如何我可以這樣做嗎?

+0

使用order by子句? – Bert

回答

5

兩種方式 - 第一是把「所有」排在聯盟的頂部:

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會刪除重複項,並且由於這個原因會更慢。

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 
) 
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 
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