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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:39:40+00:00 2026-05-15T04:39:40+00:00

Alright, first you’ll need to grab a barf bag. I’ve been tasked with optimizing

  • 0

Alright, first you’ll need to grab a barf bag. I’ve been tasked with optimizing several old stored procedures in our database. This SP does the following:

1) cursor loops through a series of “buildings”

2) cursor loops through a week, Sunday-Saturday

3) has a huge set of IF blocks that are responsible for counting how many Objects of what Types are present in a given building

Essentially what you’ll see in this code block is that, if there are 5 objects of type #2, it will increment @Type_2_Objects_5 by 1.

    IF @Number_Type_1_Objects = 0
    BEGIN
        SET @Type_1_Objects_0 = @Type_1_Objects_0 + 1
    END
    IF @Number_Type_1_Objects = 1
    BEGIN
        SET @Type_1_Objects_1 = @Type_1_Objects_1 + 1
    END
    IF @Number_Type_1_Objects = 2
    BEGIN
        SET @Type_1_Objects_2 = @Type_1_Objects_2 + 1
    END
    IF @Number_Type_1_Objects = 3
    BEGIN
        SET @Type_1_Objects_3 = @Type_1_Objects_3 + 1
    END
[... Objects_4 through Objects_20 for Type_1]

IF @Number_Type_2_Objects = 0
    BEGIN
        SET @Type_2_Objects_0 = @Type_2_Objects_0 + 1
    END
    IF @Number_Type_2_Objects = 1
    BEGIN
        SET @Type_2_Objects_1 = @Type_2_Objects_1 + 1
    END
    IF @Number_Type_2_Objects = 2
    BEGIN
        SET @Type_2_Objects_2 = @Type_2_Objects_2 + 1
    END
    IF @Number_Type_2_Objects = 3
    BEGIN
        SET @Type_2_Objects_3 = @Type_2_Objects_3 + 1
    END
[... Objects_4 through Objects_20 for Type_2]

In addition to being extremely hacky (and limited to a quantity of 20 objects), it seems like a terrible way of handling this. In a traditional language, this could easily be solved with a 2-dimensional array…

objects[type][quantity] += 1;

I’m a T-SQL novice, but since writing stored procedures often uses a lot of temporary tables (which could essentially be a 2-dimensional array) I was wondering if someone could illuminate a better way of handling a situation like this with two dynamic pieces of data to store.

Requested in comments


The columns are simply

  • Number_Type_1_Objects
  • Number_Type_2_Objects
  • Number_Type_3_Objects
  • Number_Type_4_Objects
  • Number_Type_5_Objects
  • CurrentDateTime.

Each row in the table represents 5 minutes.

The expected output is to figure out what percentage of time a given quantity of objects is present throughout each day.

Sunday - Object Type 1 
0 objects - 69 rows, 5:45, 34.85% 
1 object - 85 rows, 7:05, 42.93% 
2 objects - 44 rows, 3:40, 22.22%

On Sunday, there were 0 objects of type 1 for 34.85% of the day. There was 1 object for 42.93% of the day, and 2 objects for 22.22% of the day. Repeat for each object type.

  • 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-15T04:39:42+00:00Added an answer on May 15, 2026 at 4:39 am

    a quick and easy solution that would get you close to where you want to be would be to do a union similar to

    select 'type1objects' as objecttype
           datepart(dw, currentdatetime) as dayofweek, 
           number_type_1_objects as numberofobjects, 
           count(number_type_1_objects) as rows
    from yourtable
    group by convert(varchar(10), currentdatetime,101), datepart(dw, currentdatetime), number_type_1_objects
    union
    select 'type2objects' as objecttype
           datepart('d',currentdatetime) as dayofweek,
           number_type_2_objects as numberofobjects, 
           count(*) as rows
    from yourtable
    group by convert(varchar(10),currentdatetime,101), datepart(dw,currentdatetime), number_type_2_objects
    

    and so on for each object column. that should get you an approximation of the data you are getting now without the use of those crazy variables and cursors, buildings not withstanding. you could probably join the buildings table in though, just adding it to the group by and be done with that as well. not sure though as i have no idea what the relationship would be drawn on…

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

Sidebar

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.