ms sql查询将行转换为不同情况的列

时间:2017-09-26 12:29:39

标签: sql sql-server pivot

我有一张如下表,我尝试了很多解决方案,但我没有取得成功。请帮我解决这个问题。

   ________________________________________
   |Trend    |  MemberName  |   MemberValue|
   |---------------------------------------|
   |1/1/2017 |  TCS         |   6215812.027|
   |1/1/2017 |  InfoSys     |   2920385.747|
   |1/1/2017 |  Maruti      |   3223330.947|
   |2/1/2017 |  TCS         |   4644255.532|
   |2/1/2017 |  InfoSys     |   2378019.663|
   |2/1/2017 |  Maruti      |   2315258.732|
   |3/1/2017 |  TCS         |   4377771.365|
   |3/1/2017 |  InfoSys     |   2457987.188|
   |3/1/2017 |  Maruti      |   2060665.477|
   |4/1/2017 |  TCS         |   3749728.998|
   |4/1/2017 |  InfoSys     |   2161210.683|
   |4/1/2017 |  Maruti      |   1828319.789|
   |5/1/2017 |  TCS         |   3727746.97 |
   |5/1/2017 |  InfoSys     |   2478883.144|
   |5/1/2017 |  Maruti      |   1717573.406|
   ----------------------------------------

我想要这样

   ____________________________________________________________
   |Trend       |  TCS /Top1    | InfoSys/Top2   |  Maruti/Top2|
   -------------------------------------------------------------
   |1/1/2017    |  6215812.027  |   2920385.747  |  3223330.947|
   |2/1/2017    |  4644255.532  |   2378019.663  |  2315258.732|
   |3/1/2017    |  4377771.365  |   2457987.188  |  2060665.477|
   |4/1/2017    |  3749728.998  |   2161210.683  |  1828319.789|
   |5/1/2017    |  3727746.97   |   2478883.144  |  1717573.406|
   -------------------------------------------------------------

会员名称可以是Top1,Top2,Top3等。 您可以使用给定查询来创建表并生成样本数据。

  CREATE TABLE Members
  (
  Trend DATE, 
  MemberName VARCHAR(20), 
  MemberValue FLOAT
  )

  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('1/1/2017','TCS',6215812.027)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('1/1/2017','InfoSys',2920385.747)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('1/1/2017','Maruti',3223330.947)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('2/1/2017','TCS',4644255.532)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('2/1/2017','InfoSys',2378019.663)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('2/1/2017','Maruti',2315258.732)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('3/1/2017','TCS',4377771.365)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('3/1/2017','InfoSys',2457987.188)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('3/1/2017','Maruti',2060665.477)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('4/1/2017','TCS',3749728.998)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('4/1/2017','InfoSys',2161210.683)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('4/1/2017','Maruti',1828319.789)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('5/1/2017','TCS',3727746.97)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('5/1/2017','InfoSys',247888.144)
  INSERT INTO Members(Trend, MemberName, MemberValue) VALUES('5/1/2017','Maruti',1717573.406)

1 个答案:

答案 0 :(得分:2)

您需要使用枢轴。

有关支点的更多详情,请访问此链接:pivot tutorial

您可以将此查询用于结果。

 select * from 
 (
     select Trend, MemberName, MemberValue from Members 
 ) M
 pivot
 (
     sum(MemberValue)
     for MemberName in ([TCS], [InfoSys], [Maruti])
 ) piv;

结果;

Trend       TCS         InfoSys     Maruti
2017-01-01  6215812,027 2920385,747 3223330,947
2017-02-01  4644255,532 2378019,663 2315258,732
2017-03-01  4377771,365 2457987,188 2060665,477
2017-04-01  3749728,998 2161210,683 1828319,789
2017-05-01  3727746,97  247888,144  1717573,406

希望对你有所帮助。