pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: SQL (table transposition)
4+ messages / 4 participants
[nested] [flat]

* SQL (table transposition)
@ 2000-08-03 17:24 Dana.Reed@clinicaldatacare.com
  2000-08-03 15:46 ` Re: SQL (table transposition) Mark Volpe <volpe.mark@epamail.epa.gov>
  2000-08-04 07:30 ` Re: SQL (table transposition) Volker Paul <vpaul@dohle.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Dana.Reed@clinicaldatacare.com @ 2000-08-03 17:24 UTC (permalink / raw)
  To: pgsql-sql

Is there any way to get table T3 (below) from T1 and T2 using SQL (select, view, etc)?

T3 is basically all INDEX values from T1 matched to IND from T2 with the corresponding KEY/VALUE pairs transposed from rows to columns.

-------
|INDEX|   (T1)
-------
|  1  |
|  2  |
|  3  |
-------

-----------------
|IND|KEY| VALUE |   (T2)
-----------------
| 1 | 1 | val_a |
| 1 | 2 | val_b |
| 1 | 3 | val_c |
| 2 | 1 | val_d |
| 2 | 2 | val_e |
| 3 | 1 | val_f |
| 3 | 3 | val_g |
-----------------

----------------------------------
|T1_INDEX|KEY1VAL|KEY2VAL|KEY3VAL|   (T3)
----------------------------------
|   1    | val_a | val_b | val_c |
|   2    | val_d | val_e |       |
|   3    | val_f |       | val_g |
----------------------------------

Thanks for any suggestions

med vänlig hälsning
/Dana



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SQL (table transposition)
  2000-08-03 17:24 SQL (table transposition) Dana.Reed@clinicaldatacare.com
@ 2000-08-03 15:46 ` Mark Volpe <volpe.mark@epamail.epa.gov>
  1 sibling, 0 replies; 4+ messages in thread

From: Mark Volpe @ 2000-08-03 15:46 UTC (permalink / raw)
  To: Dana.Reed@clinicaldatacare.com; +Cc: pgsql-sql

Hope you like black magic :)

SELECT IND AS T1_INDEX, MIN(CASE WHEN KEY=1 THEN VALUE ELSE NULL END) AS
KEY1VAL, MIN(CASE WHEN KEY=2 THEN VALUE ELSE NULL END) AS KEY2VAL, MIN(CASE
WHEN KEY=3 THEN VALUE ELSE NULL END) AS KEY3VAL FROM T2 GROUP BY IND ORDER BY
IND;

Mark

Dana.Reed@clinicaldatacare.com wrote:
> 
> Is there any way to get table T3 (below) from T1 and T2 using SQL (select, view, etc)?
> 
> T3 is basically all INDEX values from T1 matched to IND from T2 with the corresponding KEY/VALUE pairs transposed from rows to columns.
> 
> -------
> |INDEX|   (T1)
> -------
> |  1  |
> |  2  |
> |  3  |
> -------
> 
> -----------------
> |IND|KEY| VALUE |   (T2)
> -----------------
> | 1 | 1 | val_a |
> | 1 | 2 | val_b |
> | 1 | 3 | val_c |
> | 2 | 1 | val_d |
> | 2 | 2 | val_e |
> | 3 | 1 | val_f |
> | 3 | 3 | val_g |
> -----------------
> 
> ----------------------------------
> |T1_INDEX|KEY1VAL|KEY2VAL|KEY3VAL|   (T3)
> ----------------------------------
> |   1    | val_a | val_b | val_c |
> |   2    | val_d | val_e |       |
> |   3    | val_f |       | val_g |
> ----------------------------------
> 
> Thanks for any suggestions
> 
> med vänlig hälsning
> /Dana



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SQL (table transposition)
  2000-08-03 17:24 SQL (table transposition) Dana.Reed@clinicaldatacare.com
@ 2000-08-04 07:30 ` Volker Paul <vpaul@dohle.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Volker Paul @ 2000-08-04 07:30 UTC (permalink / raw)
  To: Dana.Reed@clinicaldatacare.com; +Cc: pgsql-sql

Dana.Reed@clinicaldatacare.com wrote:
> 
> Is there any way to get table T3 (below) from T1 and T2 using SQL (select, view, etc)?
> 
> T3 is basically all INDEX values from T1 matched to IND from T2 with the corresponding KEY/VALUE pairs transposed from rows to columns.
> 
> -------
> |INDEX|   (T1)
> -------
> |  1  |
> |  2  |
> |  3  |
> -------
> 
> -----------------
> |IND|KEY| VALUE |   (T2)
> -----------------
> | 1 | 1 | val_a |
> | 1 | 2 | val_b |
> | 1 | 3 | val_c |
> | 2 | 1 | val_d |
> | 2 | 2 | val_e |
> | 3 | 1 | val_f |
> | 3 | 3 | val_g |
> -----------------
> 
> ----------------------------------
> |T1_INDEX|KEY1VAL|KEY2VAL|KEY3VAL|   (T3)
> ----------------------------------
> |   1    | val_a | val_b | val_c |
> |   2    | val_d | val_e |       |
> |   3    | val_f |       | val_g |
> ----------------------------------
> 


I think what you are looking for is cross tabulation, 
TRANSFORM statement, but I don't know if that
is supported by PostgreSQL.

Volker Paul



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* RE: SQL (table transposition)
@ 2000-08-04 12:57 Henry Lafleur <HLafleur@phoenixforge.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Henry Lafleur @ 2000-08-04 12:57 UTC (permalink / raw)
  To: Volker Paul <vpaul@dohle.com>; Dana.Reed@clinicaldatacare.com; +Cc: pgsql-sql

Hi,

When I saw cross tab, I realized that I'd done this before. 

If you know what your keys are ahead of time, you can write the query.
Otherwise, you can write a program go generate the query by looking at the
distinct list of keys and generating code as follows. The code generator
would only have to replace the ?'s in "SUM(CASE KEY WHEN ? THEN VALUE ELSE 0
END) AS KEY?VAL".

SELECT INDEX AS T1_INDEX, SUM(CASE KEY WHEN 1 THEN VALUE ELSE 0 END) AS
KEY1VAL, 
	SUM(CASE KEY WHEN 2 THEN VALUE ELSE 0 END) AS KEY2VAL, 
	SUM(CASE KEY WHEN 3 THEN VALUE ELSE 0 END) AS KEY3VAL
FROM T1, T2
WHERE T1.INDEX = T2.IND
GROUP BY T1.INDEX ;

Henry


-----Original Message-----
From: Volker Paul [mailto:vpaul@dohle.com]
Sent: Friday, August 04, 2000 2:30 AM
To: Dana.Reed@clinicaldatacare.com
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] SQL (table transposition)


Dana.Reed@clinicaldatacare.com wrote:
> 
> Is there any way to get table T3 (below) from T1 and T2 using SQL (select,
view, etc)?
> 
> T3 is basically all INDEX values from T1 matched to IND from T2 with the
corresponding KEY/VALUE pairs transposed from rows to columns.
> 
> -------
> |INDEX|   (T1)
> -------
> |  1  |
> |  2  |
> |  3  |
> -------
> 
> -----------------
> |IND|KEY| VALUE |   (T2)
> -----------------
> | 1 | 1 | val_a |
> | 1 | 2 | val_b |
> | 1 | 3 | val_c |
> | 2 | 1 | val_d |
> | 2 | 2 | val_e |
> | 3 | 1 | val_f |
> | 3 | 3 | val_g |
> -----------------
> 
> ----------------------------------
> |T1_INDEX|KEY1VAL|KEY2VAL|KEY3VAL|   (T3)
> ----------------------------------
> |   1    | val_a | val_b | val_c |
> |   2    | val_d | val_e |       |
> |   3    | val_f |       | val_g |
> ----------------------------------
> 


I think what you are looking for is cross tabulation, 
TRANSFORM statement, but I don't know if that
is supported by PostgreSQL.

Volker Paul



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2000-08-04 12:57 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-08-03 17:24 SQL (table transposition) Dana.Reed@clinicaldatacare.com
2000-08-03 15:46 ` Mark Volpe <volpe.mark@epamail.epa.gov>
2000-08-04 07:30 ` Volker Paul <vpaul@dohle.com>
2000-08-04 12:57 RE: SQL (table transposition) Henry Lafleur <HLafleur@phoenixforge.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox