我有一个包含数据的表,我需要使用定义的字段和变量为双工打印准备数据行。
我创建了一组临时表,如下所示:
#原始数据
Code Department Brand PageNumber SequenceNo Reverse
-----------------------------------------------------
101 201 LG 1 1 0
102 201 Samsung 1 2 0
105 203 Apple 1 3 0
106 203 Nokia 1 4 0
103 202 Sony 2 5 0
104 202 Sony 2 6 0
107 203 TCL 2 7 0
108 203 BenQ 2 8 0 #必需数据
Code Department Brand PageNumber SequenceNo Reverse
-----------------------------------------------------
101 201 LG 1 1 0
102 201 Samsung 1 2 0
105 203 Apple 1 3 0
106 203 Nokia 1 4 0
101 201 LG 1 5 1 - Required
102 201 Samsung 1 6 1 - Required
105 203 Apple 1 7 1 - Required
106 203 Nokia 1 8 1 - Required
103 202 Sony 2 9 0
104 202 Sony 2 10 0
107 203 TCL 2 11 0
108 203 BenQ 2 12 0
103 202 Sony 2 13 1 - Required
104 202 Sony 2 14 1 - Required
107 203 TCL 2 15 1 - Required
108 203 BenQ 2 16 1 - Required我需要第二张表中的数据。我计划在页面前面使用原始数据,在页面后面使用RequireDupexData表中的数据。
是否有一种使用SQL更改SequenceNo顺序的方法?因此,当与原始数据相结合时,就可以以双工打印的方式正确地打印它们。
备注:


更新:修改了我所需的数据,使之更有意义。在我的示例中,我考虑了2列和2行,但这可能会根据页面/模板设计而改变。因此,我认为必须考虑列或行或两者,才能为所需的数据重新排序SequenceNo。
发布于 2020-01-07 20:39:09
我想这个能逆转你的序列号
--drop table #temp
create table #temp(
seq int,
)
insert into #temp
values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14)
declare @third int;
set @third = ceiling((select count(seq)/3.0 from #temp))
select @third
select seq
, case when seq <= @third
then seq + 2*@third
when seq <= 2*@third
then seq
when seq > 2*@third
then seq - 2*@third
end as rseq
from #temp
order by seq aschttps://stackoverflow.com/questions/59631047
复制相似问题