SqlServer和Oracle中一些常用的sql语句3 行列转换
--217, SQL SERVER
SELECT Cust_Name
, MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009-08-01"
, MAX(CASE WHEN Order_Date ='2009-08-02' THEN AR END) "2009-08-02"
, MAX(CASE WHEN Order_Date ='2009-08-03' THEN AR END) "2009-08-03"
FROM
(
SELECT CONVERT(CHAR(10), Order_Date, 120) Order_Date
, CUST_NAME
, SUM(Qty * Price) AR
FROM Orders
WHERE Order_Date BETWEEN '2009-08-01'
AND CAST('2009-08-03' AS datetime) +1
GROUP BY CONVERT(CHAR(10), Order_Date, 120)
, CUST_NAME
UNION ALL
SELECT CONVERT(CHAR(10), Order_Date, 120) Order_Date
,NULL CUST_NAME
, SUM(Qty * Price) AR
FROM Orders
WHERE Order_Date BETWEEN '2009-08-01'
AND CAST('2009-08-03' AS datetime) +1
GROUP BY CONVERT(CHAR(10), Order_Date, 120)
) A
GROUP BY Cust_Name --218, SQL SERVER
SELECT Cust_Name
, SUM(
CASE WHEN CONVERT(CHAR(10),Order_Date, 120)='2009-08-01' THEN Qty* Price END
) "2009-08-01"
, SUM(
CASE WHEN CONVERT(CHAR(10),Order_Date, 120) ='2009-08-02' THEN Qty* Price END
) "2009-08-02"
, SUM(
CASE WHEN CONVERT(CHAR(10),Order_Date, 120) ='2009-08-03' THEN Qty* Price END
) "2009-08-03"
FROM Orders
WHERE 1=1
AND Order_Date BETWEEN '2009-08-01' AND CAST('2009-08-03' AS datetime) +1
GROUP BY Cust_Name --218, ORACLE
SELECT Cust_Name
, MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009-08-01"
, MAX(CASE WHEN Order_Date ='2009-08-02' THEN AR END) "2009-08-02"
, MAX(CASE WHEN Order_Date ='2009-08-03' THEN AR END) "2009-08-03"
FROM
(
SELECT TO_CHAR(Order_Date, 'YYYY-MM-DD')Order_Date
, CUST_NAME
, SUM(Qty * Price) AR
FROM Orders
WHERE Order_Date BETWEEN DATE'2009-08-01'
AND DATE'2009-08-03' +1
GROUP BY TO_CHAR(Order_Date, 'YYYY-MM-DD')
, CUST_NAME
UNION ALL
SELECT TO_CHAR(Order_Date, 'YYYY-MM-DD') Order_Date
,NULL CUST_NAME
, SUM(Qty * Price) AR
FROM Orders
WHERE Order_Date BETWEEN DATE'2009-08-01'
AND TO_DATE('2009-08-03', 'YYYY-MM-DD') +1
GROUP BY Order_Date
) A
GROUP BY Cust_Name --220, SQL SERVER
SELECT Cust_Name
, "2009-08-01"
, "2009-08-02"
, "2009-08-03"
FROM
(
SELECT CONVERT(CHAR(10),Order_Date, 120) Order_Date
, Cust_Name
, Qty * Price AR
FROM Orders
WHERE 1=1
AND Order_Date BETWEEN '2009-08-01' AND '2009-08-04'
)AS D
PIVOT
(
SUM(AR)
FOR Order_Date
IN ([2009-08-01], [2009-08-02], "2009-08-03")
) AS P
--220, SQL SERVER
SELECT Order_Date
,[李先生]
, [张先生]
, [曹先生]
, [陈先生]
FROM
(
SELECT CONVERT(CHAR(10),Order_Date, 120) Order_Date
, Cust_Name
, Qty
, Price
, Qty*Price AS AR
FROM Orders
WHERE 1=1
AND Order_Date BETWEEN '2009-08-01' AND '2009-08-04'
)AS D
PIVOT
(
SUM(AR)
FOR Cust_Name
IN ([李先生], [张先生], [曹先生], [陈先生])
) AS P --222, ORACLE
SELECT Cust_Name
, "2009-08-01"
, "2009-08-02"
, "2009-08-03"
FROM
(
SELECT TO_CHAR(Order_Date, 'YYYY-MM-DD') Order_Date
, Cust_Name
--, Qty
--, Price
, Qty * Price AR
FROM ORDERS
WHERE 1=1
AND Order_Date BETWEEN date'2009-08-01' AND date'2009-08-04'
) D
PIVOT
(
SUM(AR)
FOR Order_Date
IN ('2009-08-01' AS "2009-08-01", '200908-02' "2009-08-02", '2009-08-03' "2009-08-03")
) P --223, ORACLE
SELECT *
FROM
(
SELECT TO_CHAR(Order_Date, 'YYYY-MM-DD') OrderDate
, Cust_Name
, Qty
, Price
FROM Orders
WHERE 1=1
AND Order_Date BETWEEN date'2009-08-01' AND date'2009-08-04'
)
PIVOT
(
SUM(Qty * Price) AS "AR"
, COUNT(*) AS "Qty"
FOR OrderDate
IN ('2009-08-01' AS "08-01", '2009-08-02' "08-02", '2009-08-03' "08-03")
) P
--226, SQL SERVER
DROP TABLE Orders_Pivot
G0 SELECT Cust_Name
, MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009-08-01"
, MAX(CASE WHEN Order_Date ='2009-08-02' THEN AR END) "2009-08-02"
, MAX(CASE WHEN Order_Date ='2009-08-03' THEN AR END) "2009-08-03"
INTO Orders_Pivot
FROM
(
SELECT CONVERT(CHAR(10), Order_Date, 120) Order_Date
, CUST_NAME
, SUM(Qty * Price) AR
FROM Orders
WHERE 1=1
AND Order_Date BETWEEN '2009-08-01' AND CAST('2009-08-03' AS datetime) +1
GROUP BY CONVERT(CHAR(10), Order_Date, 120)
, CUST_NAME
) A
GROUP BY Cust_Name --226, ORACLE
DROP TABLE Orders_Pivot; CREATE TABLE Orders_Pivot
AS
SELECT Cust_Name
, MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009-08-01"
, MAX(CASE WHEN Order_Date ='2009-08-02' THEN AR END) "2009-08-02"
, MAX(CASE WHEN Order_Date ='2009-08-03' THEN AR END) "2009-08-03"
FROM
(
SELECT TO_CHAR(Order_Date, 'YYYY-MM-DD')Order_Date
, CUST_NAME
, SUM(Qty * Price) AR
FROM Orders
WHERE Order_Date BETWEEN DATE'2009-08-01'
AND DATE'2009-08-03' +1
GROUP BY TO_CHAR(Order_Date, 'YYYY-MM-DD')
, CUST_NAME
) A
GROUP BY Cust_Name --227, SQL SERVER
SELECT Order_Date, Cust_Name, AR
FROM
(
SELECT CAST('2009-08-01' AS datetime) Order_Date -- SQL SERVER
--DATE'2009-08-01' -- ORACLE
, Cust_Name
, "2009-08-01" AR
FROM Orders_Pivot
UNION ALL
SELECT CAST('2009-08-02' AS datetime) Order_Date
, Cust_Name
, "2009-08-02" AR
FROM Orders_Pivot
UNION ALL
SELECT CAST('2009-08-03' AS datetime) Order_Date
, Cust_Name
, "2009-08-03" AR
FROM Orders_Pivot
) A
WHERE AR IS NOT NULL --227, ORACLE
SELECT Order_Date, Cust_Name, AR
FROM
(
SELECT DATE'2009-08-01' Order_Date
, Cust_Name
, "2009-08-01" AR
FROM Orders_Pivot
UNION ALL
SELECT DATE'2009-08-02' Order_Date
, Cust_Name
, "2009-08-02" AR
FROM Orders_Pivot
UNION ALL
SELECT DATE'2009-08-03' Order_Date
, Cust_Name
, "2009-08-03" AR
FROM Orders_Pivot
) A
WHERE AR IS NOT NULL --228, SQL SERVER
SELECT Order_Date
, Cust_Name
, AR
, SUBSTRING(Order_Date, 6,5) "Date"
FROM
(
SELECT Cust_Name, "2009-08-01" , "2009-08-02", "2009-08-03"
FROM Orders_Pivot
) D
UNPIVOT
(
AR FOR Order_Date
IN ([2009-08-01], [2009-08-02], "2009-08-03")
) P --228, ORACLE
SELECT Order_Date
, Cust_Name
, AR
, SUBSTR(Order_Date, 6,5) "Date"
FROM
(
SELECT Cust_Name, "2009-08-01" , "2009-08-02", "2009-08-03"
FROM Orders_Pivot
) D
UNPIVOT
(
AR FOR Order_Date
IN ("2009-08-01", "2009-08-02", "2009-08-03")
) P --230, ORALCE / SQL SERVER
SELECT P.Cust_Name
, P."2009-08-01"
, P."2009-08-02"
, P."2009-08-03"
, I.N
FROM Orders_Pivot P, Tally I
WHERE 1=1
AND N<=3
ORDER BY 1, 4 --231, ORALCE / SQL SERVER
SELECT Order_Date
, Cust_Name
, AR
FROM
(
SELECT
CASE N WHEN 1 THEN '2009-08-01'
WHEN 2 THEN '2009-08-02'
WHEN 3 THEN '2009-08-03'
END Order_Date
, Cust_Name
, CASE N WHEN 1 THEN "2009-08-01"
WHEN 2 THEN "2009-08-02"
WHEN 3 THEN "2009-08-03"
END AR
FROM Orders_Pivot P, Tally I
WHERE 1=1
AND N<=3
) A
WHERE AR IS NOT NULL
--234, ORACLE/SQL SERVER
SELECT EmpName
, Major
, ROW_NUMBER() OVER (PARTITION BY EmpName ORDER BY Major) FldIdx
FROM Specialty --236, ORACLE / SQL SERVER
SELECT EmpName
, MAX(CASE WHEN FldIdx=1 THEN Major END) Fld1
, MAX(CASE WHEN FldIdx=2 THEN Major END) Fld2
, MAX(CASE WHEN FldIdx=3 THEN Major END) Fld3
, COUNT(*) FldCount
FROM
(
SELECT EmpName
, Major
, ROW_NUMBER() OVER (PARTITION BY EmpName ORDER BY Major) FldIdx
FROM Specialty
) A
GROUP BY EmpName
ORDER BY COUNT(*)DESC
SqlServer和Oracle中一些常用的sql语句3 行列转换的更多相关文章
- SqlServer和Oracle中一些常用的sql语句9 SQL优化
--SQL查询优化 尽量避免使用or,not,distinct运算符,简化连接条件 /*Or运算符*/ use db_business go select * from 仓库 where 城市='北京 ...
- SqlServer和Oracle中一些常用的sql语句5 流程控制语句
--在sql语句中 begin...end 用来设定一个程序块 相关于c#中的{} declare @yz real,@w int --声明变量 set @w=120 --为变量赋值 if @w< ...
- SqlServer和Oracle中一些常用的sql语句10 特殊应用
--482, ORACLE / SQL SERVER --订购数量超过平均值的书籍 WITH Orders_Book AS ( SELECT Book_Name, SUM(Qty) Book_Qty ...
- SqlServer和Oracle中一些常用的sql语句6 存储过程
--不带参数的存储过程 CREATE procedure proc_sql1 as begin declare @i int set @i=0 while @i<26 begin print c ...
- SqlServer和Oracle中一些常用的sql语句7 游标
declare db_cursor4 scroll cursor for select * from 供应商 --声明游标 open db_cursor4 --打开游标 fetch first fro ...
- SqlServer和Oracle中一些常用的sql语句8 触发器和事务
--创建和执行事后触发器 --更新仓库备份表中记录时自动创建数据表且插入三条记录 create trigger db_trigger1 on 仓库备份 for update as begin if E ...
- SqlServer和Oracle中一些常用的sql语句4 局部/全局变量
--把wh1仓库号中姓名含有"平"字的职工工资在原来的基础上加288 update 职工备份 set 工资=工资+288 where 仓库号='wh1' and 姓名 like ' ...
- SqlServer 获取字符串中小写字母的sql语句
SQL字符串截取(SubString) 作用:返回第一个参数中从第二个参数指定的位置开始.第三个参数指定的长度的子字符串. 有时候我们会截取字符串中的一些特殊想要的东西,大小写字母.模号.汉字.数字等 ...
- SqlServer 查看备份文件中逻辑文件信息的Sql语句
RESTORE FILELISTONLY FROM DISK = 'D:\All\DataBase\(2013-12-18)-1.bak' 用来查看备份文件中的逻辑文件信息. 相关信息:SqlServ ...
随机推荐
- YYHS-NOIP2017SummerTraining0914-问题 A: 组合数问题
题目描述 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1, 2, 3)三个物品中选择两个物品可以有(1, 2),(1, 3),(2, 3)这三种选择方法.根据组合数的定义 ...
- make: Nothing to be done for 'all' 解决方法
make: Nothing to be done for 'all' 解决方法 1.这句提示是说明你已经编译好了,而且没有对代码进行任何改动. 若想重新编译,可以先删除以前编译产生的目标文件:make ...
- 《深入浅出MySQL》之数据类型
MySQL提供了多种数据类型,主要包括数值型.字符串型和日期时间类型.本次博客就来谈谈MySQL中常用的数据类型吧(版本:mysql-5.7.19)! 数值类型 MySQL支持所有标准SQL中数值类型 ...
- 团队作业3--需求改进&系统设计
小学生四则运算练习软件APP 一.需求&原型改进 1.给目标用户展现原型,与目标用户进一步沟通理解需求 我们的主要目标用户是小学生,次要目标用户是小学教师 场景一:小明一个三年级的学生,放学回 ...
- 201521123035《Java程序设计》第七周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...
- 使用Eclipse Egit与码云管理你的代码
总体流程: 建立远程仓库 建立本地仓库并与远程仓库关联 将Eclipse中的项目提交到本地仓库并进而push到远程仓库 一. 配置Eclipse EGit 图解Eclipse中安装及配置EGit插件中 ...
- 201521123081《java程序设计》 第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- Java课程设计——猜数游戏(201521123111 陈伟泽)
Java课程设计--猜数游戏(201521123111 陈伟泽) 1.团队课程设计博客链接 博客作业--猜数游戏 2.个人负责模块或任务说明 Answer:一些基础界面的构造,排行榜的构造,用文件录入 ...
- 201521123018 《Java程序设计》第14周学习总结
1. 本章学习总结 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) 添 ...
- 201521123008 《Java程序设计》 第九周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避 ...