求一个SQL语句。 - MS-SQL Server / 应用实例
假设SQL数据表如不:
id field A field B ...
1 AA S1
2 AA S2
3 AA S3
4 AA S2
5 B S2
6 C S3
7 D S1
8 D S1
9 D S2
10 D S2
其中FIELD B的取值范围是有限的,要求得到以下结果:
FIELD A S1 S2 S3(上表只有三种取值)
AA 1 2 1
B 1
C 1
D 2 2
请问SQL 语句该如何写?
SQL code:
--既然fieldB有限那就可以写死
select FieldA,
S1=sum(case when FieldB='S1' then 1 else 0 end),
S2=sum(case when FieldB='S2' then 1 else 0 end),
S3=sum(case when FieldB='S3' then 1 else 0 end)
from [Table]
group by FieldA
SQL code:
select FieldA,
S1=sum(case when FieldB='S1' then 1 else 0 end),
S2=sum(case when FieldB='S2' then 1 else 0 end),
S3=sum(case when FieldB='S3' then 1 else 0 end)
from [Table]
group by FieldA
传说中的行列转换
精华帖
很经典
SQL code
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.c
相关问答:
执行的顺序:
1)文件浏览框(选择文件使用)
选择好文件后
点击一个导入按钮的时候 ,把上面上传框里的csv文件以一个ID为文件名,上传到**/**文件夹下
2)读取这个文件夹下的csv的文件,转换成sql
3 ......
如何在SQL2005中设定定时作业,比如说定时清理某些表的数据,
或者是定时的将某些表的数据导出excel!
在线等待,急急急,最好是详细步骤!
之前我做的作业有点问题!
帮UP
参考:http://hi.baidu.com/toiota ......
查询学生平均成绩及其名次
SELECT 1+(SELECT COUNT( distinct 平均成绩)
from (SELECT S#,AVG(score) AS 平均成绩
from SC&n ......
有TABLEA 字段为 采购单号、行号、物料编码、入库日期
现想按照物料编码查询最大入库日期
语句如下:
SELECT 采购单号、行号、物料编码、入库日期 from TABLEA A WHERE 入库日期=(SELECT MAX(入库日期 ......