将一个表中有一列为fatherID,我想把fatherID相同的数据作为一个临时表输出。输出过个表。

解决方案 »

  1.   

    SELECT * INTO #
    FROM T
    WHERE fatherID=XXX
      

  2.   

    你的fatherid相同
    比如
    1 ee 0
    2 dd 1
    3 dd 1
    4 mm 2
    .....
    如果fatherid=1
    是不是只要
    2 dd 1
    3 dd 1
      

  3.   


    select *
    from tb
    where fatherid in (
        select fatherid
        from tb
        group by fatherid
        having count(*) > 1
    )
      

  4.   


    --存在主键字段,假设为Idselect *
    from tb t
    where exists (select 1 from tb where fatherid=t.fatherid and id<>t.id)
      

  5.   

    我的意思是一个表有一列为fatherID,该列为外键,然后该表中有很多行具有相同的fatherID。我想根据fatherID来分成很多个表,然后输出。
      

  6.   

    DECLARE mycursor  CURSOR  for (select fatherID from dbo.Comment
    where $partition.fatherIDFunction(fatherID)=1
    Group by fatherID) 
    DECLARE @tempFatherID bigint
    open mycursor
    fetch next from mycursor into @tempFatherIDwhile(@@FETCH_STATUS=0)
    begin
    select CommentID,replyTime from Comment where $partition.fatherIDFunction(fatherID)=1
    endclose mycursor 
    deallocate mycursor