一些有用的SQL语句

mac2022-06-30  22

1)从部门主管是张三往上找,下面找2阶。如果去掉Step的限制就可以抓取所有的阶

with cte as(select *,1 as step from HRDepartment where MasterName='趙三' union allselect a.*,b.step+1 as step from HRDepartment as a,cte as b where a.DepartmentCode=b.ParentCode)select * from cte where step<=2

2)行转列,列转行的做法

create table #t1( name varchar(10), stID int, math float, English float ) insert #t1 values('A1',1,10.1,20.2) insert #t1 values('A2',2,30.3,40.4) SELECT P.name,P.stID,P.className,P.ClassNo into #t2 FROM ( SELECT name,stID, math, English FROM #t1 )T UNPIVOT ( ClassNo FOR className IN ( math, English ) ) P select * from #t2 select * from #t2 pivot(max(ClassNo) for className in (math,English))a drop table #t2 drop table #t1

 

转载于:https://www.cnblogs.com/wonder223/p/7685303.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)