简体   繁体   English

从一张表中获取数据

[英]pivot data from one table

input table 输入表

country tag short
UK  F1  Units
UK  F2  Volume
UK  F3  Value
FR  T3  Units
FR  T2  Volume
FR  T1  Value

result output i want : 我想要的结果输出:

country Units Volume Value
uk      f1      f2      f3
fr      t1      t2      t3

If there are a fixed number of different short values, simply use case expressions to do conditional aggregation : 如果有固定数量的不同short值,只需使用case表达式进行条件聚合

select country,
       max(case when short = 'Units' then tag end) as Units,
       max(case when short = 'Volume' then tag end) as Volume,
       max(case when short = 'Value' then tag end) as val
from tablename
group by country

For solution you have to use dynamic pivoting. 对于解决方案,您必须使用动态旋转。

create table #temp
(
     country varchar(30),tag varchar(20),short varchar(300)
) 
insert into #temp values ('UK',  'F1',  'Units')
insert into #temp values ('UK',  'F2' , 'Volume')
insert into #temp values ('UK'  ,'F3', 'Value')
insert into #temp values ('FR',  'T3' , 'Units')
insert into #temp values ('FR' , 'T2',  'Volume')
insert into #temp values ('FR',  'T1' , 'Value')

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.short) 
            FROM #temp c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT country, ' + @cols + ' from 
            (
                select country
                    , tag
                    , short
                from #temp
           ) x
            pivot 
            (
                 max(tag)
                for short in (' + @cols + ')
            ) p '


execute(@query)

drop table #temp

Table Structure 表结构

 CREATE TABLE tablename
  (
     [country] [NVARCHAR](10) NULL,
     [tag]     [NVARCHAR](10) NULL,
     [short]   [NVARCHAR](10) NULL
  )

INSERT INTO tablename
VALUES      
('UK','F1','Units'),
('UK','F2','Volume'),
('UK','F3','Value'),
('FR','T3','Units'),
('FR','T2','Volume'),
('FR','T1','Value');

Using Pivot function 使用枢轴功能

SELECT *
FROM   tablename
       PIVOT ( Max(tag)
             FOR short IN ([Units], [volume], [Value]) ) piv;  

Online Demo: Link 在线演示: 链接

Using Dynamic SQL PIVOT 使用动态SQL PIVOT

DECLARE @cols  AS NVARCHAR(max),
        @query AS NVARCHAR(max)

SELECT @cols = Stuff((SELECT distinct ',' + Quotename(short)
                      FROM   tablename
                      FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1,'');

SET @query = 'SELECT * 
              FROM   tablename        
                     PIVOT ( Max(tag)              
                           FOR short IN (' + @cols + ') ) piv;';

EXECUTE(@query);  

Online Demo: Link 在线演示: 链接

Result 结果

+---------+-------+--------+-------+
| country | Units | volume | Value |
+---------+-------+--------+-------+
| FR      | T3    | T2     | T1    |
| UK      | F1    | F2     | F3    |
+---------+-------+--------+-------+

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

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