⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sql.txt

📁 sql横向连接多个数据表的语法
💻 TXT
字号:
假设每人每月最多请4天假   
  明细表如下:   
  职工ID       请假日期         原因   
      1                 2003.9.1       A   
      1                 2003.9.5       B   
      1                 2003.9.15     C   
      1                 2003.9.18     D   
      2                 2003.9.2       A   
      2                 2003.9.5       A   
      .....   
  如何生成报表如下   
    ID           日期1       原因       日期2         原因       日期3           原因       日期4       原因   
    1             2003.9.1   A         2003.9.5       B           2003.9.15     C           2003.9.18     D   
    2             2003.9.2   A         2003.9.5       A   
  .........   


select   职工ID,   
  min(case   when   flag=1   then   请假日期   end)   日期1,   
  min(case   when   flag=1   then   原因   end)   原因1,   
  min(case   when   flag=2   then   请假日期   end)   日期2,   
  min(case   when   flag=2   then   原因   end)   原因2,   
  min(case   when   flag=3   then   请假日期   end)   日期3,   
  min(case   when   flag=3   then   原因   end)   原因3,   
  min(case   when   flag=4   then   请假日期   end)   日期4,   
  min(case   when   flag=4   then   原因   end)   原因4   
    from   (   
  select   *,(select   count(*)   from   表   where   职工ID=tem.职工ID   and   请假日期<=tem.请假日期)   flag   from   表   tem)   
  aa   group   by   职工ID


create table 表1(编号1 nvarchar(50), 名称1 varchar(10))
insert into 表1 values('WWW' , 'A') 
insert into 表1 values('EEE' , 'B') 
create table 表2 (编号2 nvarchar(50), 名称2 varchar(10),名称3 varchar(10))
insert into 表2 values('RRR' , 'C','DDD') 
insert into 表2 values('TTT' , 'D','334D') 
insert into 表2 values('YYY' , 'E','D35GF') 
go


select m.编号1 , m.名称1 , n.编号2 , n.名称2, n.名称3 from
(select * , px = (select count(1) from 表1 where 编号1 < t.编号1) + '1' from 表1 t) m
full join
(select * , px = (select count(1) from 表2 where 编号2 < t.编号2) + '1' from 表2 t) n
on m.px = n.px


drop table 表1 , 表2







--测试数据   
  create   table   表(coumn1   varchar(10),coumn2   varchar(10),coumn3   varchar(10),coumn4   varchar(10))   
  insert   表   select   '藏','阿里地区','普兰县',50   
  union   all   select   '藏','阿里地区','噶尔县',50   
  union   all   select   '藏','阿里地区','改则县',50   
  union   all   select   '藏','昌都地区','昌都县',50   
  union   all   select   '藏','昌都地区','类乌齐县',50   
  union   all   select   '藏','昌都地区','丁青县',50   
  union   all   select   '藏','昌都地区','八宿县',50   
  union   all   select   '藏','昌都地区','左贡县',50   
  union   all   select   '藏','昌都地区','洛隆县',50   
  go   
    
  --处理   
  select   id=identity(int,1,1),*   into   #t   from   表   
  update   #t   set   coumn4=''   
  from   #t   a   left   join(   
  select   coumn1,coumn2   
  ,id=min(id)+(max(id)-min(id)+1)/2   
  from   #t     
  group   by   coumn1,coumn2   
  )b   on   a.id=b.id   
  where   b.id   is   null   
    
  --显示处理结果   
  select   coumn1,coumn2,coumn3,coumn4   from   #t   
  go   
    
  --删除测试   
  drop   table   表,#t   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -