ここにシナリオがあります: 私はテーブルを持っています:
記事 (articleId、userId、title、datePosted)
ArticleTranslations (languageId, articleId, title) (この場合はそれほど重要ではありませんが、とにかく表示しています)
ArticleSections (articleSectionId、articleId、sectionId)
セクション (sectionId、コンテンツなど)
sectionAdditionalInfo (sectionId、isApproved)
私がやっていることは、ある方法で userId によって記事からいくつかの記事を選択することです:
SELECT article.articleId, article.userId, ArticleTranslations.title, article.datePosted
FROM Articles
LEFT OUTER JOIN ArticleTranslations ON Article.articleId= ArticlesTranslations.articleId AND ArticlesTranslations.languageId=@languageId
WHERE Articloes.userId=@userId
ORDER BY
CASE WHEN @sortDirection = 'DD' THEN datePosted END DESC, -- by date posted
CASE WHEN @sortDirection = 'DA' THEN datePosted END,
CASE WHEN @sortDirection = 'ND' THEN title END DESC, -- sort by name
CASE WHEN @sortDirection = 'NA' THEN title END,
CASE WHEN @sortDirection = 'SD' THEN ArticleTranslations.isApproved END DESC, -- is article aproved?
CASE WHEN @sortDirection = 'SA' THEN ArticleTranslations.isApproved END,
CASE WHEN @sortDirection = 'ID' THEN areAllSectionsApproved END DESC, -- sort by information if sections are all approved - within the article?
CASE WHEN @sortDirection = 'IA' THEN areAllSectionsApproved END
私の質問をより理解しやすくするために、情報の一部を省略したことを心に留めておいてください。
ここで、別の属性を選択します (上記の SQL で返された記事ごとに): areAllArticleSectionsApproved
SQL を個別に組み立てましたが、すべての行に対してこれが返されるようにしたいと思います。
SELECT CASE WHEN COUNT(sectionsAdditionalInfo.sectionId) > 0 THEN 0 ELSE 1 END AS areAllSectionsApproved
FROM ArticleSections
LEFT OUTER JOIN sectionsAdditionalInfo ON ArticleSections.sectionId = sectionsAdditionalInfo.sectionId
WHERE articleId=@articleId AND sectionsAdditionalInfo.isApproved=0
ある方法で、このSQLをネストしてみました:
SELECT (outer SQL) .....
a.*(
nested SQL - second one I posted here
) as a
しかし、まったく機能しませんでした。
SQL Server 2008 を使用しています。
これを解決する方法についてのヒントは大歓迎です;)