ORDER BY CASE WHEN 不能用于 COUNT() AS Foo 结果列?
ORDER BY CASE WHEN can't be used on an COUNT() AS Foo result column?
我有一个查询,我试图允许基于 C# SQL 服务器客户端库传入的参数进行排序。我的查询目前看起来像这样...
SELECT d.OpinionDocumentId,
d.DisplayTitle,
d.Court,
d.State,
d.Date,
(SELECT i.Identifier + ', '
FROM dbo.OpinionDocumentCitationIdentifiers i
WHERE i.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
FOR XML PATH('')) AS Identifiers,
COUNT(DISTINCT c.OpinionDocument_OpinionDocumentId1) AS CitationCount,
COUNT(DISTINCT t.OpinionDocument_OpinionDocumentId) AS CitedByCount
FROM dbo.OpinionDocumentBookmarks b
INNER JOIN dbo.OpinionDocuments d ON d.OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments c ON c.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments t ON t.OpinionDocument_OpinionDocumentId1 = b.OpinionDocument_OpinionDocumentId
WHERE b.Account_AccountId = @accountId
GROUP BY b.OpinionDocumentBookmarkId,
b.OpinionDocument_OpinionDocumentId,
d.OpinionDocumentId,
d.DisplayTitle,
d.Court,
d.State,
d.Date
ORDER BY CASE WHEN @sort = 'DisplayTitle' THEN d.DisplayTitle END,
CASE WHEN @sort = 'Date' THEN d.Date END,
CASE WHEN @sort = 'Court' THEN d.Court END,
CASE WHEN @sort = 'State' THEN d.State END,
CASE WHEN @sort = 'Citations' THEN CitationCount END
OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY
如您所见,我试图允许基于参数进行排序。我正在尝试允许对聚合的 CitationCount
或 CitedByCount
列进行排序。当我不使用 ORDER BY CASE WHEN
时,我可以按这些列排序,但是当我尝试将其指定为 ORDER BY CASE WHEN
中的列时,我收到一条错误消息,提示它不是一列。
这可能吗?
SELECT *
FROM
(
SELECT
d.OpinionDocumentId
, d.DisplayTitle
, d.Court
, d.[State]
, d.[Date]
, (
SELECT i.Identifier + ', '
FROM dbo.OpinionDocumentCitationIdentifiers AS i
WHERE i.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
FOR XML PATH('')
) AS Identifiers
, COUNT(DISTINCT c.OpinionDocument_OpinionDocumentId1) AS CitationCount
, COUNT(DISTINCT t.OpinionDocument_OpinionDocumentId) AS CitedByCount
FROM
dbo.OpinionDocumentBookmarks AS b
INNER JOIN dbo.OpinionDocuments AS d
ON d.OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments AS c
ON c.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments AS t
ON t.OpinionDocument_OpinionDocumentId1 = b.OpinionDocument_OpinionDocumentId
WHERE b.Account_AccountId = @accountId
GROUP BY
b.OpinionDocumentBookmarkId
, b.OpinionDocument_OpinionDocumentId
, d.OpinionDocumentId
, d.DisplayTitle
, d.Court
, d.State
, d.Dat
) AS H
ORDER BY
CASE WHEN @sort = 'DisplayTitle' THEN DisplayTitle END,
CASE WHEN @sort = 'Date' THEN [Date] END,
CASE WHEN @sort = 'Court' THEN Court END,
CASE WHEN @sort = 'State' THEN [State] END,
CASE WHEN @sort = 'Citations' THEN CitationCount END
我有一个查询,我试图允许基于 C# SQL 服务器客户端库传入的参数进行排序。我的查询目前看起来像这样...
SELECT d.OpinionDocumentId,
d.DisplayTitle,
d.Court,
d.State,
d.Date,
(SELECT i.Identifier + ', '
FROM dbo.OpinionDocumentCitationIdentifiers i
WHERE i.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
FOR XML PATH('')) AS Identifiers,
COUNT(DISTINCT c.OpinionDocument_OpinionDocumentId1) AS CitationCount,
COUNT(DISTINCT t.OpinionDocument_OpinionDocumentId) AS CitedByCount
FROM dbo.OpinionDocumentBookmarks b
INNER JOIN dbo.OpinionDocuments d ON d.OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments c ON c.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments t ON t.OpinionDocument_OpinionDocumentId1 = b.OpinionDocument_OpinionDocumentId
WHERE b.Account_AccountId = @accountId
GROUP BY b.OpinionDocumentBookmarkId,
b.OpinionDocument_OpinionDocumentId,
d.OpinionDocumentId,
d.DisplayTitle,
d.Court,
d.State,
d.Date
ORDER BY CASE WHEN @sort = 'DisplayTitle' THEN d.DisplayTitle END,
CASE WHEN @sort = 'Date' THEN d.Date END,
CASE WHEN @sort = 'Court' THEN d.Court END,
CASE WHEN @sort = 'State' THEN d.State END,
CASE WHEN @sort = 'Citations' THEN CitationCount END
OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY
如您所见,我试图允许基于参数进行排序。我正在尝试允许对聚合的 CitationCount
或 CitedByCount
列进行排序。当我不使用 ORDER BY CASE WHEN
时,我可以按这些列排序,但是当我尝试将其指定为 ORDER BY CASE WHEN
中的列时,我收到一条错误消息,提示它不是一列。
这可能吗?
SELECT *
FROM
(
SELECT
d.OpinionDocumentId
, d.DisplayTitle
, d.Court
, d.[State]
, d.[Date]
, (
SELECT i.Identifier + ', '
FROM dbo.OpinionDocumentCitationIdentifiers AS i
WHERE i.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
FOR XML PATH('')
) AS Identifiers
, COUNT(DISTINCT c.OpinionDocument_OpinionDocumentId1) AS CitationCount
, COUNT(DISTINCT t.OpinionDocument_OpinionDocumentId) AS CitedByCount
FROM
dbo.OpinionDocumentBookmarks AS b
INNER JOIN dbo.OpinionDocuments AS d
ON d.OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments AS c
ON c.OpinionDocument_OpinionDocumentId = b.OpinionDocument_OpinionDocumentId
LEFT JOIN dbo.OpinionDocumentOpinionDocuments AS t
ON t.OpinionDocument_OpinionDocumentId1 = b.OpinionDocument_OpinionDocumentId
WHERE b.Account_AccountId = @accountId
GROUP BY
b.OpinionDocumentBookmarkId
, b.OpinionDocument_OpinionDocumentId
, d.OpinionDocumentId
, d.DisplayTitle
, d.Court
, d.State
, d.Dat
) AS H
ORDER BY
CASE WHEN @sort = 'DisplayTitle' THEN DisplayTitle END,
CASE WHEN @sort = 'Date' THEN [Date] END,
CASE WHEN @sort = 'Court' THEN Court END,
CASE WHEN @sort = 'State' THEN [State] END,
CASE WHEN @sort = 'Citations' THEN CitationCount END