我有一个查询,查询到的数据列表如下:
每个内容都有一个特定的类别,每个类别都有一个用于根据类别播放或不根据类别播放的复选框。
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个小组:
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
,我想要下面的顺序:
第一组的第一内容,第二组的第一内容,第三组的第一内容
第一组的第二个内容,第二组的第二个,第三组的第二个
第一组的第3个内容,第二组的第3个,第三组的第3个
但是,如果任何类别组的IsCategoryWise
为0
,那么它们将始终处于相同的顺序:
第一组的第一内容,(第二组的所有3个内容),第三组的第一内容
第一组的第二个内容,(第二组的所有三个内容),第三组的第二个内容
所需的输出:
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查询或C#linq中怎么可能?
答案 0 :(得分:1)
请参考代码中的注释以获取解释
-- 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 :(得分:0)
POST
“ orderby”字段进行正确的排序。