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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:32+00:00 2026-05-25T20:39:32+00:00

I want to optimize my sql query which i have written earlier (please see

  • 0

I want to optimize my sql query which i have written earlier (please see below attached sql query). This query is straight forward and very simple but this needs to be modified as it is failing at performance test, and I know the query is slow. My team lead did mention to me to use ‘Pivoting’ in the query but i didn’t catch his point how to pivot. Please can some one help me in this regard.

Declare @tempTable Table(
DataSourceColumID int, fDataSourceID int, seqNum int, ColName varchar(50), HeaderName varchar(50)
)
Insert into @tempTable 
(DataSourceColumID, fDataSourceID,seqNum, ColName,HeaderName) 
Select 101,1,2,'col1', 'column 1'
Union ALL
Select 102,1,1,'col2', 'column 2'
Union All
Select 103,1,3,'col6', 'column 6'
Union All
Select 104,1,4,'col50', 'column 50'
select * From @tempTable 

Declare @ColumnOrderTable table (col_A varchar(10),col_B varchar(10),col_C varchar(10),col_D varchar(10),col_E varchar(10),col_F varchar(10),col_G varchar(10))
Insert into @ColumnOrderTable (col_A ,col_B ,col_C ,col_D ,col_E ,col_F ,col_G )
select 
Case When seqNum=1 then HeaderName else '' end as col_A,
Case When seqNum=2 then HeaderName else '' end as col_B ,
Case When seqNum=3 then HeaderName else '' end as col_C ,
Case When seqNum=4 then HeaderName else '' end as col_D ,
Case When seqNum=5 then HeaderName else '' end as col_E ,
Case When seqNum=6 then HeaderName else '' end as col_F, 
Case When seqNum=7 then HeaderName else '' end as col_G
from @tempTable

select max(col_A) as col_A ,max(col_B) col_B,max(col_C) col_C,max(col_D) col_D,max(col_E) col_E,max(col_F) col_F,max(col_G) col_G  From @ColumnOrderTable 
  • 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-25T20:39:33+00:00Added an answer on May 25, 2026 at 8:39 pm

    Instead of selecting into a @ColumnOrderTable, you can ommit that step and use a subselect.

    Simplified original statement

    SELECT  MAX(col_A)
            , MAX(col_B)
            , MAX(col_C)
            , MAX(col_D)
            , MAX(col_E)
            , MAX(col_F)
            , MAX(col_G)
    FROM    (
              SELECT
                Case When seqNum=1 then HeaderName else '' end as col_A,
                Case When seqNum=2 then HeaderName else '' end as col_B ,
                Case When seqNum=3 then HeaderName else '' end as col_C ,
                Case When seqNum=4 then HeaderName else '' end as col_D ,
                Case When seqNum=5 then HeaderName else '' end as col_E ,
                Case When seqNum=6 then HeaderName else '' end as col_F, 
                Case When seqNum=7 then HeaderName else '' end as col_G
              FROM  @tempTable
            ) t
    

    The subselect itself can be ommitted by converting this statement using the PIVOT function.

    Using PIVOT

    SELECT  col_A = [1]
            , col_B = [2]
            , col_C = [3]
            , col_D = [4]
            , col_E = [5] 
            , col_F = [6]
            , col_G = [7]
    FROM    (SELECT seqNum, HeaderName FROM @tempTable) t
    PIVOT   (MAX(HeaderName) FOR seqNum IN ([1], [2], [3], [4], [5], [6], [7])) pt  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query I've written with NHibernate's Criteria functionality and I want to
I'm working on a piece of sql that I want to optimize. I have
want to have a Hyperlink-Button in a gridView in which I can display a
So I have a couple SQL commands that I basically want to make a
I want to optimize some of the SQL and just need an opinion on
I have a Django query and some Python code that I'm trying to optimize
This question is for SQL Server 2005. I have a table with 2 columns.
I am trying to optimize the search query which is the most used in
We have a database called AVL in SQL Server 2008 R2 SE. This database
So the situation is: I want to optimize my code some for doing counting

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.