我有一个获取数据列表的查询,如下所示:
其中每个内容都有一个特定的类别,并且每个类别都有一个用于基于类别播放它的复选框。
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,我希望序列如下所示
第一组内容第一,第二组第一,第三组第一
第一组第二内容,第二组第二,第三组第二
第一组的第三内容,第二组的第三,第三组的第三
但是,如果任何类别组的IsCategoryWise为0,则它们将始终处于相同的序列中:
第一组的第一个内容,(第二组的所有三个内容),第三组的第一个
第一组第二个内容,(第二组所有三个内容),第三组第二个
所需输出:
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中实现?
发布于 2019-06-22 14:52:16
请参考代码内注释进行解释
-- 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
*/发布于 2019-06-22 16:42:24
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‘进行了正确的排序。
https://stackoverflow.com/questions/56712184
复制相似问题