T SQL Chain joins 4 tables
Here's the scenario: I have tables:
-
Articles (articleId, userId, title, datePosted)
-
ArticleTranslations (languageId, articleId, title) (not that important for this case, but I show it anyway)
-
ArticleSections (articleSectionId, articleId, sectionId)
-
sections (sectionId, content, ...)
-
sectionsAdditionalInfo (sectionId, isApproved)
What I am doing is selecting some articles from userId articles in a way:
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
Please note that I have left some of the information to make my question clearer.
Now what I would like to do is select a different attribute (for each article returned in the SQL above): areAllArticleSectionsApproved
I have compiled the SQL separately, but I would like this to be returned for every row:
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
I tried to nest this SQL in some sense:
SELECT (outer SQL) .....
a.*(
nested SQL - second one I posted here
) as a
but it didn't work at all.
I am using SQL Server 2008.
Any hint on how to fix this would be greatly appreciated;)
source to share
Without fully understanding your data and the desired results, something like this should work using your above query and joining your second query as a subquery:
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
Good luck.
source to share