以下是该方案: 我有桌子:
文章(articleId,userId,title,datePosted)
ArticleTranslations(languageId,articleId,title)(对于这种情况不是那么重要,但无论如何我都在展示)
ArticleSections(articleSectionId,articleId,sectionId)
部分(sectionId,content,...)
sectionsAdditionalInfo(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,但我希望每行返回一次:</ p>
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。
任何关于如何解决这个问题的提示都将非常感激;)
答案 0 :(得分:3)
如果没有完全理解您的数据和您想要的结果,这样的事情应该可以使用您的上述查询并将您的第二个查询作为子查询加入:
SELECT article.articleId,
article.userId,
ArticleTranslations.title,
article.datePosted,
t.areAllSectionsApproved
FROM Articles
LEFT OUTER JOIN ArticleTranslations
ON Article.articleId= ArticlesTranslations.articleId
AND ArticlesTranslations.languageId=@languageId
LEFT JOIN (
SELECT
articleId,
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 sectionsAdditionalInfo.isApproved=0
GROUP BY articleId
) t ON articles.articleId = t.articleId
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
祝你好运。