id yearmonth d001 d001date d002 d002date
001 2006-4-1 2.00 2006-6-6 2.00 null
001 2006-5-1 1.00 null 2.00 2006-6-6
001 2006-6-1 3.00 2006-6-6 4.00 2006-6-6
002 2006-6-1 2.00 2006-6-6 2.00 2006-6-6
003 2006-6-1 null null 1.00 2006-6-6
004 2006-6-1 4.00 2006-9-8 3.00 2006-6-6
005 2005-5-1 1.00 2006-6-6 2.00 null
006 2005-5-1 1.00 null 2.00 null
007 2005-5-1 null 2006-6-6 2.00 2006-6-6
需要得到的结果是:
001 11.00 2006-6-6
002 4.00 2006-6-6
003 1.00 2006-6-6
004 3.00 2006-6-6
004 4.00 2006-9-8
005 1.00 2006-6-6
007 2.00 2006-6-6
12 个解决方案
#1
看的不是很懂
#2
004 3.00 2006-6-6
004 4.00 2006-9-8
為什麼是兩條??
004 4.00 2006-9-8
為什麼是兩條??
#3
看明白了。
Create Table TEST
(id Char(3),
yearmonth Varchar(10),
d001 Numeric(10,2),
d001date Varchar(10),
d002 Numeric(10,2),
d002date Varchar(10))
Insert TEST Select '001', '2006-4-1', 2.00, '2006-6-6', 2.00, null
Union All Select '001', '2006-5-1', 1.00, null , 2.00, '2006-6-6'
Union All Select '001', '2006-6-1', 3.00, '2006-6-6', 4.00, '2006-6-6'
Union All Select '002', '2006-6-1', 2.00, '2006-6-6', 2.00, '2006-6-6'
Union All Select '003', '2006-6-1', null , null , 1.00, '2006-6-6'
Union All Select '004', '2006-6-1', 4.00, '2006-9-8', 3.00, '2006-6-6'
Union All Select '005', '2005-5-1', 1.00, '2006-6-6', 2.00, null
Union All Select '006', '2005-5-1', 1.00, null , 2.00, null
Union All Select '007', '2005-5-1', null , '2006-6-6', 2.00, '2006-6-6'
GO
Select
id,
ddate,
SUM(d) As d
From(
Select id,IsNull(d001,0.0) As d,d001date As ddate From TEST Where d001date Is Not Null
Union All
Select id,IsNull(d002,0.0),d002date From TEST Where d002date Is Not Null)A
Group By id,ddate
Order By id,ddate
GO
Drop Table TEST
--Result
/*
id ddate d
001 2006-6-6 11.00
002 2006-6-6 4.00
003 2006-6-6 1.00
004 2006-6-6 3.00
004 2006-9-8 4.00
005 2006-6-6 1.00
007 2006-6-6 2.00
*/
Create Table TEST
(id Char(3),
yearmonth Varchar(10),
d001 Numeric(10,2),
d001date Varchar(10),
d002 Numeric(10,2),
d002date Varchar(10))
Insert TEST Select '001', '2006-4-1', 2.00, '2006-6-6', 2.00, null
Union All Select '001', '2006-5-1', 1.00, null , 2.00, '2006-6-6'
Union All Select '001', '2006-6-1', 3.00, '2006-6-6', 4.00, '2006-6-6'
Union All Select '002', '2006-6-1', 2.00, '2006-6-6', 2.00, '2006-6-6'
Union All Select '003', '2006-6-1', null , null , 1.00, '2006-6-6'
Union All Select '004', '2006-6-1', 4.00, '2006-9-8', 3.00, '2006-6-6'
Union All Select '005', '2005-5-1', 1.00, '2006-6-6', 2.00, null
Union All Select '006', '2005-5-1', 1.00, null , 2.00, null
Union All Select '007', '2005-5-1', null , '2006-6-6', 2.00, '2006-6-6'
GO
Select
id,
ddate,
SUM(d) As d
From(
Select id,IsNull(d001,0.0) As d,d001date As ddate From TEST Where d001date Is Not Null
Union All
Select id,IsNull(d002,0.0),d002date From TEST Where d002date Is Not Null)A
Group By id,ddate
Order By id,ddate
GO
Drop Table TEST
--Result
/*
id ddate d
001 2006-6-6 11.00
002 2006-6-6 4.00
003 2006-6-6 1.00
004 2006-6-6 3.00
004 2006-9-8 4.00
005 2006-6-6 1.00
007 2006-6-6 2.00
*/
#4
001为什么只有1条,给个理由先
#5
哈,和我開始一樣,你再慢慢看,就明白了。
#6
他的意思是 d001与d001date成对, d002与d002成对
当某一个为null时,对应的数据就当不存在,当两组都有值时,就拆成两条记录.
昨天好像刚写过类似的, 不过是针对于以"|"分隔的,而不是多个字段.
当某一个为null时,对应的数据就当不存在,当两组都有值时,就拆成两条记录.
昨天好像刚写过类似的, 不过是针对于以"|"分隔的,而不是多个字段.
#7
对拆过的记录再分组
#8
我倒,按时间统计总和,有Null的数据不要^^;
CSDN上时间长了,设计成什么样乱七八糟的表都见了
CSDN上时间长了,设计成什么样乱七八糟的表都见了
#9
瑞士2:0赢了
韩国被逼上绝路了-_-
韩国被逼上绝路了-_-
#10
CSDN上时间长了,设计成什么样乱七八糟的表都见了
---------
哈哈!:)
---------
哈哈!:)
#11
已经搞定,多谢各位
#12
paoluo(一天到晚游泳的鱼) 正解
#1
看的不是很懂
#2
004 3.00 2006-6-6
004 4.00 2006-9-8
為什麼是兩條??
004 4.00 2006-9-8
為什麼是兩條??
#3
看明白了。
Create Table TEST
(id Char(3),
yearmonth Varchar(10),
d001 Numeric(10,2),
d001date Varchar(10),
d002 Numeric(10,2),
d002date Varchar(10))
Insert TEST Select '001', '2006-4-1', 2.00, '2006-6-6', 2.00, null
Union All Select '001', '2006-5-1', 1.00, null , 2.00, '2006-6-6'
Union All Select '001', '2006-6-1', 3.00, '2006-6-6', 4.00, '2006-6-6'
Union All Select '002', '2006-6-1', 2.00, '2006-6-6', 2.00, '2006-6-6'
Union All Select '003', '2006-6-1', null , null , 1.00, '2006-6-6'
Union All Select '004', '2006-6-1', 4.00, '2006-9-8', 3.00, '2006-6-6'
Union All Select '005', '2005-5-1', 1.00, '2006-6-6', 2.00, null
Union All Select '006', '2005-5-1', 1.00, null , 2.00, null
Union All Select '007', '2005-5-1', null , '2006-6-6', 2.00, '2006-6-6'
GO
Select
id,
ddate,
SUM(d) As d
From(
Select id,IsNull(d001,0.0) As d,d001date As ddate From TEST Where d001date Is Not Null
Union All
Select id,IsNull(d002,0.0),d002date From TEST Where d002date Is Not Null)A
Group By id,ddate
Order By id,ddate
GO
Drop Table TEST
--Result
/*
id ddate d
001 2006-6-6 11.00
002 2006-6-6 4.00
003 2006-6-6 1.00
004 2006-6-6 3.00
004 2006-9-8 4.00
005 2006-6-6 1.00
007 2006-6-6 2.00
*/
Create Table TEST
(id Char(3),
yearmonth Varchar(10),
d001 Numeric(10,2),
d001date Varchar(10),
d002 Numeric(10,2),
d002date Varchar(10))
Insert TEST Select '001', '2006-4-1', 2.00, '2006-6-6', 2.00, null
Union All Select '001', '2006-5-1', 1.00, null , 2.00, '2006-6-6'
Union All Select '001', '2006-6-1', 3.00, '2006-6-6', 4.00, '2006-6-6'
Union All Select '002', '2006-6-1', 2.00, '2006-6-6', 2.00, '2006-6-6'
Union All Select '003', '2006-6-1', null , null , 1.00, '2006-6-6'
Union All Select '004', '2006-6-1', 4.00, '2006-9-8', 3.00, '2006-6-6'
Union All Select '005', '2005-5-1', 1.00, '2006-6-6', 2.00, null
Union All Select '006', '2005-5-1', 1.00, null , 2.00, null
Union All Select '007', '2005-5-1', null , '2006-6-6', 2.00, '2006-6-6'
GO
Select
id,
ddate,
SUM(d) As d
From(
Select id,IsNull(d001,0.0) As d,d001date As ddate From TEST Where d001date Is Not Null
Union All
Select id,IsNull(d002,0.0),d002date From TEST Where d002date Is Not Null)A
Group By id,ddate
Order By id,ddate
GO
Drop Table TEST
--Result
/*
id ddate d
001 2006-6-6 11.00
002 2006-6-6 4.00
003 2006-6-6 1.00
004 2006-6-6 3.00
004 2006-9-8 4.00
005 2006-6-6 1.00
007 2006-6-6 2.00
*/
#4
001为什么只有1条,给个理由先
#5
哈,和我開始一樣,你再慢慢看,就明白了。
#6
他的意思是 d001与d001date成对, d002与d002成对
当某一个为null时,对应的数据就当不存在,当两组都有值时,就拆成两条记录.
昨天好像刚写过类似的, 不过是针对于以"|"分隔的,而不是多个字段.
当某一个为null时,对应的数据就当不存在,当两组都有值时,就拆成两条记录.
昨天好像刚写过类似的, 不过是针对于以"|"分隔的,而不是多个字段.
#7
对拆过的记录再分组
#8
我倒,按时间统计总和,有Null的数据不要^^;
CSDN上时间长了,设计成什么样乱七八糟的表都见了
CSDN上时间长了,设计成什么样乱七八糟的表都见了
#9
瑞士2:0赢了
韩国被逼上绝路了-_-
韩国被逼上绝路了-_-
#10
CSDN上时间长了,设计成什么样乱七八糟的表都见了
---------
哈哈!:)
---------
哈哈!:)
#11
已经搞定,多谢各位
#12
paoluo(一天到晚游泳的鱼) 正解