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 6216543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:16:00+00:00 2026-05-24T07:16:00+00:00

My objective is to write a query that will return all the Categories with

  • 0

My objective is to write a query that will return all the Categories with 3 flag fields, like this:

ID | En | Ru | Fr

The language flags must be ON or OFF according to if is any Lesson record associated with the category.

My current statement is this:

SELECT c.ID,
    (CASE WHEN c.ID IN (SELECT c.ID FROM Lessons AS l WHERE l.Category_Id = c.ID AND l.Language_Id = 1) THEN 1 ELSE 0 END) AS En,
    (CASE WHEN c.ID IN (SELECT c.ID FROM Lessons AS l WHERE l.Category_Id = c.ID AND l.Language_Id = 2) THEN 1 ELSE 0 END) AS Ru,
    (CASE WHEN c.ID IN (SELECT c.ID FROM Lessons AS l WHERE l.Category_Id = c.ID AND l.Language_Id = 3) THEN 1 ELSE 0 END) AS Fr
FROM LessonCategories AS c

The problem is that this query is VERY slow, since the Lessons table has more than 60,000 records, and i’m running over it 3 times.

I’m looking for a way to make this query more efficient.
I had a thought to use grouping on join between Categories and lessons but i dont know exactly how and if it is even possible.

The pseudo-code for more fast query is:

SELECT c.[ID], 
    COUNT(l.Language_Id = 1) > 0 AS En
    COUNT(l.Language_Id = 2) > 0 AS Ru
    COUNT(l.Language_Id = 3) > 0 AS Fr
FROM CategoryTreeView AS c
INNER JOIN Lessons AS l ON l.Category_Id = c.ID
GROUP BY c.[ID]

It it possible to express this using valid t-sql?
Or is any better way to handle this kind of query?

P.S. If it helps, i don’t care to get one bitwise flag field instead of the 3 language fields.

Thanks.

  • 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-05-24T07:16:02+00:00Added an answer on May 24, 2026 at 7:16 am

    Well you have just to combine your two queries 😉

    SELECT c.ID, 
           SUM(case when l.Language_Id = 1 then 1 else 0) AS En
           SUM(case when l.Language_Id = 2 then 1 else 0) AS Ru
           SUM(case when l.Language_Id = 3 then 1 else 0) AS Fr
    FROM CategoryTreeView AS c
    INNER JOIN Lessons AS l ON l.Category_Id = c.ID
    GROUP BY c.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My objective is to write a program which will call another executable on a
I would like to write some Objective-C code for distribution. What is the best
I have recently started to learn Objective-C and write my tests using OCUnit that
Actually I have read about that I can write Objective-C app on Linux (using
So I'm trying to write an Objective-C macro that takes an object and a
I am trying to write a query that rates how well a particular object
I would like to write a fuzzy date method for calculating dates in Objective-C
I am working my way through some Objective-C code that I did not write
I'm trying to write a query where I'd like to get a list of
I'd like to write an Objective-C class without Cocoa or GNU's Object.h (for educational

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.