惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
腾讯CDC
博客园_首页
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗

博客园 - shenpeng

C#中的具名参数和可选参数 C#中使用ExcelDataReader读取Excel文件 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法(转) 在eclipse中使用中文JAVA api文档 .NET、ASP.NET控件及源码大汇总 60多个精品源码站 ASP.net常用代码(常用技巧备忘) C#程序转为VB.NET程序的一个小问题 Any和Some和ALL 的使用,以及交操作差操作的嵌套查询(Oracle) (转) CEIL和FLOOR函数查询(Oracle,MSSQL) (转) 字符串分割自定义函数(SQL) (转) ORACLE中巧用一条SQL 实现其它进制到十进制转换(转) 数据库优化设计方案(转) 浅谈反射与特性在接口系统中的应用(编码表转化) (转) 数字格式化(转) 将DataSet导出成XLS、XML、HTML、CSV、TSV等格式 Web页面的数据导出excel时的格式问题(转) Access中使用SQL语句应掌握的几点技巧(转) 在SQL Server中修改sa的密码
纵表变横表经典中的经典
shenpeng · 2008-03-17 · via 博客园 - shenpeng

drop table t1
CREATE TABLE [t1] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [xm] [varchar] (50) default '',
    [km] [varchar] (50) default '',
    [fs] [float] NULL default 0
) ON [PRIMARY]

insert into t1(xm,km,fs)
select '张三','数学',10 union all
select '张三','语文',20 union all
select '张三','英语',30 union all
select '李四','数学',40 union all
select '李四','语文',50 union all
select '李四','英语',60 union all
select '王五','英语',70

SELECT
    xm,
    MAX(数学) AS 数学,
    MAX(语文) AS 语文,
    MAX(英语) AS 英语
FROM
    (
    SELECT
        xm,
        CASE km WHEN '数学' THEN fs END AS 数学,
        CASE km WHEN '语文' THEN fs END AS 语文,
        CASE km WHEN '英语' THEN fs END AS 英语
    FROM t1
    ) AS a
GROUP BY xm order by xm

select * from t1

select xm,
"数学"=(select fs from t1 where km='数学' and xm=t.xm),
"语文"=(select fs from t1 where km='语文' and xm=t.xm),
"英语"=(select fs from t1 where km='英语' and xm=t.xm)
from t1 t group by xm

select 代码,进货单位,
"西药"=(select 让利金额 from abc where 药类 ='西药' and 代码=t1.代码),
"中成药"=(select 让利金额 from abc where 药类 ='中成药' and 代码=t1.代码),
"医材 "=(select 让利金额 from abc where 药类 ='医材' and 代码=t1.代码),
"中草药"=(select 让利金额 from abc where 药类 ='中草药' and 代码=t1.代码),
"合计"=(select sum(药类) from abc where  代码=t1.代码 group by 代码)
 from  abc  t1 group by 代码,进货单位