本帖最后由 ancestor1 于 2010-03-03 17:20:09 编辑

解决方案 »

  1.   

    去sql版问吧,比较easy得到答案
      

  2.   

    如果ID是连续的
    select distinct t1.Name from A t1,A t2 where t1.id-t2.id=1 and t1.Name=t2.Name如果不是连续的,先放到一张带自增长字段的临时表。
      

  3.   

    方法重载指南   
      当类包含两个名称相同但签名不同的方法时发生方法重载。本节提供一些使用重载方法的指南。     
      用方法重载来提供在语义上完成相同功能的不同方法。     
      使用方法重载而不是允许默认参数。默认参数的版本控制性能不好,因此公共语言规范   (CLS)   中不允许使用默认参数。下面的代码示例阐释重载的   String.IndexOf   方法。     
      [Visual   Basic]   
      Function   String.IndexOf(name   As   String)   As   Integer   
      Function   String.IndexOf(name   As   String,   startIndex   As   Integer)   As   Integer   
      [C#]   
      int   String.IndexOf   (String   name);   
      int   String.IndexOf   (String   name,   int   startIndex);   
      正确使用默认值。在一个重载方法系列中,复杂方法应当使用参数名来指示从简单方法中假定的默认状态发生的更改。例如,在下面的代码中,第一个方法假定搜索将不区分大小写。第二个方法用名称   ignoreCase   而不是   caseSensitive   来指示正在如何更改默认行为。     
      [Visual   Basic]   
      '   Method   #1:   ignoreCase   =   false.   
      Function   Type.GetMethod(name   As   String)   As   MethodInfo   
      '   Method   #2:   Indicates   how   the   default   behavior   of   method   #1     
      '   is   being   changed.   
      Function   Type.GetMethod(name   As   String,   ignoreCase   As   Boolean)   As   MethodInfo   
      [C#]   
      //   Method   #1:   ignoreCase   =   false.   
      MethodInfo   Type.GetMethod(String   name);     
      //   Method   #2:   Indicates   how   the   default   behavior   of   method   #1   is   being   //   changed.   
        MethodInfo   Type.GetMethod   (String   name,   Boolean   ignoreCase);   
      对方法参数使用一致的排序和命名模式。提供一组重载方法,这组重载方法带有递增数目的参数,以使开发人员可以指定想要的级别的信息,这种情况很常见。您指定的参数越多,开发人员就可指定得越详细。在下面的代码示例中,重载的   Execute   方法具有一致的参数顺序和命名模式变体。每个   Execute   方法变体都对共享的参数集使用相同的语义。     
      [Visual   Basic]   
      Public   Class   SampleClass   
            Private   defaultForA   As   String   =   "default   value   for   a"   
            Private   defaultForB   As   Integer   =   "42"   
            Private   defaultForC   As   Double   =   "68.90"   
                    
            Overloads   Public   Sub   Execute()   
                  Execute(defaultForA,   defaultForB,   defaultForC)   
            End   Sub   
                    
            Overloads   Public   Sub   Execute(a   As   String)   
                  Execute(a,   defaultForB,   defaultForC)   
            End   Sub   
                    
            Overloads   Public   Sub   Execute(a   As   String,   b   As   Integer)   
                  Execute(a,   b,   defaultForC)   
            End   Sub   
                    
            Overloads   Public   Sub   Execute(a   As   String,   b   As   Integer,   c   As   Double)   
                  Console.WriteLine(a)   
                  Console.WriteLine(b)   
                  Console.WriteLine(c)   
                  Console.WriteLine()   
            End   Sub     
      End   Class   
      [C#]   
      public   class   SampleClass   
      {   
            readonly   string   defaultForA   =   "default   value   for   a";   
            readonly   int   defaultForB   =   "42";   
            readonly   double   defaultForC   =   "68.90";   
              
            public   void   Execute()   
            {   
                  Execute(defaultForA,   defaultForB,   defaultForC);   
            }   
                                
            public   void   Execute   (string   a)   
            {   
                  Execute(a,   defaultForB,   defaultForC);   
            }   
                          
            public   void   Execute   (string   a,   int   b)   
            {   
                  Execute   (a,   b,   defaultForC);             
            }   
              
            public   void   Execute   (string   a,   int   b,   double   c)   
            {   
                  Console.WriteLine(a);   
                  Console.WriteLine(b);   
                  Console.WriteLine(c);   
                  Console.WriteLine();   
            }     
      }   
      请注意,组中唯一的虚拟方法应该是具有大多数参数的方法,并且只应在需要可扩展性时才应有虚拟方法。     
      如果必须提供重写方法的能力,请仅使最完整的重载是虚拟的并根据它来定义其他操作。下面的示例阐释了这种模式。     
      [Visual   Basic]   
      Public   Class   SampleClass   
            Private   myString   As   String   
              
            Public   Sub   New(str   As   String)   
                  Me.myString   =   str   
            End   Sub   
              
            Overloads   Public   Function   IndexOf(s   As   String)   As   Integer   
                  Return   IndexOf(s,   0)   
            End   Function   
              
            Overloads   Public   Function   IndexOf(s   As   String,   startIndex   As     
                              Integer)   As   Integer   
                  Return   IndexOf(s,   startIndex,   myString.Length   -   startIndex)   
            End   Function   
              
            Overloads   Public   Overridable   Function   IndexOf(s   As   String,     
                              startIndex   As   Integer,   count   As   Integer)   As   Integer   
                  Return   myString.IndexOf(s,   startIndex,   count)   
            End   Function     
      End   Class   
      [C#]   
      public   class   SampleClass   
      {   
            private   string   myString;   
        
            public   MyClass(string   str)   
            {   
                  this.myString   =   str;   
            }   
              
            public   int   IndexOf(string   s)     
            {   
                  return   IndexOf   (s,   0);   
            }   
        
            public   int   IndexOf(string   s,   int   startIndex)     
            {   
                  return   IndexOf(s,   startIndex,   myString.Length   -   startIndex   );   
            }   
        
            public   virtual   int   IndexOf(string   s,   int   startIndex,   int   count)     
            {   
                  return   myString.IndexOf(s,   startIndex,   count);   
            }   
      }   
      带有可变数目的参数的方法   
      您可能希望公开带可变数目的参数的方法。C   编程语言中的   printf   方法是一个经典的示例。对于托管类库,请对此构造使用   params(Visual   Basic   中为   ParamArray)关键字。例如,使用以下代码代替几个重载的方法。   
      [Visual   Basic]   
      Sub   Format(formatString   As   String,   ParamArray   args()   As   Object)   
      [C#]   
      void   Format(string   formatString,   params   object   []   args)   
      不应以独占方式使用   VarArgs   或省略号   (...)   调用约定,公共语言规范不支持它。   
      对于极度性能敏感的代码,您可能希望为少量元素提供特殊代码路径。只有在打算将整个代码路径作为特殊情况处理(而不只是创建数组和调用更为通用的方法)时才可以这么做。在这种情况下,建议使用下面的模式来在性能和特殊处理的代码的成本之间进行协调。   
      [Visual   Basic]   
      Sub   Format(formatString   As   String,   arg1   As   Object)   
      Sub   Format(formatString   As   String,   arg1   As   Object,   arg2   As   Object)   
        
      Sub   Format(formatString   As   String,   ParamArray   args()   As   Object)   
      [C#]   
      void   Format(string   formatString,   object   arg1)   
      void   Format(string   formatString,   object   arg1,   object   arg2)   
              
      void   Format(string   formatString,   params   object   []   args)   
      
    拷贝别人的
      

  4.   

    Create table #A
    (
    ID int,
    Name1 varchar(50),
    type1 int
    )
    insert into #A
      select 1,'张三',0
    union all select 2,'李四',1
    union all select 3,'王五',0
    union all select 4,'张三',1
    union all select 5,'张三',1select * from #A---
    select *,ROW_NUMBER() OVER(order by ID) as Row from #A where type1=1  
    下班了,没完成。。谁在这基础上试试吧。。
      

  5.   

    好像完成了。Create table #A
    (
    ID int,
    Name1 varchar(50),
    type1 int
    )
    insert into #A
      select 1,'张三',0
    union all select 2,'李四',1
    union all select 3,'王五',0
    union all select 4,'张三',1
    union all select 5,'张三',1
    union all select 6,'阿奇',1
    union all select 7,'阿奇',1select * from #A
    --delete from #A---
    select a.* from #A as a  
    inner join #A as b on a.Name1=b.Name1
    where a.type1=1 and a.ID-b.ID=1
    order by a.ID
      

  6.   

    create table aa
    (
      id  int,
      name varchar(6),
      type int
      
    )
    insert into aa values(1,'张三',0)
    insert into aa values(2,'李四',1)
    insert into aa values(3,'王五',0)
    insert into aa values(4,'张三',1)
    insert into aa values(5,'张三',1)select * from aa a
    inner join(select *
    from aa 
    where type=1
    ) b on a.name=b.name and a.id-b.id=1
    ---------------------------
    5  张三  1 4 张三 1
      

  7.   

    Create table #A
    (
    ID int,
    Name1 varchar(50),
    type1 int
    )
    insert into #A
              select 1,'张三',0
    union all select 2,'李四',1
    union all select 3,'王五',0
    union all select 4,'张三',1
    union all select 5,'张三',1select * from #A T WHERE EXISTS(SELECT 1 FROM #A WHERE NAME1=T.NAME1 AND ID=T.ID+1)
    UNION ALL
    select * from #A T WHERE EXISTS(SELECT 1 FROM #A WHERE NAME1=T.NAME1 AND ID=T.ID-1)
    (所影响的行数为 5 行)ID          Name1                                              type1       
    ----------- -------------------------------------------------- ----------- 
    4           张三                                                 1
    5           张三                                                 1(所影响的行数为 2 行)
      

  8.   

    ------------------------------------------------------------------------
    -- Author : HappyFlyStone 
    -- Date   : 2010-03-03 18:14:06
    -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) 
    --       Apr 14 2006 01:12:25 
    --       Copyright (c) 1988-2005 Microsoft Corporation
    --       Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
    --      
    -------------------------------------------------------------------------- Test Data: ta
    IF OBJECT_ID('[ta]') IS NOT NULL 
        DROP TABLE [ta]
    Go
    CREATE TABLE ta([ID] INT,[NAME] NVARCHAR(2),[TYPE] NVARCHAR(1))
    Go
    INSERT INTO ta
       SELECT 1,'张三','0' UNION ALL
       SELECT 2,'李四','1' UNION ALL
       SELECT 3,'王五','0' UNION ALL
       SELECT 4,'张三','1' UNION ALL
       SELECT 5,'张三','1' 
    GO
    --Start
    SELECT 
    top 1 name 
    FROM
    TA a
    where exists(select 1 from ta where id  = a.id +1 and type= a.type)
          and type = 1
    --Result:
    /*
    name
    ----
    张三(1 行受影响)
    */
    --End 
      

  9.   

    Hi,  Another 0)表和数据--这里ID并没有简单的做IDENTITY处理IF OBJECT_ID('[ta]') IS NOT NULL 
        DROP TABLE [ta]
    Go
    CREATE TABLE ta([ID] INT,[NAME] NVARCHAR(2),[TYPE] NVARCHAR(1))
    Go
    INSERT INTO ta
       SELECT 1,'张三','0' UNION ALL
       SELECT 2,'李四','1' UNION ALL
       SELECT 3,'王五','0' UNION ALL
       SELECT 4,'王五','0' UNION ALL
       SELECT 5,'张三','1' UNION ALL
       SELECT 6,'张三','1' UNION ALL
       SELECT 7,'张三','1' UNION ALL
       SELECT 8,'张三','0' UNION ALL
       SELECT 9,'李四','1' UNION ALL
       SELECT 10,'李四','1'UNION ALL
       SELECT 15,'王五','1' UNION ALL
       SELECT 18,'王五','1' UNION ALL
       SELECT 23,'皇帝','1' UNION ALL
       SELECT 19,'王五','1' UNION ALL
       SELECT 20,'皇帝','1' UNION ALL
       SELECT 22,'皇帝','1'
    GO--SELECT * FROM ta1)取出ID和RANK的差(Minus) 作为后续判断ID是否连续的标志WITH CTE AS
    (SELECT ID,NAME,TYPE,RANK,Minus  = ID - RANK
    FROM
    (SELECT * ,RANK = RANK()OVER(PARTITION BY NAME,TYPE ORDER BY ID) FROM ta
    WHERE TYPE = 1)A)2)CTE预览,CTE仅可被引用一次,故已注释.--SELECT * FROM CTE ID          NAME TYPE RANK                 Minus
    ----------- ---- ---- -------------------- --------------------
    20          皇帝   1    1                    19
    22          皇帝   1    2                    20
    23          皇帝   1    3                    20
    2           李四   1    1                    1
    9           李四   1    2                    7
    10          李四   1    3                    7
    15          王五   1    1                    14
    18          王五   1    2                    16
    19          王五   1    3                    16
    5           张三   1    1                    4
    6           张三   1    2                    4
    7           张三   1    3                    4(12 row(s) affected)
    3)取出Minus中出现重复的记录
     
    SELECT * FROM CTE A
    WHERE A.Minus IN
    (SELECT B.Minus FROM CTE B
    WHERE A.ID <> B.ID)
    ORDER BY A.ID4)连续的ID的NAME相等且TYPE等于1的记录,结果展示如下:ID          NAME TYPE RANK                 Minus
    ----------- ---- ---- -------------------- --------------------
    5           张三   1    1                    4
    6           张三   1    2                    4
    7           张三   1    3                    4
    9           李四   1    2                    7
    10          李四   1    3                    7
    18          王五   1    2                    16
    19          王五   1    3                    16
    22          皇帝   1    2                    20
    23          皇帝   1    3                    20(9 row(s) affected) 
      

  10.   

    Hi,--参照#7,修改如下:SELECT  * FROM
    (
    SELECT * FROM ta A WHERE EXISTS (SELECT * FROM ta WHERE NAME = A.NAME AND ID = A.ID - 1  )
    UNION 
    SELECT * FROM ta A WHERE EXISTS (SELECT * FROM ta WHERE NAME = A.NAME AND ID = A.ID + 1  )
    )A
    WHERE A.TYPE = 1
      

  11.   

    IF OBJECT_ID('[ta]') IS NOT NULL 
        DROP TABLE [ta]
    Go
    CREATE TABLE ta([ID] INT,[NAME] NVARCHAR(2),[TYPE] NVARCHAR(1))
    Go
    INSERT INTO ta
       SELECT 1,'张三','0' UNION ALL
       SELECT 2,'李四','1' UNION ALL
       SELECT 3,'王五','0' UNION ALL
       SELECT 4,'张三','1' UNION ALL
       SELECT 5,'张三','1' 
    GOselect name
    from
    (
    select group_id = (select count(1) from ta where id <= t.id and [NAME]=t.[NAME])-id,*
    from ta t
    ) r
    group by group_id,name
    having sum(case TYPE when '1' then 1 else 0 end) = 2
    --------------------------------
    张三
      

  12.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb(ID  int ,NAME nvarchar(20),   TYPE int)
    insert into tb
     select 1 ,   '张三',    0 union all
     select 2 ,   '李四'  ,  1 union all
     select 3 ,   '王五' ,   0 union all
     select 4 ,   '张三'  ,  1 union all
     select 5 ,   '张三'  ,  1 
    select * from tb t where exists (select * from tb where id>t.id and type=t.type and name=t.name)
      

  13.   


    SELECT [name],count(*) AS num INTO #tmp_tb FROM tb WHERE type=1 GROUP BY [name]
    SELECT * FROM #tmp_tb WHERE num=2
      

  14.   

    或者这样也行!SELECT * 
    FROM (SELECT [name],count(*) AS num FROM ta WHERE type=1 GROUP BY [name]) a  
    WHERE a.num=2