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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:14:50+00:00 2026-05-13T21:14:50+00:00

I have the following function: CREATE FUNCTION [dbo].[ListStockBySubCategory] ( @CategoryID varchar(10), @SubCategoryID varchar(10), @startRowIndex

  • 0

I have the following function:

CREATE FUNCTION [dbo].[ListStockBySubCategory]
(   
    @CategoryID varchar(10),
    @SubCategoryID  varchar(10),
    @startRowIndex  int,
    @maximumRows    int
)
RETURNS TABLE 
AS
RETURN 
(
SELECT ISBN FROM (
SELECT ISBN, 
ROW_NUMBER() OVER(AddedDate DESC) AS RowNum
FROM (
        SELECT DISTINCT RTRIM(LTRIM(CategoryCode)) + '%' AS Pattern
        FROM tblSubCategories
        WHERE SubCategoryID = @SubCategoryID) Cats
        JOIN tblStock Stock
        ON Stock.CategoryCode LIKE Cats.Pattern 
) AS Info
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1
)

Which thanks to help from others on StackOverflow, would list all the items with a Given SubCategory – however I want to be able to include the following:

    SELECT DISTINCT RTRIM(LTRIM(CategoryCode)) + '%' AS Pattern
    FROM tblCategories
    WHERE CategoryID = @CategoryID) Cats
    JOIN tblStock Stock
    ON Stock.CategoryCode LIKE Cats.Pattern

So that it gets the Categories as a Set, then the SubCategories is a SubSet of this, for example I have a Category of EG, which contains two SubCategories EG-EG and EG-IE which themselves are a List of category codes, for example:

EG-EG
– ETC
– ECT
– TCE
EG-IE
– EIEG
– EGIE

How to I get it so it does the Categories then from this List then does thr SubCategories, as part of this I need a “NOT” behaviour as there will be a General Category which will pick up all the left over SubCategories, that are not specifically stated, but would be picked up by the Category Query.

I just cannot find the right combination for this – SubCategories and Categories work seperately but I want them to be SuperSets and SubSets of each other.


Here is the the ListStockByCategoryFunction:

CREATE FUNCTION [dbo].[ListStockByCategory]
(   
    @CategoryID varchar(10),
    @startRowIndex  int,
    @maximumRows    int
)
RETURNS TABLE AS
RETURN
(
SELECT ISBN FROM (SELECT ISBN, 
ROW_NUMBER() OVER(ORDER BY AddedDate DESC) AS RowNum
FROM (
        SELECT DISTINCT RTRIM(LTRIM(CategoryCode)) + '%' AS Pattern
        FROM tblCategory
        WHERE CategoryID = @CategoryID) Cats
        JOIN tblStock Stock
        ON Stock.CategoryCode LIKE Cats.Pattern
) AS Info
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1
)

I have a Solution which works, however the performance is not acceptable, could anyone help with this as I have been working on it for some time and cannot seem to find a way to optimise this – the SubCategories are a SubSet of the Categories if this helps, see the example below:

CREATE FUNCTION [dbo].[ListStockBySubCategory]
(   
    @CategoryID     varchar(10),
    @SubCategoryID  varchar(10),
    @startRowIndex  int,
    @maximumRows    int
)
RETURNS TABLE 
AS
RETURN (
SELECT ISBN FROM (
SELECT ISBN,
ROW_NUMBER() OVER(ORDER BY AddedDate DESC) AS RowNum
FROM BooksInStock WHERE ISBN IN
(SELECT ISBN FROM(SELECT DISTINCT RTRIM(LTRIM(CategoryCode)) + '%' AS Pattern
        FROM tblCategories
        WHERE CategoryID = @CategoryID) Cats
        JOIN tblStock Stock
        ON Stock.CategoryCode LIKE Cats.Pattern
WHERE 
ISBN IN
(SELECT ISBN FROM(SELECT DISTINCT RTRIM(LTRIM(CategoryCode)) + '%' AS Pattern
        FROM tblSubCategories
        WHERE SubCategoryID = @SubCategoryID) Cats
        JOIN tblStock Stock
        ON Stock.CategoryCode LIKE Cats.Pattern))
) AS Info WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1)
  • 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-13T21:14:51+00:00Added an answer on May 13, 2026 at 9:14 pm

    Well, its impossible to say without the table definitions, keys, indexes query plans or data examples, but I think that this will help some:

    CREATE FUNCTION [dbo].[ListStockBySubCategory]
    (   
        @CategoryID     varchar(10),
        @SubCategoryID  varchar(10),
        @startRowIndex  int,
        @maximumRows    int
    )
    RETURNS TABLE AS
    RETURN 
    (
        SELECT ISBN FROM 
        (
            SELECT  ISBN,  ROW_NUMBER() OVER(ORDER BY AddedDate DESC) AS RowNum
            FROM BooksInStock 
            WHERE EXISTS
            (        
                SELECT *
    
                FROM tblStock AS stk
                JOIN tblStock AS stk2         ON stk.ISBN = stk2.ISBN
                JOIN tblCategories AS cat     ON cat.CategoryID = @CategoryId
                JOIN tblSubCategories AS sub  ON cat.CategoryID = sub.CategoryID
    
                WHERE cat.CategoryID = @CategoryId
                  AND sub.CategoryID = @CategoryId
                  AND bis.ISBN = stk.ISBN
                  AND  stk.CategoryCode LIKE RTRIM(LTRIM(cat.CategoryCode))+'%'
                  AND stk2.CategoryCode LIKE RTRIM(LTRIM(sub.CategoryCode))+'%'
            )
        ) AS Info 
        WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @maximumRows) - 1
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function, designed to walk through XML and create a linear
I have the following code to allow users to invite thier Facebook friends: function
Have a look at the following function in a CFC (I'm using ColdFusion 9).
I must have some permissions wrong, but I can't figure out how. The following
I have a form for creating an album like following: # app/views/albums/new.html.erb <h2>Details of
I'm trying to create a function to parse out all values in a multidimensional
I have a huge difference of time execution between a 1-minute query and the
I am trying to load a file using the SQL Server Import Data function
I have a checkbox in a component: <s:CheckBox id=myCB_1 /> In my main.mxml I
a few minutes ago i asked here how to get parent records with 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.