Click here to Skip to main content
14,641,302 members

How do I project 3 different columns from 3 rows without creating temp table.

istudent asked:

Open original thread
How do i project 3 different columns from 3 rows without creating temp table.

In my table, I have 3 columns name type, identifier and value.

column 'type' has records 'myapp' in all 3 rows.

column 'identifier' has record 'app-owner' in first row.
column 'identifier' has record 'app-dev' in second row.
column 'identifier' has record 'app-tester' in third row.

column 'value' has record 'The Owner' in row 1.
column 'value' has record 'The Dev' in row 2.
column 'value' has record 'The Tester' in row 3.

How do i write query so that i can get data back with columns 'Owner', 'Dev' and 'Tester' with values 'The Owner', 'The Dev' and 'The Tester' ?

What I have tried:

I tried by creating temp table but that is not how I want to do it.

Self Join can produce the result but is it efficient way to do it when table contains many records?

Select TOP(1) as 'Owner', as 'Dev', as 'Tester FROM myTable m
JOIN myTable a on a.type = m.type and a.identifier = 'app-owner'
JOIN myTable b on b.type = m.type and b.identifier = 'app-dev'
JOIN myTable c on c.type = m.type and c.identifier = 'app-tester'
Tags: SQL


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100