TSQL按subselect的值排序结果

时间:2014-07-15 21:43:04

标签: sql sql-server tsql stored-procedures

我有一个查询从一个表中提取提交,然后从另一个表中提取投票数。然后,我想根据从子查询中收集的总票数来订购记录。

如何通过在子查询中收集的totalVotes来订购此表?

SELECT A.[submissionID],
       A.[entryID],
       E.[subEmpID],
       E.[nomineeEmpID],
       CONVERT (VARCHAR (10), E.[submissionDate], 101) AS submissionDate,
       E.[situation],
       E.[task],
       E.[action],
       E.[result],
       E.[timestamp],
       E.[statusID],
       E.[approver],
       E.[approvalDate],
       B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
       B.[ntid] AS nomineeNTID,
       B.[qid] AS nomineeQID,
       C.[FirstName] + ' ' + C.[LastName] AS submitName,
       C.[ntid] AS submitNTID,
       D.[categoryName],
       (
            SELECT count(G.[empID]) as totalVotes FROM empowermentVotes as G WHERE entryID = A.[entryID]
            ORDER BY totalVotes ASC
            FOR    XML PATH (''), TYPE, ELEMENTS
       ) 
FROM   empowermentEntries AS A
       INNER JOIN empowermentSubmissions as E
       ON A.[submissionID] = E.[submissionID]
       INNER JOIN
       empTable AS B
       ON E.[nomineeEmpID] = B.[empID]
       INNER JOIN
       empTable AS C
       ON E.[subEmpID] = C.[empID]
       INNER JOIN
       empowermentCategories AS D
       ON E.[categoryID] = D.[catID]
WHERE  A.[sessionID] = @sessionID
FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');

4 个答案:

答案 0 :(得分:0)

您可以通过ORDER BY(在您的情况下为19)或可能通过别名来执行此操作。但是,SQL并不总是允许您这样做。它可能会引发错误"必须至少有一个非外部参考..." (无论如何都是这样的)。如果是这种情况,您可能必须将整个查询包装在CTE或子查询中,并将顺序包装在外部查询中。另外一个选择是在中间层订购。

答案 1 :(得分:0)

正如Gordon Linoff在评论中所说的那样,给列添加一个别名,并在你的ORDER BY中引用它。

示例:

SELECT A.SubmissionID,
 ...
 ...
(
    SELECT count(G.[empID]) as totalVotes FROM empowermentVotes as G WHERE entryID = A.[entryID]
     ORDER BY totalVotes ASC
           FOR    XML PATH (''), TYPE, ELEMENTS
 ) AS TotalVotes
FROM empowermentEntries AS A
...
...
WHERE blah blah blah
ORDER BY TotalVotes

答案 2 :(得分:0)

SELECT * FROM
(
SELECT A.[submissionID],
       A.[entryID],
       E.[subEmpID],
       E.[nomineeEmpID],
       CONVERT (VARCHAR (10), E.[submissionDate], 101) AS submissionDate,
       E.[situation],
       E.[task],
       E.[action],
       E.[result],
       E.[timestamp],
       E.[statusID],
       E.[approver],
       E.[approvalDate],
       B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
       B.[ntid] AS nomineeNTID,
       B.[qid] AS nomineeQID,
       C.[FirstName] + ' ' + C.[LastName] AS submitName,
       C.[ntid] AS submitNTID,
       D.[categoryName],
       (
            SELECT count(G.[empID]) as totalVotes FROM empowermentVotes as G WHERE entryID = A.[entryID]
            ORDER BY totalVotes ASC
            FOR    XML PATH (''), TYPE, ELEMENTS
       ) As TotalVotes
FROM   empowermentEntries AS A
       INNER JOIN empowermentSubmissions as E
       ON A.[submissionID] = E.[submissionID]
       INNER JOIN
       empTable AS B
       ON E.[nomineeEmpID] = B.[empID]
       INNER JOIN
       empTable AS C
       ON E.[subEmpID] = C.[empID]
       INNER JOIN
       empowermentCategories AS D
       ON E.[categoryID] = D.[catID]
WHERE  A.[sessionID] = @sessionID
FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root')
) tmp
ORDER BY tmp.TotalVotes

答案 3 :(得分:0)

您可能需要在FOR XML之前订购它:

SELECT(
SELECT A.[submissionID],
       A.[entryID],
       E.[subEmpID],
       E.[nomineeEmpID],
       CONVERT (VARCHAR (10), E.[submissionDate], 101) AS submissionDate,
       E.[situation],
       E.[task],
       E.[action],
       E.[result],
       E.[timestamp],
       E.[statusID],
       E.[approver],
       E.[approvalDate],
       B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
       B.[ntid] AS nomineeNTID,
       B.[qid] AS nomineeQID,
       C.[FirstName] + ' ' + C.[LastName] AS submitName,
       C.[ntid] AS submitNTID,
       D.[categoryName],
       (
            SELECT count(G.[empID]) as totalVotes FROM empowermentVotes as G WHERE entryID = A.[entryID]
            ORDER BY totalVotes ASC
            FOR    XML PATH (''), TYPE, ELEMENTS
       ) votes
FROM   empowermentEntries AS A
       INNER JOIN empowermentSubmissions as E
       ON A.[submissionID] = E.[submissionID]
       INNER JOIN
       empTable AS B
       ON E.[nomineeEmpID] = B.[empID]
       INNER JOIN
       empTable AS C
       ON E.[subEmpID] = C.[empID]
       INNER JOIN
       empowermentCategories AS D
       ON E.[categoryID] = D.[catID]
WHERE  A.[sessionID] = @sessionID
ORDER BY votes
FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root')
)

请注意外部选择和别名已添加