D
D
des1roer2015-05-14 12:43:24
PostgreSQL
des1roer, 2015-05-14 12:43:24

postgres crosstab?

I want to create a show.
approximately such a base,
9rSosfW.png
that is, analiz_create has several records in analiz_data
and you need to "flip" the selection to get
ATFEApm.png
it, you need to select all records from the analiz_data table grouped by analiz_create id
UPD
something tells me that I need to use crosstab but here

select *  from crosstab('SELECT 
                            t1.value,
                            t2.name
                          FROM 
                            analiz_data t1,
                            element t2
                          WHERE
                            t1.elem_id = t2.id   ')
            as (value text, name text);

writes
ERROR: invalid source data SQL statement DETAIL: The provided SQL must return 3 columns: rowid, category, and values.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
des1roer, 2015-05-14
@des1roer

select * from 
    crosstab('SELECT 
            t1.analiz_id,
            t1.elem_id,
              t1.value
            FROM 
              a_analiz_data t1
            WHERE
              t1.analiz_id =36
                            ', //analiz_id - общий ид, 
            //elem_id - имя элемента
            //value - отображаемое значение
    '       SELECT 
  elem_id //elem_id - видимо нужен для связи двух таблиц
    FROM 
    a_analiz_data t1
  WHERE                            
     t1.analiz_id = 36')
as (rowid int, att1 text, att2 text, att3 text, att4 text, att5 text, att6 text, att7 text);

It was
SELECT 
  t1.value,
  t1.elem_id
FROM 
  a_analiz_data t1
WHERE
  t1.analiz_id =36

became
1iQax.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question