zoukankan      html  css  js  c++  java
  • 一些有用的SQL语句

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

    with cte as
    (
    select *,1 as step from HRDepartment where MasterName='趙三'
    union all
    select 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
  • 相关阅读:
    bzoj 1503: [NOI 2004]郁闷的出纳员
    bzoj 1001: [BeiJing2006]狼抓兔子
    bzoj 1005: [HNOI2008]明明的烦恼
    bzoj 1004: [HNOI2008]Cards
    bzoj 1003: [ZJOI2006]物流运输trans
    06day1
    05day2
    04day2
    04day1
    03day2
  • 原文地址:https://www.cnblogs.com/wonder223/p/7685303.html
Copyright © 2011-2022 走看看