Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将多行合并为一列中具有多个行值的行_Sql_Postgresql - Fatal编程技术网

Sql 将多行合并为一列中具有多个行值的行

Sql 将多行合并为一列中具有多个行值的行,sql,postgresql,Sql,Postgresql,我目前正在构建一个查询以从我的数据库中检索一些数据,我需要在一行中使用公共id访问一些信息 使用此查询: select missions_answer.response_id as "response", crm_player."document" as "document", missions_question.label as "label", m

我目前正在构建一个查询以从我的数据库中检索一些数据,我需要在一行中使用公共id访问一些信息

使用此查询:

select 
        missions_answer.response_id as "response",
        crm_player."document" as "document",
        missions_question.label as "label",
        missions_answertext.body as "bill #",
        missions_answerselectmultiple.body as "product",
        missions_answerinteger.body as "answer" 
from missions_answer 
    left join missions_question on missions_answer.question_id = missions_question.id 
    left join missions_answertext on missions_answer.id = missions_answertext.answer_ptr_id 
    left join missions_answerselectmultiple on missions_answer.id = missions_answerselectmultiple.answer_ptr_id
    left join missions_answerinteger on missions_answer.id = missions_answerinteger.answer_ptr_id 
    left join missions_response on missions_answer.response_id = missions_response.id
    left join crm_player on missions_response.player_id = crm_player.id
    LEFT JOIN crm_user ON crm_player.user_id = crm_user.id
    where  missions_answer.response_id = '71788176'
    group by missions_answer.response_id, crm_player.document,missions_answertext.body,
        missions_question.label,
        missions_answerselectmultiple.body ,
        missions_answerinteger.body,
        crm_user.first_name,
        crm_user.last_name
这就是我目前拥有的:

+   response    +     document    +    label    +    bill #  +    product  +  answer
-   71788176    -     79907201    -    bill #   -    26899   -             -
-   71788176    -     79907201    -    amount   -            -             -    1
-   71788176    -     79907201    -    product  -      -    {"name": "Shoes"}   -
-   71788176    -     79907201    -    price    -            -             -  25.99
这就是我要找的:

+   response    +     document    +    bill #  +    product  +  amount  +   price 
-   71788176    -     79907201    -    26899   -     shoes   -       1  -   25.99 

我一直在尝试使用
交叉表
,但我仍然找不到它,提前感谢您的任何提示或帮助。

从您当前的状态,您只需使用
过滤器
子句进行透视即可:


我不太清楚,你原来的桌子是什么样子的。如果是这样的话:

response | document | label   | value
-------: | -------: | :------ | :----
71788176 | 79907201 | bill    | 26899
71788176 | 79907201 | amount  | 1    
71788176 | 79907201 | product | shoes
71788176 | 79907201 | price   | 25.99
然后,您可以按如下方式修改查询:

response | document | label   | value
-------: | -------: | :------ | :----
71788176 | 79907201 | bill    | 26899
71788176 | 79907201 | amount  | 1    
71788176 | 79907201 | product | shoes
71788176 | 79907201 | price   | 25.99


编辑:要将JSON值添加到产品列:

变体1:您可以简单地将类型
json
转换为类型
text

MAX(product::text) FILTER (WHERE label = 'product') as product,
变量2:从
“name”
属性读取值:

MAX(product ->> 'name') FILTER (WHERE label = 'product') as product,

一个响应总是只包含一个产品吗?非常感谢您的回答,我在将MAX应用于包含该产品的JSON单元格时遇到了一个错误,知道吗?必须对JSON进行稍微不同的处理。请编辑您的问题并附加一些实际输入数据。JSON数据在哪里,它看起来怎么样,您期望的输出是什么
MAX(product ->> 'name') FILTER (WHERE label = 'product') as product,