如何使用pivot sql [英] How to use pivot sql

查看:139
本文介绍了如何使用pivot sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张包含以下数据的表格..



 A B C D 
BR00002 281 2.22 12-01-2013
BR00002 277 1.65 12-02-2013
BR00002 267 1.8 12-03-2013





我想要得到以下结果..



 X y 12-01-2013 12-02-2013 12-03-2013 
BR00002 825 2.22 1.65 1.8





请帮助,如何获得所需.. .. div class =h2_lin>解决方案

学会自己动手:

在SQL查询中使用Pivot的简单方法 [ ^ ]

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++

根据要求,试试这个:

  SELECT  A < span class =code-keyword> as  X,Y,[12-01-2013],[12-02-2013],[12-03-20 13] 
FROM
SELECT A,
SELECT SUM(B)来自 table1 t2 其中 t2 .a = t1.a group by t2.a) as Y,
C,D FROM table1 t1) AS src
PIVOT

SUM(C) FOR D IN ([12 -01-2013],[12-02-2013],[12-03-2013])
as pvt


现在解决了。

我已经采用临时表将A组插入SUM(B)并加入两个语句..

它解决了我的问题..



感谢你们支持和教我PIVOT。 :)

I have a table with following data..

A	B	C	D
BR00002	281	2.22	12-01-2013
BR00002	277	1.65	12-02-2013
BR00002	267	1.8	12-03-2013



I Want to get the following result..

X	y	12-01-2013	12-02-2013	12-03-2013
BR00002	825	2.22	         1.65	          1.8



Please help, How to get the required..

解决方案

Learn to do it yourself:
Simple Way To Use Pivot In SQL Query[^]
++++++++++[round 2]++++++++++
As per request, try this:

SELECT  A as  X, Y, [12-01-2013],[12-02-2013],[12-03-2013]
FROM
(SELECT A,
    (SELECT SUM(B) from table1 t2 where t2.a=t1.a group by t2.a) as Y,
 C, D  FROM table1 t1) AS src
PIVOT
(
  SUM(C)  FOR D IN ([12-01-2013],[12-02-2013],[12-03-2013])
) as  pvt


It is solved now.
I have taken a temp table to insert SUM(B) with group by A and join the both statement..
It has solved my problem..

Thanks for you guys to support and teach me about PIVOT. :)


这篇关于如何使用pivot sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆