本文介绍了SQL Select-转换列中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很基本,但是我现在被困住了.

Quite basic, but I am stuck at the moment.

在Informix数据库(无数据透视选项)上,我正在寻找一种动态方法来使用SQL转换下表:

On an Informix database (no pivot option), I am searching for a dynamic way to transform the following table using SQL:


book       | info  | value
-----------------------------
Moby Dick  | price | high
Moby Dick  | stock | few
Hamlet     | price | low
Hamlet     | stock | many
Faust      | price | medium
Faust      | stock | normal

结果表:


book       | price  | stock
-----------------------------
Moby Dick  | high   | few
Hamlet     | low    | many
Faust      | medium | normal

感谢您的帮助!

推荐答案

您可以基于按书分组的CASE表达式进行汇总.尝试这样的事情.

You can aggregate based on CASE expression grouped by book. Try something like this.

SELECT book,
MAX(CASE WHEN info = 'price' THEN value END) as price,
MAX(CASE WHEN info = 'stock' THEN value END) as stock
FROM table1
GROUP BY book

这篇关于SQL Select-转换列中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:08