3

这是场景:我有表:

  • 文章(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。

任何有关如何解决此问题的提示将不胜感激;)

4

1 回答 1

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

祝你好运。

于 2013-01-31T07:44:59.473 回答