当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: ##include <stdio.h> #include <conio.h> #include <stdlib.h> #include <limits.h> #include <time.h> int in[1000][5]; int person[1000][5]; int tempperson[1000][5]; int gr......
    摘要: treeview控件,如何让其点击结点后再从数据库中取得这个节点的下一层节点? 因为我的树非常庞大,一次读出来,速度会非常慢,只一层一层的去读! 大虾帮帮忙吧!成功后立马付帐!:) ......


这样的sql 怎样写

表内容如下:  
  proj_id   wbs_id   type     name    
  1                 a             001     aa  
  1                 b             001     bb  
  1                 b             003     cc  
  1                 c             001     cc  
  1                 d             001     dd  
  1                 d             003     ee  
  我想查询到如下内容    
  proj_id   wbs_id   type     name     name1  
  1                 a             001     aa           aa  
  1                 b             003     bb           cc  
  1                 c             001     cc           cc  
  1                 d             003     ee           dd  
  也就是说   如果   type是003,相同的proj_id   ,wbs_id   而且type是001  
  这条记录就不显示,但他的   name   字段的值,显示在name1中,  
  这样的sql如何写啊  
  谢谢大虾了  
 

NO.1   作者: yujohny

create   table   表(proj_id   int,wbs_id   nvarchar(1),  
                        type   nvarchar(3),name   nvarchar(2))  
   
  insert   into   表   values(1,a,001,aa)  
  insert   into   表   values(1,b,001,bb)  
  insert   into   表   values(1,b,003,cc)  
  insert   into   表   values(1,c,001,cc)  
  insert   into   表   values(1,d,001,dd)  
  insert   into   表   values(1,d,003,ee)  
   
  select   a.proj_id,a.wbs_id,   001,a.[name],a.[name]   name1  
  from   表   a   left   join   表   b   on   a.proj_id   =   b.proj_id    
                and   a.wbs_id   =   b.wbs_id   and   b.type=003  
    where   a.type   =001   and   b.proj_id   is   null  
  union  
  select   a.proj_id,a.wbs_id,003,max(a.[name]),  
                (select   [name]   from   表   b   where   b.proj_id=a.proj_id    
                  and   b.wbs_id=a.wbs_id   and   b.type=001)   as   name1  
  from   表   a  
  where   a.type=003  
  group   by   a.proj_id,a.wbs_id  
   
  drop   table   表

NO.2   作者: friendliu

select   distinct   proj_id,wbs_id,type,name,name1  
        from   aa

NO.3   作者: happydreamer

 
   
   
  select   *,   case   when   type=001   then   name   else   (select   name   from   table   where   proj_id=a.proj_id   and   wbs_id=a.wbs_id   and   type=001)   end  
    from   table   a  
  where   type   in   (select   top   1   type   from   table   where   proj_id=a.proj_id   and   wbs_id=a.wbs_id   order   by   type   desc)  
 

NO.4   作者: zjcxc

select   a.*  
  ,name1=case   type   when   003   then   (select   name   from   表   where   type<>003   and   proj_id=a.proj_id   and   wbs_id=a.wbs_id)  
  else   name   end  
  from   表   a  
  where   not   exists(select   1   from   表   where   a.type<>003   and   type=003   and   proj_id=a.proj_id   and   wbs_id=a.wbs_id)  
 

NO.5   作者: txlicenhe

select   a.*  
  ,name1=case   type   when   003   then   (select   name   from   表   where   type<>003   and   proj_id=a.proj_id   and   wbs_id=a.wbs_id)  
  else   name   end  
  from   表   a  
  where   not   exists(select   1   from   表   where   a.type<>003   and   type=003   and   proj_id=a.proj_id   and   wbs_id=a.wbs_id)  
 


 ·呵呵    »显示摘要«
    摘要: rt ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE