简体   繁体   English

如何在SQL中转置查询结果?

[英]How can I transpose a query result in SQL?

As the result of many subqueries, I have the following table: 作为许多子查询的结果,我有下表:

+---------------------------------------------+
| register | simulation | hasLimit | approved |
+---------------------------------------------+
|     X    |     Y      |    Z     |    W     |
+---------------------------------------------+

But I want it to be like this: 但我希望它是这样的:

+----------------+
| register   | X | 
+----------------+
| simulation | Y | 
+----------------+
| hasLimit   | Z | 
+----------------+
| approved   | W | 
+----------------+

In RI would simply transpose using the function t(), however in SQL it seems a bit harder. 在RI中,可以使用函数t()进行简单的转置,但是在SQL中,似乎有点困难。 I've tried to understand the function pivot to apply in this case, but it seems a little strange, since I'm actually just transposing, not pivoting anything. 我试图理解在这种情况下要应用的功能枢轴,但是似乎有点奇怪,因为我实际上只是在调换,而不枢轴任何东西。

You seem to want an "unpivot" operation. 您似乎想要“取消透视”操作。

Assuming the types of the columns are all compatible, you can use union all : 假设列的类型都是兼容的,则可以使用union all

select 'register' as which, register as value union all
select 'simulation',  simulation union all
select 'hasLimit', hasLimit union all
select 'approved', approved;

Some databases require a FROM clause for each subquery, such as from dual . 一些数据库要求每个子查询都具有FROM子句,例如from dual

This is a very small amount of data. 这是非常少量的数据。 For larger data, I prefer a lateral join, but the syntax (and availability of which) depends on the database. 对于较大的数据,我更喜欢横向联接,但是语法(及其可用性)取决于数据库。

Given the return from 鉴于回报

         SELECT 'x' AS register
              , 'y' AS simulation
              , 'z' AS hasLimit
              , 'w' AS approved

We can wrap that in a set of parens and reference it as an inline view in an outer query. 我们可以将其包装在一组括号中,并在外部查询中作为内联视图引用它。

For example: 例如:

SELECT CASE i.n
         WHEN 1 THEN 'register'
         WHEN 2 THEN 'simulation'
         WHEN 3 THEN 'hasLimit'
         WHEN 4 THEN 'approved'
       END AS col1
     , CASE i.n
         WHEN 1 THEN q.register
         WHEN 2 THEN q.simulation
         WHEN 3 THEN q.hasLimit
         WHEN 4 THEN q.approved
       END AS col2
  FROM (
         SELECT 'x' AS register
              , 'y' AS simulation
              , 'z' AS hasLimit
              , 'w' AS approved
       ) q
 CROSS
  JOIN ( SELECT 1 AS n
         UNION ALL SELECT 2
         UNION ALL SELECT 3
         UNION ALL SELECT 4
       ) i
 ORDER
    BY i.n

That seems a bit of a rigmarole, given that you already have subqueries that are returning scalar values, we could combine those with UNION ALL set operators 鉴于您已经有返回标量值的子查询,这似乎有些繁琐,我们可以将它们与UNION ALL集运算符结合使用

SELECT 'register'  AS col1, ( scalar_subquery_for_x ) AS col2
UNION ALL 
SELECT 'simulation'       , ( scalar_subquery_for_y )
UNION ALL 
SELECT 'hasLimit'         , ( scalar_subquery_for_z )
UNION ALL 
SELECT 'approved'         , ( scalar_subquery_for_w )

IF we need a guaranteed order, we can wrap that whole thing in parens and add an ORDER BY clause. 如果我们需要保证的顺序,则可以将整个内容包装在parens中并添加ORDER BY子句。 (Without the ORDER BY, we do observe rows are returned "in order" from the UNION ALL , but this behavior is not guaranteed.) (在没有ORDER BY的情况下,我们确实观察到行从UNION ALL “按顺序”返回,但是不能保证此行为。)

Use a simple UNPIVOT as below, 使用以下简单的UNPIVOT

SELECT Col, Val
FROM <Your Table>
UNPIVOT (Val FOR Col IN([register],[simulation],[hasLimit],[approved])) unpiv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM