我正在进行查询,我需要根据列值显示n个记录。
E.g。我的输入表如下
ITMREF_0 STOFCY_0 ITCSEQ_0 VLTTOT_0
110205 11 7 7.00000
110205 18 1 22.30130
110206 11 5 0.00000
我想要的输出是第3列中不同列值的第3列中的任何值。第1列的列值将重复那些次。
E.g。第1列(ITMREF_0)有两个不同的数字。 110205 and 110206
对于110205,第3列中有两个不同的值,即7和1。这意味着110205的第2和第4列将从1到7重复,然后是1
所以输出应该是
ITMREF_0 STOFCY_0 ITCSEQ_0 VLTTOT_0
110205 11 1 7.00000
110205 11 2 7.00000
110205 11 3 7.00000
110205 11 4 7.00000
110205 11 5 7.00000
110205 11 6 7.00000
110205 11 7 7.00000
110205 18 1 22.30130
110206 11 1 0.00000
110206 11 2 0.00000
110206 11 3 0.00000
110206 11 4 0.00000
110206 11 5 0.00000
答案 0 :(得分:2)
with cte as
(
select *
from mytable
union all
select ITMREF_0,STOFCY_0,ITCSEQ_0 - 1,VLTTOT_0
from cte
where ITCSEQ_0 > 1
)
select *
from cte
order by ITMREF_0,STOFCY_0,ITCSEQ_0
+----------+----------+----------+----------+
| ITMREF_0 | STOFCY_0 | ITCSEQ_0 | VLTTOT_0 |
+----------+----------+----------+----------+
| 110205 | 11 | 1 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 11 | 2 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 11 | 3 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 11 | 4 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 11 | 5 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 11 | 6 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 11 | 7 | 7.00 |
+----------+----------+----------+----------+
| 110205 | 18 | 1 | 22.30 |
+----------+----------+----------+----------+
| 110206 | 11 | 1 | 0.00 |
+----------+----------+----------+----------+
| 110206 | 11 | 2 | 0.00 |
+----------+----------+----------+----------+
| 110206 | 11 | 3 | 0.00 |
+----------+----------+----------+----------+
| 110206 | 11 | 4 | 0.00 |
+----------+----------+----------+----------+
| 110206 | 11 | 5 | 0.00 |
+----------+----------+----------+----------+
答案 1 :(得分:1)
使用数字表格或数字cte
非常容易:
rextester:http://rextester.com/UCEM72371
create table t (
ITMREF_0 int
, STOFCY_0 int
, ITCSEQ_0 int
, VLTTOT_0 decimal(19,6)
);
insert into t values
(110205 ,11 ,7 ,7)
, (110205 ,18 ,1 ,22.3013)
, (110206 ,11 ,5 ,0) ;
数字cte:
/* numbers table */
with a as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, numbers as (
select n = row_number() over (order by (select 1))
from a as deka
cross join a as hecto
cross join a as kilo
--cross join a as [tenK]
--cross join a as [hundredk]
--cross join a as mega
)
查询:
select
t.ITMREF_0
, t.STOFCY_0
, ITCSEQ_0 = n
, VLTTOT_0
from t
inner join numbers
on numbers.n <= t.ITCSEQ_0
order by
t.ITMREF_0
, t.STOFCY_0
, n
结果:
+----------+----------+----------+-----------+
| ITMREF_0 | STOFCY_0 | ITCSEQ_0 | VLTTOT_0 |
+----------+----------+----------+-----------+
| 110205 | 11 | 1 | 7,000000 |
| 110205 | 11 | 2 | 7,000000 |
| 110205 | 11 | 3 | 7,000000 |
| 110205 | 11 | 4 | 7,000000 |
| 110205 | 11 | 5 | 7,000000 |
| 110205 | 11 | 6 | 7,000000 |
| 110205 | 11 | 7 | 7,000000 |
| 110205 | 18 | 1 | 22,301300 |
| 110206 | 11 | 1 | 0,000000 |
| 110206 | 11 | 2 | 0,000000 |
| 110206 | 11 | 3 | 0,000000 |
| 110206 | 11 | 4 | 0,000000 |
| 110206 | 11 | 5 | 0,000000 |
+----------+----------+----------+-----------+
答案 2 :(得分:0)
Select A.ITMREF_0
,A.STOFCY_0
,ITCSEQ_0 = B.N
,A.VLTTOT_0
From YourTable A
Cross Apply (Select Top (A.ITCSEQ_0) N=Row_Number() Over (Order By Number) From master..spt_values ) b
返回
ITMREF_0 STOFCY_0 ITCSEQ_0 VLTTOT_0
110205 11 1 7.000000
110205 11 2 7.000000
110205 11 3 7.000000
110205 11 4 7.000000
110205 11 5 7.000000
110205 11 6 7.000000
110205 11 7 7.000000
110205 18 1 22.301300
110206 11 1 0.000000
110206 11 2 0.000000
110206 11 3 0.000000
110206 11 4 0.000000
110206 11 5 0.000000