这个是 SQL 的 Pivot (一列变多列) 函数的逆函数 UnPivot (多列合并到1列)
如果用一句 SQL 语句做:
*!*
UnPivot 函数顾名思义就是把几列合并到1列中去
Close Databases
Create Table Test1 (Id Int, 姓名 C(20), Q1 Int, Q2 Int, Q3 Int, Q4 Int)
Insert Into Test1 Values(1, '老张', 1000, 2000, 4000, 5000)
Insert Into Test1 Values(2, '小李', 3000, 3500, 4200, 5500)
*!*
假如要把 4 个季度的字段合并到一个年度的字段中
Select Id, 姓名, (Select Q1 As 年度 From Test1 Where Id=a.Id) From Test1 As A ;
Union ;
Select Id, 姓名, (Select Q2 From Test1 Where Id=a.Id) From Test1 As A ;
Union ;
Select Id, 姓名, (Select Q3 From Test1 Where Id=a.Id) From Test1 As A ;
Union ;
Select Id, 姓名, (Select Q4 From Test1 Where Id=a.Id) ;
from Test1 As A