0

I'm a new to SQL so, can you please help me in figuring out how to get a key-value pair from a SQL for example : Table A

A(PK)    B(INT)  C       D
------ ------- ------- --------

1        X       Y        Z

2        A       B        C

I want the output in the form

A = 1
B = X 
C = Y 
D = Z

i tried

SELECT A,B,C,D from tableA ; 

But it only prints in the form 1 X Y Z So, can you please help me ?

11
  • Do you only want a single record's worth of keys? Is there a column which defines an order? Commented Sep 17, 2018 at 5:32
  • yes, it will be a single result query Commented Sep 17, 2018 at 5:34
  • Generally getting the column names along with the row values is a thing the SQL client provides for you. What SQL client are you using? Commented Sep 17, 2018 at 5:34
  • i'm using pgadmin 4 Commented Sep 17, 2018 at 5:35
  • 1
    And what about multiple rows? What about a JSON result? rextester.com/XNB38806 Commented Sep 17, 2018 at 6:02

3 Answers 3

1

Generally getting the column names along with the row values and formatting the result is a thing the SQL client provides for you. pgAdmin isn't really suited for this, a programming language would be better.

For example, here's how you'd do it using Ruby and the ruby-pg gem.

#!/usr/bin/env ruby

require 'pg'

conn = PG.connect( dbname: 'test' )
conn.exec( "SELECT * FROM tableA" ) do |result|
  result.each do |row|
    row.each do |column,value|
      puts "#{column} = #{value}"
    end
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

would it be possible in java?
@Ani Yes, just about any programming language can connect to Postgres. I don't know Java, but this may help. tutorialspoint.com/postgresql/postgresql_java.htm
I used JSON and java to complete this answer, though i didn't use ruby as the language, the idea was pretty much the same Tnx
1

You can do a union of the queries picking one column at a time

SELECT 'A', A from tableA WHERE ID = @ID
UNION
SELECT 'B', B from tableA WHERE ID = @ID
UNION
SELECT 'C', C from tableA WHERE ID = @ID
UNION
SELECT 'D', D from tableA WHERE ID = @ID

you will probably need to cast all the columns to a common type (varchar) for this to work.

2 Comments

I added that just to ensure one record, Ani has not specified what clause is used.
I combined 2 answers here, i tried you answer but didn't work for the time being, but the above 2 answers ( one as reply ) i tried as JSON object and then parsed it using JAVA
0

Another option is to create a function that returns the result as desired:

create type crosstab_type as (row_num bigint, column_name text, value text);

create or replace function kv()
  returns setof crosstab_type
as
$$
declare 
  l_rec record;
  l_row bigint := 1;
begin
  for l_rec in select * from ani 
  loop
    return next (l_row, 'a', l_rec.a::text)::crosstab_type;
    return next (l_row, 'b', l_rec.b::text)::crosstab_type;
    return next (l_row, 'c', l_rec.c::text)::crosstab_type;
    return next (l_row, 'd', l_rec.d::text)::crosstab_type;
    l_row := l_row + 1;
  end loop;
end;
$$
language plpgsql;



select *
from kv();

returns:

row_num | column_name | value
--------+-------------+------
      1 | a           | 1    
      1 | b           | X    
      1 | c           | Y    
      1 | d           | Z    
      2 | a           | 2    
      2 | b           | A    
      2 | c           | B    
      2 | d           | C   

Online example: http://rextester.com/RCKB51862

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.