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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:57:38+00:00 2026-05-25T00:57:38+00:00

SELECT Name1, Name2, Value FROM mytable gives me the following result set: Name1 Name2

  • 0

SELECT Name1, Name2, Value FROM mytable gives me the following result set:

Name1 Name2 Value
A     P1     1
A     P2     1
A     P3     2
B     P1     3
B     P2     1
B     P4     1

How do I translate that to:

       A     B
P1     1     4
P2     1     1
P3     2     null
P4     null  1

Thanks,

  • 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-25T00:57:38+00:00Added an answer on May 25, 2026 at 12:57 am

    You can use a PIVOT clause. Your query could be something like this:

    WITH Source as (
        SELECT Name1, Name2, [Value]
        FROM mytable 
    )
    
    SELECT Name2, CASE WHEN A IS NOT NULL THEN A ELSE 'your string' END As A
    , CASE WHEN B IS NOT NULL THEN B ELSE 'your string' END As B
    FROM (
            SELECT Name2, Name1, [Value]
            FROM Source
    ) s
    PIVOT 
    (
        MAX([Value]) FOR Name1 IN (A, B) -- any other Name1 would go here
    ) p
    

    using your sample data above, my results were

    P1  1           3
    P2  1           1
    P3  2           your string
    P4  your string 1
    

    EDIT:

    Since you have an unknown number of columns, you will need to look at using dynamic SQL and there are several answers here on SO about that with PIVOT.

    SQL Server 2005 Pivot on Unknown Number of Columns

    Pivot Table and Concatenate Columns

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

Sidebar

Related Questions

How to set default VALUE in SubQuery result using mysql SELECT p.`id`, p.`name`, p.`class_name`,
i am querying the DB as follows: $result=mysql_query(SELECT name,items FROM mytable WHERE price='$price'); now,i
I have to update value based on values from another table: update OracleOb..NS.myTable set
Please, help me with join results of commands (MS SQL): SELECT name,value FROM table1
INSERT INTO options (owner, name, value, modified) SELECT owner, name, value, modified, @draft:=draft FROM
Searching for values that uses the same value Example SELECT Name, UnitPrice, Quantity, Color
Following dropdown: <select id='dropdown' name='dropdown' onchange='showChart(this.value)'> <option value=1>Foo</value> <option value=2>Bar</value> </select> Calls this javascript
I'm having problems with this query: SELECT col1, col2,col3,...,coln FROM MyTable WHERE col1 =
We have this statement: (SELECT res_bev.bev_id, property_value.name AS priority FROM res_bev, bev_property, property_value WHERE
<select name="select"> </select> I want to populate the above tag with values from database.

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.