【问题标题】:How to get table format output with sql query for a given table [closed]如何使用给定表的sql查询获取表格式输出[关闭]
【发布时间】:2014-05-22 23:38:31
【问题描述】:

你能帮我查询一下得到下面的输出表吗?

谢谢,

输入表

S   T   C
a   a   1
a   b   2   
a   c   3
a   d   4
b   a   5
b   b   6
b   c   7
b   d   8
c   a   9
c   b   10
c   c   11
c   d   12
d   a   13
d   b   14
d   c   15
d   d   16

输出表

S   a   b   c   d
a   1   2   3   4
b   5   6   7   8
c   9   10  11  12
d   13  14  15  16

【问题讨论】:

  • 我尝试在 select 中使用 'Pivot' 和 Case 语句,但还没有得到预期的输出。谢谢。
  • 如果你知道pivot在sql server中是如何工作的,那么这个问题的答案就很简单了。我想知道你尝试了什么。

标签: sql sql-server sql-server-2008 stored-procedures sql-server-2012


【解决方案1】:

这就是答案——

select * 
from
(
select S,T,C as Cd
from  Data
) as src
pivot(
Sum(Cd)--Max gives the same output too.
For T in ([a],[b],[c],[d])
) as pvt

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2017-06-07
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多