【问题标题】:Get data with condition获取有条件的数据
【发布时间】:2019-07-11 20:02:22
【问题描述】:
declare @t table (
    Id      int ,
    Section int,
    Moment  date
);

insert into @t values
(   1   ,   1   , '2014-01-01'),
(   2   ,   1   , '2014-01-02'),
(   3   ,   1   , '2014-01-03'),
(   4   ,   1   , '2014-01-04'),
(   5   ,   1   , '2014-01-05'),

(   6   ,   2   , '2014-02-06'),
(   7   ,   2   , '2014-02-07'),
(   8   ,   2   , '2014-02-08'),
(   9   ,   2   , '2014-02-09'),
(   10  ,   2   , '2014-02-10'),

(   11  ,   3   , '2014-03-11'),
(   12  ,   3   , '2014-03-12'),
(   13  ,   3   , '2014-03-13'),
(   14  ,   3   , '2014-03-14'),
(   15  ,   3   , '2014-03-15');

获取这样的数据

select * from @t

Id  Section Moment
1   1   2014-01-01
2   1   2014-01-02
3   1   2014-01-03
4   1   2014-01-04
5   1   2014-01-05
6   2   2014-02-06
7   2   2014-02-07
8   2   2014-02-08
9   2   2014-02-09
10  2   2014-02-10
11  3   2014-03-11
12  3   2014-03-12
13  3   2014-03-13
14  3   2014-03-14
15  3   2014-03-15

但我想要 this.group by 3 和 Section wise 之类的数据

如果 ant Section 有 5 行,则会创建 2 个组。

Id  Section Moment  Group by 3
1   1   1/1/2014    1
2   1   1/2/2014    1
3   1   1/3/2014    1
4   1   1/4/2014    2
5   1   1/5/2014    2
6   2   2/6/2014    3
7   2   2/7/2014    3
8   2   2/8/2014    3
9   2   2/9/2014    4
10  2   2/10/2014   4
11  3   3/11/2014   5
12  3   3/12/2014   5
13  3   3/13/2014   5
14  3   3/14/2014   6
15  3   3/15/2014   6

【问题讨论】:

    标签: sql database sql-server-2008


    【解决方案1】:

    您可以使用窗口函数和算术。下面列举了每个部分:

    select (row_number() over (partition by section order by moment) + 2) / 3, t.*
    from @t;
    

    然后应用dense_rank() 得到你想要的:

    select dense_rank() over (order by section, tempcol) as group3,
           t.*
    from (select (row_number() over (partition by section order by moment) + 2) / 3 as tempcol, t.*
          from t
         ) t
    order by id
    

    Here 是一个 dbfiddle。

    【讨论】:

    • 您好先生,您的答案最大到最大接近我想要的答案,但不同的是,第一列 id 应该增加。 (无列名) Id Section Moment 1 8 2 2014-02-08 2 9 2 2014-02-09 2 10 2 2014-02-10 1 11 3 2014-03-11 1 12 3 2014-03-12 1 13 3 2014-03-13 2 14 3 2014-03-14 2 15 3 2014-03-15
    • @radhasingh。 . .这会完全返回您想要的,除了“第 3 组”值位于第一列而不是最后一列,因此更容易查看。
    • 请帮我解决这个话题stackoverflow.com/questions/54742413/…
    【解决方案2】:

    解释可能有错误,但直到哪里承认我相信你正在寻找这个问题。我已经使用光标完成了它。希望它对你有所帮助。

    DECLARE @i int =0 -- row count 
    DECLARE @GroupCount int=1 
    DECLARE @Id int
    DECLARE @Section int
    DECLARE @Moment DateTime
    
    
    declare @temp table (
        SNO int,
        Id      int ,
        Section int,
        Moment  date,
        GroupedIn nvarchar(200)
    );
    DECLARE db_cursor CURSOR FOR 
    SELECT Id,Section,Moment       
    FROM @t 
    WHERE Section = 3 --suppose 
    
    OPEN db_cursor  
    FETCH NEXT FROM db_cursor INTO @Id,@Section,@Moment  
    
    WHILE @@FETCH_STATUS = 0  
    BEGIN  
    Set @i=@i+1
    
    Insert into @temp values(@i,@Id,@Section,@Moment,'G'+CONVERT(nvarchar(20),@GroupCount))
          FETCH NEXT FROM db_cursor INTO @Id,@Section,@Moment   
    END 
    
    CLOSE db_cursor  
    DEALLOCATE db_cursor 
    
    Select * from @temp
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多