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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:57:18+00:00 2026-05-30T20:57:18+00:00

when i issue select statement like SELECT EMPID,ENAME,SALARY,DEPARTMENT from EMPLOYEE then data shows like

  • 0

when i issue select statement like

SELECT EMPID,ENAME,SALARY,DEPARTMENT from EMPLOYEE

then data shows like

EMPID     ENAME    SALARY   DEPARTMENT
---------------------------------------------
01        TEST1    2000     A/C
02        TEST2    3000     SALES

but now i want to show it like

EMPID       01      02

ENAME       TEST1   TEST2

SALARY      2000    3000

DEPARTMENT  A/C     SALES

so how to write sql for the above query.

  • 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-30T20:57:20+00:00Added an answer on May 30, 2026 at 8:57 pm

    Maybe something like this:

    First some test data:

    CREATE TABLE tblTempValues
        (
            EMPID VARCHAR(100), 
            ENAME VARCHAR(100),
            SALARY INT, 
            DEPARTMENT VARCHAR(100)
        )
    
    INSERT INTO tblTempValues
    VALUES
        ('01','TEST1',2000,'A/C'),
        ('02','TEST2',3000,'SALES')
    

    The get the columns to PIVOT on. I use the ROW_NUMBER over the EMPID:

    DECLARE @cols VARCHAR(MAX)
    ;WITH CTE
    AS
    (
        SELECT
            ROW_NUMBER() OVER(ORDER BY tblTempValues.EMPID) AS RowNbr
        FROM
            tblTempValues
    )
    SELECT
        @cols=COALESCE(@cols +','+QUOTENAME(RowNbr),QUOTENAME(RowNbr))
    FROM
        CTE
    

    Then do a dynamic pivot like this. The orderWeight so that you will have the order of the columns:

    DECLARE @query NVARCHAR(4000)=
    N';WITH CTE
    AS
    (
        SELECT ''EMPID'' AS ID, CAST(EMPID AS VARCHAR(MAX)) AS Value, ROW_NUMBER() OVER(ORDER BY EMPID) AS RowId,1 as orderWeight FROM tblTempValues UNION ALL
        SELECT ''ENAME'' AS ID, CAST(ENAME AS VARCHAR(MAX)) AS Value, ROW_NUMBER() OVER(ORDER BY EMPID) AS RowId,2 as orderWeight FROM tblTempValues UNION ALL
        SELECT ''SALARY'' AS ID, CAST(SALARY AS VARCHAR(MAX)) AS Value, ROW_NUMBER() OVER(ORDER BY EMPID) AS RowId,3 as orderWeight FROM tblTempValues UNION ALL
        SELECT ''DEPARTMENT'' AS ID, CAST(DEPARTMENT AS VARCHAR(MAX)) AS Value, ROW_NUMBER() OVER(ORDER BY EMPID) AS RowId,4 as orderWeight FROM tblTempValues
    )
    SELECT
        ID,'+@cols+'
    FROM
        CTE
    PIVOT
    (
        MAX(Value)
        FOR RowId IN('+@cols+')
    
    ) AS p'
    
    EXECUTE(@query)
    

    Then in my case I will drop the temp table

    DROP TABLE tblTempValues
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue regarding to auto populating a select dropdown from jQuery/JSON data
We've encountered the following issue. I like to use the following writing: SELECT Id,
I was browsing through the questions and noticed this: SELECT prodid, issue FROM Sales
If I have a SQL statement such as: SELECT TOP 5 * FROM Person
I have a .NET application which executes a statement like this: SELECT ST_GeomFromKML(' <LineString>
My issue is that I have a Select statement that has a where clause
Here's the issue. I have a prepared statement, like this: $select_something = $db->stmt_init(); $select_something->prepare
I'm having trouble retrieving data from my database using Spring Jdbc. Here's my issue:
I have a SQL Statement that works as I want. select COUNT(*), MIN(emailed_to)from email.email_archive
I'm not getting any errors as such just a minor performance issue. EXPLAIN SELECT

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.