SQL语句 自连表查询。inner join用法,partition by ,列转行查询
use mydb1
go
-- 表T_Employee2
-- Id Name Position Dept
-- 1 张三 员工 市场部
-- 2 李四 经理 销售部
-- 3 王五 经理 市场部
-- 4 马六 员工 销售部
-- 5 钱七 员工 市场部
select * from T_Employee2 -- 需求:查询表中所有员工以及所在部门的经理姓名。(使用表自连接查询) -- 分析:要求返回结果
-- Name Position Dept 经理
-- 张三 员工 市场部 王五
-- 马六 员工 销售部 李四
-- 钱七 员工 市场部 王五 -- 1、查出 每个部门对应的经理姓名
select Dept,Name from T_Employee2 where Position='经理'
-- 结果:
-- Dept Name
-- 销售部 李四
-- 市场部 王五 -- 2、查出 所有的员工
select * from T_Employee2 where Position='员工'
-- 结果:
-- Id Name Position Dept
-- 1 张三 员工 市场部
-- 4 马六 员工 销售部
-- 5 钱七 员工 市场部 --3、上面2个表连接查询,把1个表当做2个表来查询。
select t1.Name,Position,t1.Dept,t2.Name '经理' from
(select * from T_Employee2 where Position='员工') as t1
inner join (select Dept,Name from T_Employee2 where Position='经理')as t2 on t1.Dept = t2.Dept --4、简化后结果
select t1.Name,Position,t1.Dept,t2.Name '经理' from T_Employee2
as t1
inner join (select Dept,Name from T_Employee2 where Position='经理')as t2 on t1.Dept = t2.Dept
where t1.Position='员工' -- 结果:
-- Name Position Dept 经理
-- 张三 员工 市场部 王五
-- 马六 员工 销售部 李四
-- 钱七 员工 市场部 王五
记录开窗函数分组查询
--自连表查询
select t.A,t.B,t.C,t.D from T_D as t
inner join (select B,row_number() over(order by MIN(A)) as E from T_D group by B) tt on t.B=tt.B
order by tt.E
select * from T_D order by min(A) over(partition by B)
下面是列转行
go
if OBJECT_ID('tempdb..#temp') is not null
drop table #temp
create table #temp(fnum int, fa int, fb int)
insert into #temp values
(15070, 1, 3),
(15070, 2, 0),
(15070, 3, 3),
(15070, 4, 1),
(15070, 5, 0),
(15070, 7, 1),
(15070, 8, 1),
(15070, 9, 1),
(15070, 10, 0), (15071, 1, 3),
(15071, 2, 0),
(15071, 3, 1),
(15071, 4, 3),
(15071, 5, 0),
(15071, 7, 3),
(15071, 8, 1),
(15071, 9, 3),
(15071, 10, 3), (15072, 1, 3),
(15072, 2, 3),
(15072, 3, 0),
(15072, 4, 1),
(15072, 5, 0),
(15072, 7, 1),
(15072, 8, 0),
(15072, 9, 0),
(15072, 10, 0)
go
--select * from #temp
--请求转化成:
--fnum 1 2 3 4 5 7 8 9 10
--15070 3 0 3 1 0 1 1 1 0
--15071 3 0 1 3 0 3 1 3 3
--15072 3 3 0 1 0 1 0 0 0 select * from (select distinct fnum from #temp) as A
OUTER APPLY
(select [fbs]= replace(replace(
(select fb as value FROM #temp as B
where fnum = A.fnum order by B.fa FOR XML AUTO
),'<B value="',''),'"/>',' ')
)B --创建新表
go
if OBJECT_ID('tempdb..#new') is not null
drop table #new
create table #new(fnum int,l_1 int,l_2 int,l_3 int,l_4 int,l_5 int,l_7 int,l_8 int,l_9 int,l_10 int) --循环导入数据
declare @i int = 1;
declare @count int = (select count(*) from (select distinct fnum from #temp) as a);
declare @fnum varchar(50),@fbs varchar(50),@sql varchar(100); while @i<=@count
begin select @fnum=fnum, @fbs=fbs from
(select fnum,fbs,ROW_NUMBER() over(order by fnum) as row from (select distinct fnum from #temp) as A
OUTER APPLY
(select [fbs]= replace(replace(
(select fb as value FROM #temp as B
where fnum = A.fnum order by B.fa FOR XML AUTO
),'<B value="',''),'"/>',' ')
)B)tmp
where tmp.row = @i; set @sql='insert into #new values('+@fnum+',';
set @sql=@sql + replace(@fbs,' ',',')+')';
set @sql=replace(@sql,',)',')');
exec (@sql) --执行SQL
--PRINT @sql set @i=@i+1
end go select * from #new select fnum,
max(case fa when 1 then fb end) F1,
max(case fa when 2 then fb end) F2,
max(case fa when 3 then fb end) F3,
max(case fa when 4 then fb end) F4,
max(case fa when 5 then fb end) F5,
max(case fa when 7 then fb end) F7,
max(case fa when 8 then fb end) F8,
max(case fa when 9 then fb end) F9,
max(case fa when 10 then fb end) F10
from #temp group by fnum
SQL语句 自连表查询。inner join用法,partition by ,列转行查询的更多相关文章
- SQL语句之 多表管理
SQL语句之 多表管理 一个数据库内通常会有不止一张表,有时候我们要把多张表联系起来,这就需要用到多表管理的语句. 1.外键约束 一个表中的非主键字段,如果在另外一张表中是主键,那么这个字段我们叫它做 ...
- 使用sql语句获取数据库表的信息
下面的sql语句可以查看表的信息.其中modify_date和create_date可以根据表的修改时间来查看.如果不需要删除后,就能看到所有表的字段信息 ) PERCENT d.name AS 表名 ...
- sql语句中----删除表数据drop、truncate和delete的用法
sql语句中----删除表数据drop.truncate和delete的用法 --drop drop table tb --tb表示数据表的名字,下同 删除内容和定义,释放空间.简单来说就是把整 ...
- (转载)用SQL语句创建Access表
<来源网址:http://www.delphifans.com/infoview/Article_220.html>用SQL语句创建Access表 很久以前弄的,用了一天的时间,没有什么技 ...
- ylb:sql语句重命名表名和列名
ylbtech-SQL Server:SQL Server-sql语句重命名表名和列名 sql语句重命名表名和列名 ylb:sql语句重命名表名和列名 返回顶部 一.更改数据库名 sp_rena ...
- Mybatis中动态SQL语句中的parameterType不同数据类型的用法
Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型, 此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...
- SQL语句中count(1)count(*)count(字段)用法的区别
SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...
- SQL语句中count(1)count(*)count(字段)用法的区别(转)
SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...
- sql语句中3表删除和3表查询
好久没来咱们博客园了,主要近期在忙一些七七八八的杂事,包括打羽毛球比赛的准备和自己在学jqgrid的迷茫.先不扯这些没用的了,希望大家能记得小弟,小弟在此谢过大家了. 回归正题:(以下的sql是本人在 ...
随机推荐
- FreeBSD编译安装emacs,不要用ports
1. 解压emacs 2. 进入解压之后的目录,执行configure命令,大体配置如下: ./configure --with-x-toolkit=no --with-xpm=no --with-j ...
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
/** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...
- Easyui 基于kindeditor的扩展
源码 /** * Author : ____′↘夏悸 * Easyui KindEditor的简单扩展. * 有了这个之后,你就可以像使用Easyui组件的方式使用KindEditor了 * 前提是你 ...
- Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成
Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成,它们的细节如下: Web 模块提供面向web的基本功能和面向web的应用上下文,比如多部分(multipa ...
- WinRAR 5.01 正式版 (简体中文)附注册机及注册码
软件分类:数据压缩软件大小:1.91 MB 软件类别:国外软件 软件授权:注册版软件语言:简体中文点击进入:官方主页 应用平台:Win 8.Win 7.Win 2008 R2.Win 2008.Wi ...
- 怎样在asp.net中用一般处理文件ashx实现下载功能
/// <summary> /// 下载文件,支持大文件.续传.速度限制.支持续传的响应头Accept-Ranges.ETag,请求头Range . /// Accept-Ranges:响 ...
- 制作dos启动u盘
需要准备的工具: 空U盘的U盘HP优盘启动盘格式化工具 链接:https://pan.baidu.com/s/1i59wgUp 密码:l5ke 1.1插入U盘,打开 HP优盘启动盘格式化工具 1. ...
- 设计模式之备忘录模式(Memento)
备忘录模式(Memento) 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. Originator(发起人):负责创建一个备忘录 ...
- 多个 python的pip版本选择
如果你电脑里面装了多个版本的python python3 -m pip instatll xlutilspython2 -m pip instatll xlutils 加载新的pippython -m ...
- joisino's travel
D - joisino's travel Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement T ...