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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:29:47+00:00 2026-05-23T14:29:47+00:00

I have the the following SQL query Declare @tempcalctbl Table ( ItemId varchar(50), ItemLocation

  • 0

I have the the following SQL query

Declare @tempcalctbl Table
(
    ItemId varchar(50),
    ItemLocation varchar(50),
    ItemNo varchar(50),
    Width real,
    Unit varchar(50),
    date datetime
)

Insert Into @tempcalctbl 
     Select distinct SubId,ItemLocation,ItemNo,
       (ABS((Select width From @temptbl a Where ItemProcess ='P1'and a.ItemId = c.ItemId 
           and a.ItemNo = c.ItemNo and a.ItemLocation = c.ItemLocation)
       -(Select width From @temptbl b Where ItemProcess ='P2' and b.ItemId = c.ItemId 
           and b.ItemNo = c.ItemNo and b.ItemLocation = c.ItemLocation))) * 1000,
       Unit,date
From @temptbl c
Group by ItemId,ItemLocation,ItemNo,Unit,date

I was wondering how to optimize this query.
The idea is to find out the different in width (p1’s item – p2’s item) between ItemProcess ‘P1’ and ‘P2’ according to the same ItemID, same ItemNo and same ItemLocation.
I have around 75000 and it took more then 25 minute to get the width differences for all the ItemId.

I tried to use Group by for the width different calculation but it would return multiple row instead of just a value which then would return error. By the way I am use MS SQL server 2008 and @tempcalctbl is a table that I declared in a store procedure.

  • 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-23T14:29:47+00:00Added an answer on May 23, 2026 at 2:29 pm

    Does the following help?

    INSERT  INTO @tempcalctbl
    SELECT  P1.SubId ,
            P1.ItemLocation ,
            P1.ItemNo ,
            ABS(P1.Width - P2.Width) * 1000 AS Width ,
            P1.Unit ,
            P1.date
    FROM    @temptbl AS P1
            INNER JOIN @temptbl AS P2 ON P1.ItemId = P2.ItemId
                                         AND P1.ItemNo = P2.ItemNo
                                         AND P1.ItemLocation = P2.ItemLocation
    WHERE   P1.ItemProcess = 'P1'
            AND P2.ItemProcess = 'P2'
    

    EDIT

    To make use of indexes, you will need to change your table variable to a temporary table

    CREATE TABLE #temptbl
    (
        ItemId varchar(50),
        ItemLocation varchar(50),
        ItemNo varchar(50),
        Width real,
        Unit varchar(50),
        date DATETIME,
        ItemProcess INT,
        SubId INT
    )
    
    CREATE NONCLUSTERED INDEX Index01 ON #temptbl
    (
        ItemProcess ASC,
        ItemId ASC,
        ItemLocation ASC,
        ItemNo ASC
    )
    INCLUDE ( SubId,Width,Unit,date)
    GO
    

    That should speed you up a little.

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

Sidebar

Related Questions

I have the following t-sql code which generates an error Declare @table TABLE (
Have added the following code to my SQL query: (Note cut down version) DECLARE
I have the following SQL query: Declare @Total_SysDown as int, @Login_SysDown as int Set
The following SQL query WORKS perfectly in SQL Management Studio: DECLARE @phone AS varchar(25)
I have the following sql DECLARE @tmpSelectedData TABLE -- table variable (SlNo INT IDENTITY(1,1)
I have the following sql query for transforming data but is it possible to
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following Sql Query that returns the type of results that I
I have the following sql query and I want to filter the results where

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.