相关疑难解决方法(0)

SQL将行转置为列

我有一个有趣的难题,我认为可以用纯粹的SQL解决.我有类似于以下的表:

responses:

user_id | question_id | body
----------------------------
1       | 1           | Yes
2       | 1           | Yes
1       | 2           | Yes
2       | 2           | No
1       | 3           | No
2       | 3           | No


questions:

id | body
-------------------------
1 | Do you like apples?
2 | Do you like oranges?
3 | Do you like carrots?
Run Code Online (Sandbox Code Playgroud)

我想得到以下输出

user_id | Do you like apples? | Do you like oranges? | Do you like carrots?
---------------------------------------------------------------------------
1 …
Run Code Online (Sandbox Code Playgroud)

sql postgresql pivot pivot-without-aggregate

38
推荐指数
3
解决办法
5万
查看次数

SQLITE - 正确地将行转换为列

我有一个包含产品订单表的数据库,如下所示:

order_id | prod_code | prod_color | size | quantity |  
-----------------------------------------------------
1          SHIRT       001          S      10
1          SHIRT       001          M      7
1          SHIRT       001          L      8
1          SHIRT       001          XL     1
1          SHIRT       995          S      2
1          SHIRT       995          M      1
1          SHIRT       995          L      0
1          SHIRT       995          XL     1
2          PANTS       ....
Run Code Online (Sandbox Code Playgroud)

和这样的产品价格表:

prod_code | prod_color | price | currency
-----------------------------------------
SHIRT       001          10      EUR
SHIRT       001          9       USD
SHIRT       001          50      YEN
SHIRT       001          15      RUB …
Run Code Online (Sandbox Code Playgroud)

sql database sqlite

5
推荐指数
1
解决办法
1万
查看次数