首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按给定顺序更改序列

按给定顺序更改序列
EN

Stack Overflow用户
提问于 2019-06-22 10:37:30
回答 2查看 1.4K关注 0票数 0

我有一个获取数据列表的查询,如下所示:

其中每个内容都有一个特定的类别,并且每个类别都有一个用于基于类别播放它的复选框。

代码语言:javascript
复制
Id  Category    content_type   Content   IsCategorywise
=====================================================
1   ABC         Image          content1  1
2   ABC         Image          content2  1
3   EFG         Video          content3  0
4   EFG         Image          content4  0
5   EFG         Image          content5  0
6   XYZ         Image          content6  1
7   XYZ         Image          content7  1

所以在这里我得到了3个组:

代码语言:javascript
复制
Id  Category    content_type   Content   IsCategorywise
=====================================================
1   ABC         Image          content1  1
2   ABC         Image          content2  1

Id  Category    content_type   Content   IsCategorywise
=====================================================
3   EFG         Video          content3  0
4   EFG         Image          content4  0
5   EFG         Image          content5  0

Id  Category    content_type   Content   IsCategorywise
=====================================================
6   XYZ         Image          content6  1
7   XYZ         Image          content7  1

现在,如果所有内容的IsCategoryWise都为1,我希望序列如下所示

第一组内容第一,第二组第一,第三组第一

第一组第二内容,第二组第二,第三组第二

第一组的第三内容,第二组的第三,第三组的第三

但是,如果任何类别组的IsCategoryWise0,则它们将始终处于相同的序列中:

第一组的第一个内容,(第二组的所有三个内容),第三组的第一个

第一组第二个内容,(第二组所有三个内容),第三组第二个

所需输出:

代码语言:javascript
复制
Id  Category    content_type   Content   IsCategorywise
=====================================================
1   ABC         Image          content1  1
3   EFG         Video          content3  0
4   EFG         Image          content4  0
5   EFG         Image          content5  0
6   XYZ         Image          content6  1

2   ABC         Image          content2  1
3   EFG         Video          content3  0
4   EFG         Image          content4  0
5   EFG         Image          content5  0
7   XYZ         Image          content7  1

如何在SQL Server query或C# linq中实现?

EN

回答 2

Stack Overflow用户

发布于 2019-06-22 14:52:16

请参考代码内注释进行解释

代码语言:javascript
复制
-- create sample table
declare @tbl table
(
    Id              int,
    Category        varchar(10),
    content_type    varchar(10),  
    Content         varchar(10), 
    IsCategorywise  int
)

-- insert sample data
insert into @tbl 
values
    (1,   'ABC',         'Image',          'content1',  1),
    (2,   'ABC',         'Image',          'content2',  1),
    (3,   'EFG',         'Video',          'content3',  0),
    (4,   'EFG',         'Image',          'content4',  0),
    (5,   'EFG',         'Image',          'content5',  0),
    (6,   'XYZ',         'Image',          'content6',  1),
    (7,   'XYZ',         'Image',          'content7',  1)

-- the query
; with 
-- use numbers / tally table if you have one. 
numbers as
(
    select  n = 1
    union all
    select  n = n + 1
    from    numbers
    where   n < 10
),
cte as
(
    select  *,      
            -- Group wise IsCategorywise value
            GrpCW   = min(IsCategorywise) over (partition by Category),
            -- generate row_number for GrpCW = 1 for each category order by Id
            rn  = case  when    min(IsCategorywise) over (partition by Category) = 1
                        then    row_number() over (partition by Category order by Id)
                        end
    from    @tbl
),
cte2 as
(
    select  *, 
            -- m is for repeating the GrpCW = 0 for each grouping
            m = case when GrpCW = 1 then 1 else max(rn) over () end
    from    cte
)
-- basically you want to order by "rn" but for cases where IsCategorywise = 0,
-- you want to repeat it. That is where the inner join to "numbers" comes in
select  Id, Category, content_type, Content, IsCategorywise
from    cte2 c
        inner join numbers n    on  n.n <= c.m
order by coalesce(rn, n), Category, Id

/*
Id          Category   content_type Content    IsCategorywise
----------- ---------- ------------ ---------- --------------
1           ABC        Image        content1   1
3           EFG        Video        content3   0
4           EFG        Image        content4   0
5           EFG        Image        content5   0
6           XYZ        Image        content6   1
2           ABC        Image        content2   1
3           EFG        Video        content3   0
4           EFG        Image        content4   0
5           EFG        Image        content5   0
7           XYZ        Image        content7   1
*/
票数 1
EN

Stack Overflow用户

发布于 2019-06-22 16:42:24

代码语言:javascript
复制
SELECT
  Id
  ,Category
  ,content_type
  ,Content
  ,IsCategorywise
FROM (

    SELECT
      Id
      ,Category
      ,content_type
      ,Content
      ,IsCategorywise
      ,ROW_NUMBER() OVER ( ORDER BY Category,Id) as orderby
    FROM myTable
    WHERE IsCategorywise=1
    UNION ALL
    SELECT
      Id
      ,Category
      ,content_type
      ,Content
      ,IsCategorywise
      ,1.5
    FROM myTable
    WHERE IsCategorywise=0
    UNION ALL
    SELECT
      Id
      ,Category
      ,content_type
      ,Content
      ,IsCategorywise
      ,3.5
    FROM myTable
    WHERE IsCategorywise=0
) X
ORDER BY orderby

字段'orderby‘进行了正确的排序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56712184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档