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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:18:03+00:00 2026-05-26T07:18:03+00:00

So, i have 2 tables: Categories table Analit int PositionInMenu int Item table Analit

  • 0

So, i have 2 tables:

Categories table

Analit  int 
PositionInMenu  int 

Item table

Analit  int 
CategoryAn  int 

So, i have about 50 categories and 2000 items. I need to take for each category count of items it includes.

1)SELECT *, (SELECT COUNT(Analit) FROM Item_table t2 WHERE t2.CategoryAn = t1.Analit) as tCount FROM Categories_table t1 ORDER BY PositionInMenu

2) Add into Categories table computed column with function call:

([dbo].[Categories_GetItemsCountInCategory]([Analit]))

and function code:

CREATE FUNCTION dbo.Categories_GetItemsCountInCategory
    (   
    @categoryId int = null
    )
RETURNS int
AS
    BEGIN
    RETURN (SELECT COUNT(Analit) 
            FROM Items 
            WHERE CategoryAn = @categoryId)
    END

And then i can simply take value of added column into my query:

SELECT *
FROM Categories_table
ORDER BY PositionInMenu

So, the question. What’s better for me?

  • 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-26T07:18:04+00:00Added an answer on May 26, 2026 at 7:18 am

    You are pretty much always better off having data access inline in the query rather than in scalar UDFs.

    The query optimiser does not expand out scalar (or multi statement) UDFs so you always enforce a nested loops join plan rather than allowing it to consider alternatives.

    You could also consider an OUTER JOIN ... GROUP BY rather than a correlated subquery.

    SELECT t1.*,
           ISNULL(tCount, 0) AS tCount
    FROM   Categories_table t1
           LEFT JOIN (SELECT COUNT(Analit) AS tCount,
                             CategoryAn
                      FROM   Item_table
                      GROUP  BY CategoryAn) t2
             ON t2.CategoryAn = t1.Analit
    ORDER  BY PositionInMenu  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables as follows: Table user_prefs: user_prefs_id int(11) PK user_id int(11) item
I have two tables ITEMS : id, name CATEGORIES: items_id, category I need to
I have a database with the following tables: create table Categories ( Id int
I have a Categories table in which each category has a ParentId that can
I have a table of categories. Each category can either be a root level
I have the following tables: 1-Categories: -CategoryID -CategoryName -ParentID 2-Items: -ItemId -ItemName -CategoryID categories
Now I have 4 tables: table categories: -id -category table products: -id -product -price
I have 2 tables: products and categories . Each category has many products, and
i have 2 tables for categories & posts the categories table contain id name
I have tables with listings, categories and one that maps them to each other.

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.