如何在mysql查询中重用结果集

时间:2014-05-19 12:36:06

标签: mysql sql resultset

执行" select * from temp"

后,我有这样的结果集
id | Month | value1 | value2 |
------------------------------
1  | Apr   | 100    | 150
2  | May   | 50     | 75
3  | Jan   | 50     | 75
5  | Feb   | 50     | 75
6  | mar   | 50     | 75

我想将其更改为列到像数据透视表这样的行。

我使用case语句来改变它。

select
    'value1' as Field,

    SUM(CASE 
        WHEN dc.month = 'January' THEN dc.value1 
        ELSE  ''
    END)    AS January,

    SUM(CASE 
        WHEN dc.month = 'February' THEN dc.value1 
        ELSE  ''
    END)    AS February,

    SUM(CASE 
        WHEN dc.month = 'March' THEN dc.value1 
        ELSE  ''
    END)    AS March
      SUM(CASE 
        WHEN dc.month = 'April' THEN dc.value1 
        ELSE  ''
    END)    AS April,

    SUM(CASE 
        WHEN dc.month = 'May' THEN dc.value1 
        ELSE  ''
    END)    AS May
      from
      (select 
         * from 
      temp)dc

Field   | Jan | Feb | Mar | Apr | May
-------------------------------------
Value1  | 50  | 50  | 50  | 100  | 50

我希望在不使用UNION的情况下在同一查询中为value2获取相同的结果集。因为临时表有很多数据。

Field   | Jan | Feb | Mar | Apr | May
-------------------------------------
Value1  | 50  | 50  | 50  | 100  | 50
Value1  | 75  | 75  | 75  | 150  | 75

2 个答案:

答案 0 :(得分:1)

此解决方案使用union,但只创建“假”行来表示每个值*列,然后按这些行连接和分组:

SELECT
    `field`,

    SUM(CASE 
        WHEN dc.month = 'Jan' THEN
            CASE `field`
            WHEN 'value1' THEN dc.value1
            WHEN 'value2' THEN dc.value2
        END
        ELSE  ''
    END)    AS January,

    SUM(CASE 
        WHEN dc.month = 'Feb' THEN
            CASE `field`
            WHEN 'value1' THEN dc.value1
            WHEN 'value2' THEN dc.value2
        END
        ELSE  ''
    END)    AS February,

    SUM(CASE 
        WHEN dc.month = 'Mar' THEN
            CASE `field`
            WHEN 'value1' THEN dc.value1
            WHEN 'value2' THEN dc.value2
        END
        ELSE  ''
    END)    AS March,
      SUM(CASE 
        WHEN dc.month = 'Apr' THEN
            CASE `field`
            WHEN 'value1' THEN dc.value1
            WHEN 'value2' THEN dc.value2
        END
        ELSE  ''
    END)    AS April,

    SUM(CASE 
        WHEN dc.month = 'May' THEN
            CASE `field`
            WHEN 'value1' THEN dc.value1
            WHEN 'value2' THEN dc.value2
        END
        ELSE  ''
    END)    AS May
      FROM
      (SELECT 
         * FROM temp
      )dc
      INNER JOIN (
       SELECT 'value1' AS `field`
            UNION SELECT 'value2'
      ) AS value_columns
    GROUP BY `field`

答案 1 :(得分:0)

如果它只有2列,即value1value2,那么您可以将UNION用作 DEMO

select
    'value1' as Field,
    SUM(
        CASE 
        WHEN dc.month = 'Jan' THEN dc.value1 
        ELSE  ''
    END)    AS January,

    SUM(
        CASE 
        WHEN dc.month = 'Feb' THEN dc.value1 
        ELSE  ''
    END)    AS February,

    SUM(
        CASE 
        WHEN dc.month = 'Mar' THEN dc.value1 
        ELSE  ''
    END)    AS March,
      SUM(
        CASE 
        WHEN dc.month = 'Apr' THEN dc.value1 
        ELSE  ''
    END)    AS April,

    SUM(
        CASE 
        WHEN dc.month = 'May' THEN dc.value1 
        ELSE  ''
    END)AS May
from
temp dc

UNION

select
    'value2' as Field,
    SUM(
        CASE 
        WHEN dc.month = 'Jan' THEN dc.value2 
        ELSE  ''
    END)    AS January,

    SUM(
        CASE 
        WHEN dc.month = 'Feb' THEN dc.value2 
        ELSE  ''
    END)    AS February,

    SUM(
        CASE 
        WHEN dc.month = 'Mar' THEN dc.value2 
        ELSE  ''
    END)    AS March,
      SUM(
        CASE 
        WHEN dc.month = 'Apr' THEN dc.value2 
        ELSE  ''
    END)    AS April,

    SUM(
        CASE 
        WHEN dc.month = 'May' THEN dc.value2 
        ELSE  ''
    END)AS May
from temp dc