简体   繁体   English

SQL查询-数据透视分组或任何获取我的结果表的概念

[英]SQL Query - Pivot Grouping or any concept to get my result table

I have a table called #MyTable. 我有一个名为#MyTable的表。

MyTable MyTable的

    ID  Column Value
    1   x1_abc  11
    1   x1_def  23
    1   x2_abc  9
    1   x2_def  10
    2   x1_abc  1
    2   x1_def  55
    2   x2_abc  42
    2   x2_def  44
    3   x1_abc  32
    3   x1_def  33
    3   x2_def  88
    3   x2_def  90

My output should be like this 我的输出应该是这样的

    ID  Column  x1  x2
    1   abc     11  9
    1   def     23  10
    2   abc     1   42
    2   def     55  44
    3   abc     32  88
    3   def     33  90

Not sure this is this possible in SQL query. 不确定在SQL查询中是否可行。 I tried using SQL Pivot but didn't workout. 我尝试使用SQL Pivot,但没有锻炼。 Please give suggestion to write query to get this result. 请提出编写查询的建议以获得此结果。

It looks like you might be able to do a PIVOT to create the table you are looking for, see below. 您似乎可以执行PIVOT来创建您要查找的表,请参见下文。

SELECT ID, Column1, ISNULL([x1],0) AS 'x1', ISNULL([x2],0) AS 'x2'
FROM

(SELECT ID, RIGHT(Column1,3) AS Column1, LEFT(Column1,2) AS 'x', Value
FROM #start) AS d

PIVOT
(SUM(Value) FOR x IN ([x1],[x2])
) AS piv

I created a temp table to use to test it and it worked. 我创建了一个临时表来对其进行测试,并且该表有效。 See temp table below. 请参见下面的临时表。

 CREATE TABLE #start ( ID int, Column1 nvarchar(50), Value int);


INSERT INTO #start 
VALUES (1,   'x1_abc',  11),
(1,   'x1_def',  23),
(1,  'x2_abc',  9),
(1,   'x2_def',  10),
(2,   'x1_abc',  1),
(2,   'x1_def',  55),
(2,   'x2_abc',  42),
(2,   'x2_def',  44),
(3,   'x1_abc',  32),
(3,   'x1_def',  33),
(3,   'x2_def',  88),
(3,   'x2_def',  90)

Here are the results that I received using the PIOVT: 这是我使用PIOVT收到的结果:

ID  Column1 x1  x2
1   abc 11  9
2   abc 1   42
3   abc 32  0
1   def 23  10
2   def 55  44
3   def 33  178

I hope this is helpful. 我希望这是有帮助的。 Feel free to reach out with any questions. 如有任何问题,请随时与我们联系。

Just in case you need to go dynamic. 以防万一您需要保持动态。

Example

Declare @SQL varchar(max) = '
Select *
 From (
        Select ID
              ,[Column] = substring([column],charindex(''_'',[column])+1,50)
              ,Item     = left([column],charindex(''_'',[column])-1)
              ,Value  
         From  YourTable
      ) Src
 Pivot (max([Value]) For [Item] in (' + Stuff((Select Distinct ','+QuoteName(left([column],charindex('_',[column])-1)) 
                                                From YourTable
                                                Order By 1 
                                               For XML Path('')),1,1,'')  + ') ) p
 Order By 1,2'
Exec(@SQL);

Returns 返回

ID  Column  x1  x2
1   abc     11  9
1   def     23  10
2   abc     1   42
2   def     55  44
3   abc     32  88
3   def     33  90

The Generated SQL Looks like this 生成的SQL看起来像这样

Select *
 From (
        Select ID
              ,[Column] = substring([column],charindex('_',[column])+1,50)
              ,Item     = left([column],charindex('_',[column])-1)
              ,Value  
         From  YourTable
      ) Src
 Pivot (max([Value]) For [Item] in ([x1],[x2]) ) p
 Order By 1,2

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

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