请问怎样得到这个结果

ignacio2222 2012-06-14 04:41:13
初始值:
line count serial
1 2 22
1 3 23
2 7 24

要得到的结果:
1 1 22
1 2 22
1 3 23
1 4 23
1 5 23
2 1 24
2 2 24
2 3 24
2 4 24
2 5 24
2 6 24
2 7 24
...全文
125 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
你的意思是不是count从1开始自增,一直增加到相同line值对应的count之和为止,得到各个line所对应的一个自增序列?那么你的serial这一列是怎么处理的?
[/Quote]

比如说count是2,那么serial就有2个相同的,如果count是3,那么serial就有3个是相同的
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
SQL code

----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2012-06-14 17:12:45
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM)……
[/Quote]

number那里应该是1,2,3,4,5,而不是1,2,1,2,3。。。。。
lycorisraya 2012-06-14
  • 打赏
  • 举报
回复
你的意思是不是count从1开始自增,一直增加到相同line值对应的count之和为止,得到各个line所对应的一个自增序列?那么你的serial这一列是怎么处理的?
Daniel-C 2012-06-14
  • 打赏
  • 举报
回复

int data[][] = { { 1, 2, 22 }, { 1, 3, 23 }, { 2, 7, 24 }, { 2, 7, 25 } };
int rows = data.length;
int temp = 0;
for (int i = 0; i < rows; i++) {
if (0 == i || (data[i][0] != data[i - 1][0])) {
for (int j = 0; j < data[i][1]; j++) {
System.out.print(data[i][0]);
System.out.print(" ");
System.out.print(j + 1);
System.out.print(" ");
System.out.println(data[i][2]);
}
temp = 0;
} else {
if (0 == temp) {
temp = data[i - 1][1] + data[i][1];
} else {
temp += data[i][1];
}
for (int j = data[i - 1][1]; j < temp; j++) {
System.out.print(data[i][0]);
System.out.print(" ");
System.out.print(j + 1);
System.out.print(" ");
System.out.println(data[i][2]);
}
}
}
刚那段程序写的有问题..这个才是对的..你把这段转成sql吧..
--小F-- 2012-06-14
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2012-06-14 17:12:45
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([line] int,[count] int,[serial] int)
insert [tb]
select 1,2,22 union all
select 1,3,23 union all
select 2,7,24
--------------开始查询--------------------------
;with f as
(
select line,serial,sum([count]) as num from tb group by line,serial
)

select
a.line,c.number,b.serial
from
f a join (select distinct line,serial from tb)b
on
a.line=b.line
and
a.serial=b.serial
left join
master..spt_values c
on
c.number<=a.num
and
c.number>0
and
c.[type]='p'

----------------结果----------------------------
/* line number serial
----------- ----------- -----------
1 1 22
1 2 22
1 1 23
1 2 23
1 3 23
2 1 24
2 2 24
2 3 24
2 4 24
2 5 24
2 6 24
2 7 24

(12 行受影响)

*/
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
楼上的大神,可以用sql语句表达不?谢谢啊!
Daniel-C 2012-06-14
  • 打赏
  • 举报
回复

public class Test2 {

public static void main(String sts[]) {
int data[][] = { { 1, 2, 22 }, { 1, 3, 23 }, { 2, 7, 24 } };
int rows = data.length;
for (int i = 0; i < rows; i++) {
if (0 == i || (data[i][0] != data[i - 1][0])) {
for (int j = 0; j < data[i][1]; j++) {
System.out.print(data[i][0]);
System.out.print(" ");
System.out.print(j + 1);
System.out.print(" ");
System.out.println(data[i][2]);
}
} else {
int temp = data[i - 1][1] + data[i][1];
for (int j = data[i - 1][1]; j < temp; j++) {
System.out.print(data[i][0]);
System.out.print(" ");
System.out.print(j + 1);
System.out.print(" ");
System.out.println(data[i][2]);
}
}
}
}
}
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
举一个更显眼的例子吧,
初始值是
1 1 a
1 2 b
2 4 c

想要得到的结果:
1 1 a
1 2 b
1 3 b
2 1 c
2 2 c
2 3 c
2 4 c
猫熊 2012-06-14
  • 打赏
  • 举报
回复
没看明白
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
不知道我这样表述,各位大神能不能看明白。。
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
怎么不是1,2,3 而是12345
[/Quote]

因为是2+3=5,line相同,count就相加
--小F-- 2012-06-14
  • 打赏
  • 举报
回复
怎么不是1,2,3 而是12345
ignacio2222 2012-06-14
  • 打赏
  • 举报
回复
上面是初始数据,后面是要得到的结果,请问怎么写这个语句??
就是根据第一个字段,如果line相同,count就自增。自增的总数为count的数量。

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧