Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:53:30+00:00 2026-06-18T01:53:30+00:00

Here is the scenario: I have tables: Articles (articleId, userId, title, datePosted) ArticleTranslations (languageId,

  • 0

Here is the scenario:
I have tables:

  • Articles (articleId, userId, title, datePosted)

  • ArticleTranslations (languageId, articleId, title) (not so important for this case, but I am showing anyway)

  • ArticleSections (articleSectionId, articleId, sectionId)

  • sections (sectionId, content, …)

  • sectionsAdditionalInfo (sectionId, isApproved)

What I am doing is selecting some articles from articles by userId 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 bare in mind I left out some of the info in order for my question to be more understandable.

Now, what I would like to do is select another attribute (for each article returned in SQL above): areAllArticleSectionsApproved

I have assembled 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 have tried nesting this SQL, in a way:

    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 solve this would be greatly appreciated 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:53:31+00:00Added an answer on June 18, 2026 at 1:53 am

    Without fully understanding your data and your desired results, something like this should work using your above query and joining on 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
Here is the scenario I have: 5 tables. Author, Publisher, Region, Medium & Title
Here's the scenario: I have 2 tables with data, one is the 2009 version
Here is my scenario: I have two MySQL tables: Categories (columns: id, category) Items
Here's my scenario: I have two tables A, B which (for the sake of
Here is the scenario: I have a brand new database, with the tables with
Here is the scenario: I have 2 tables and 2 temporary tables. Before I
Here is my scenario: I have two tables: subfirms and subfirmdetails. I am trying
Here's the scenario -- I have two tables Names & Sentences: Names Sentences ID
Here's the scenario: I have a public repo A . Bob forks A ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.