4

这是数据:

id    name     period      data1    data2
===================================================
111   name1    monthly     aaaaa    bbbbb
111   name1    quaterly    ccccc    ddddd
111   name1    halfYearly  eeeee    fffff
111   name1    annually    ggggg    hhhhh

我想要在单行中获取数据的查询,例如

id    name     monthlYdata1    monthlYdata2      quaterlydata1    quaterlydata2      halfYearlydata1    halfYearlydata2      annuallydata1    annuallydata2
==========================================================================================================================================================
111   name1    aaaaa           bbbbb             ccccc            ddddd              eeeee              fffff                ggggg            hhhhh
4

4 回答 4

7

您没有指定您正在使用什么 RDBMS,但是,这将适用于所有这些:

select id,
  name,
  max(case when period = 'monthly' then data1 end) as MonthlyData1,
  max(case when period = 'monthly' then data2 end) as MonthlyData2,
  max(case when period = 'quaterly' then data1 end) as quarterlyData1,
  max(case when period = 'quaterly' then data2 end) as quarterlyData2,
  max(case when period = 'halfYearly' then data1 end) as halfYearlyData1,
  max(case when period = 'halfYearly' then data2 end) as halfYearlyData2,
  max(case when period = 'annually' then data1 end) as annuallyData1,
  max(case when period = 'annually' then data2 end) as annuallyData2
from yourtable
group by id, name

请参阅带有演示的 SQL Fiddle

如果您使用的是具有PIVOT函数的 RDBMS,那么您可以执行以下操作,同时使用UNPIVOTPIVOT来生成结果。正如 Andriy M 指出的那样,UNPIVOT假设两者的数据类型data1data2是相同的类型,如果不是,则需要对UNPIVOT数据进行转换:

甲骨文 11g:

select *
from
(
  select id, name, value, 
    period||data new_col
  from yourtable
  unpivot
  (
    value for data in (data1, data2)
  ) u
) x
pivot
(
  max(value)
  for new_col in ('monthlyDATA1', 'monthlyDATA2',
                  'quaterlyDATA1', 'quaterlyDATA2',
                  'halfYearlyDATA1', 'halfYearlyDATA2',
                  'annuallyDATA1', 'annuallyDATA2')
) p

请参阅带有演示的 SQL Fiddle

SQL 服务器:

select *
from
(
  select id, name, value, 
    period+data new_col
  from yourtable
  unpivot
  (
    value for data in (data1, data2)
  ) u
) x
pivot
(
  max(value)
  for new_col in ('monthlyDATA1', 'monthlyDATA2',
                  'quaterlyDATA1', 'quaterlyDATA2',
                  'halfYearlyDATA1', 'halfYearlyDATA2',
                  'annuallyDATA1', 'annuallyDATA2')
) p
于 2012-10-29T13:03:16.350 回答
0

从查询本身获取数据。

从 query1 中获取 id 和 name,并使用从 query1 中获取的 id 将该查询与月度、季度和年度数据连接起来

于 2012-10-29T12:39:50.340 回答
0

三篇使用完整文章回答您的问题:

如何在 MySQL 中将行显示为列?

如何旋转 MySQL 实体-属性-值模式

http://forums.mysql.com/read.php?20,75357,75357#msg-75357

于 2012-10-29T12:57:16.500 回答
0

首先,您应该将所有不同的 id 检索到临时表中。

Select DISTINCT id
into #tmpIds
from Table

然后使用您想要的列(id, name, monthlYdata1, monthlYdata2, quaterlydata1, quaterlydata2, halfYearlydata1, halfYearlydata2, annuallydata1, annuallydata2)创建一个临时表,并遍历您的第一个临时表以获取 id。

对于每个 id,您应该执行以下操作:

Insert into #tmpTable
   @id,
   '',
  (Select data1 from Table where id=@id and period='monthly') as monthlyData1
  ...

这是我想到的第一个解决方案。

于 2012-10-29T12:58:02.213 回答