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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:44:46+00:00 2026-06-03T03:44:46+00:00

I have a table like att1 att2 att3 att4 ——————- 1 6 11 16

  • 0

I have a table like

att1 att2 att3 att4
-------------------
1    6    11    16
2    7    12    17 
3    8    13    18
4    9    14    19 

What is the query to get the table as a coordinate system as:

a b val
-------
1 1  1
1 2  6 
1 3  11
1 4  16
2 1  2
2 2  7 
2 3  12
2 4  17
3 1  3
3 2  8
3 3  13
3 4  18
4 1  4
4 2  9
4 3  14
4 4  19

Currently I have:

select 1,att1 from data_journal_insurance_raw union all
select 2,att2 from data_journal_insurance_raw union all
select 3,att3 from data_journal_insurance_raw union all
select 4,att4 from data_journal_insurance_raw

I am missing the ‘b’ part how to do it?

  • 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-06-03T03:44:47+00:00Added an answer on June 3, 2026 at 3:44 am

    If I understand you correctly, you want to convert the table into a table containing Row, Column and Value.

    Actually, you’re missing the a part. a represents the row, and b the column. The following query should help you.

    WITH cte AS (SELECT ROW_NUMBER() OVER (ORDER BY att1) AS a, * FROM data_journal_insurance_raw)
    SELECT a, 1 as b, CAST(att1 AS VARCHAR(MAX)) FROM cte UNION ALL
    SELECT a, 2 as b, CAST(att2 AS VARCHAR(MAX)) FROM cte UNION ALL
    SELECT a, 3 as b, CAST(att3 AS VARCHAR(MAX)) FROM cte UNION ALL
    SELECT a, 4 as b, CAST(att4 AS VARCHAR(MAX)) FROM cte
    ORDER BY a, b
    

    Notice that I’m casting the value column to VARCHAR(MAX), because I don’t know what data types you have in your table. If they’re all numeric, then you can drop the CAST functions.

    If you’re only doing this once on a single table, then this will work fine for you. However, if you want to be able to do this on various tables, you can make SQL Server write the code for you. Here’s a procedure that will do just that:

    CREATE PROCEDURE usp_Coordinate_table(@TableName NVARCHAR(200)) AS
    BEGIN
        DECLARE @sql NVARCHAR(MAX);
        WITH cte AS (SELECT TOP 1 c.name FROM sys.columns c, sys.objects o WHERE c.object_id = o.object_id AND o.name = @TableName)
        SELECT @sql = 'WITH RowData AS (SELECT ROW_NUMBER() OVER (ORDER BY '+name+') AS RowNum, * FROM '+@TableName+') ' FROM cte;
    
        WITH ColData AS (SELECT ROW_NUMBER() OVER (ORDER BY c.column_id) AS ColNum, c.name 
            FROM sys.columns c, sys.objects o WHERE c.object_id = o.object_id AND o.name = @TableName)
        SELECT @sql = @sql + 'SELECT RowNum, '+CAST(c.ColNum AS VARCHAR)+' AS ColNum, CAST('+c.name+' AS VARCHAR(MAX)) AS Value FROM RowData UNION ALL '
        FROM ColData c
    
        SET @sql = SUBSTRING(@sql, 1, LEN(@sql) - 10) + ' ORDER BY RowNum, ColNum'
        EXEC sp_executesql @sql
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data in SQL Server as: att1 att2 att3 att4 att5 ...
I have a table like this: http://jsfiddle.net/DSvb7/ It currently works normally. However, when I
I have 2 tables like id att1 att2 1 34 56 2 45 12
I have a variable table: DECLARE @A_Table TABLE(ID INT, att1 VARCHAR(100), att2 nvarchar(200)) I
I have table like this -> http://jsfiddle.net/jurisKaste/FvgeQ/ And I need to get array of
I have problem with sql query. For example, I have table like this: ID
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
I have a table like this: name code group john 12 smith 15 how

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.