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

  • Home
  • SEARCH
  • 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 6971973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:54:29+00:00 2026-05-27T16:54:29+00:00

Table1 Code, desc, type id 01 Rajan 1 01 Sajan 1 01 Vijayan 2

  • 0

Table1

Code, desc, type id

01    Rajan    1
01    Sajan    1
01    Vijayan  2
01    Suresh   3
01    Caresh   4
01    Sujesh   4
01    vikran   4
02    desk     1
02    card     2
02    villa    2
02    megash   2
02    supan    3
....

I want to view the table by type id wise

Expected Output

Code type-1 type-2 type-3 type-4

01   Rajan  Vijayan suresh caresh
01   Sajan  null    null   Sujan
01   null   null    null   vikran
02   desk   card    supan  null
02   null   villa   null   null
02   null   megash  null   null

How to make a query for the above condition

Need Query Help

  • 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-27T16:54:30+00:00Added an answer on May 27, 2026 at 4:54 pm

    So first off just staging your data up. Note that I’m adding an identity row id for later.

    IF OBJECT_ID('tempdb..#test') IS NOT NULL
       drop table #test
    IF OBJECT_ID('tempdb..#Numbered') IS NOT NULL
       drop table #Numbered
    
     CREATE TABLE #test (Code CHAR(2), [DESC] varchar(10), [type id] INT, RowNumber INT IDENTITY(1,1))
    
    
    INSERT #test 
        VALUES ('01', 'Rajan', 1),
               ('01' ,'Sajan', 1),
               ('01' ,'Vijayan', 2),
               ('01' ,'Suresh', 3),
               ('01' ,'Caresh', 4),
               ('01' ,'Sujesh', 4),
               ('01' ,'vikran', 4),
               ('02' ,'desk', 1),
               ('02' ,'card' ,2),
               ('02' ,'villa', 2),
               ('02', 'megash', 2),
               ('02', 'supan', 3)
    

    Then we create a holding area that uses that row id to calculate which row of the code each of the names should go on.

      CREATE TABLE #Numbered
          (
           RowNum int, Code CHAR(2), [type] VARCHAR(10), [DESC] VARCHAR(10)
           )
    
       INSERT #Numbered
             SELECT (select count(*) from #test where code=t1.Code AND [type id]=t1.[type id] AND  RowNumber<=t1.RowNumber),
                     code, 
                    [type id], 
                    [DESC]
                FROM #test t1
    

    Lastly, we create a PIVOT table on the data, (done in a standard SQL 2000 way of “faking” that operator). We then place that “PIVOT table” in a derived select that returns only the columns we want but allows us to sort on the code and rownum columns to generate the output you asked for.

         SELECT Code,[type-1],[type-2],[type-3],[type-4]
            FROM (Select P.Code,RowNum
                       , Min( Case When type = '1' Then [DESC] End ) As [type-1]
                       , Min( Case When type = '2' Then [DESC] End ) As [type-2]
                       , Min( Case When type = '3' Then [DESC] End ) As [type-3]
                       , Min( Case When type = '4' Then [DESC] End ) As [type-4]
                          From #Numbered As P
                            Group By P.Code,RowNum) R
            ORDER BY Code,RowNum
    

    Please let me know if you want further explanation on any of this.

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

Sidebar

Related Questions

I am using this code to display a grid $('#table1 tr').bind('click', shows); function shows()
When using this code (simplified for asking): var rows1 = (from t1 in db.TABLE1
I have some older (broken) code that has a join using a *= table1.ID
I have the following table mysql> desc items; +-------------------+--------------+------+-----+---------+----------------+ | Field | Type |
I have a SQL Server table with 2 columns, Code and CodeDesc. I want
I've got my database set up with three tables - code, tags, and code_tags
Should I still be using tables anyway? The table code I'd be replacing is:
i have a table with code like this: <tr> <th scope=row>5-17</th> <td>23.6 (22.0-24.0)</td> <td>18.0
I'm using a hash table (source code by Google Inc) to store some method
I am trying to access an HTML table from code behind, and set its

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.