fID fDate fNO
--------------------------------------
001 1900-01-01 FQ
001 2003-05-04 GC
002 1900-01-01 FQ
002 2007-03-04 GC
002 1900-01-01 BX
002 2007-03-05 AJ
002 2007-02-04 KG
...................
同一个fID号,把fDate为1900-01-01的日期更新为:
同一个fID号,fNO字段值为GC的fDate不为1900-01-01的fDate值
同一个fID号,fNO字段值为GC的fDate为1900-01-01,AJ的fDate不为1900-01-01的值
同一个fID号,fNO字段值为GC的fDate为1900-01-01,AJ的fDate为1900-01-01,FQ的fDate不为1900-01-01的值
此思路顺延顺序:
GC
AJ
FQ
KG
BX
8 个解决方案
#1
已写出了一个,看我的写法到底有没有进步,请高手们先拿出自已的方案
#2
更新成什么阿?
#3
完全不懂意思...
#4
晕,看来要好好学习学习母语了,
也就是一个判断, 属于同一个fID的几条记录中,如果有哪条记录的fDate值是1900-01-01,就需要改变一下,变成这几条记录中fDate值不为1900-01-01的日期,当然这几条记录的fDate值可能不同,到底需要哪一条呢,就需要找这几条中fNO值中是否有“GC”,如果fNO=“GC”且fDate不是1900-01-01,就把fDate值是1900-01-01的改成fNO=“GC”的fDate值,如果没有fNO=“GC”且fDate不是1900-01-01,那就找是不是有fNO=“AJ”且fDate不是1900-01-01,如果有就改.....
不行了,只能描述成这样了
也就是一个判断, 属于同一个fID的几条记录中,如果有哪条记录的fDate值是1900-01-01,就需要改变一下,变成这几条记录中fDate值不为1900-01-01的日期,当然这几条记录的fDate值可能不同,到底需要哪一条呢,就需要找这几条中fNO值中是否有“GC”,如果fNO=“GC”且fDate不是1900-01-01,就把fDate值是1900-01-01的改成fNO=“GC”的fDate值,如果没有fNO=“GC”且fDate不是1900-01-01,那就找是不是有fNO=“AJ”且fDate不是1900-01-01,如果有就改.....
不行了,只能描述成这样了
#5
疯了...
#6
晕了
#7
我帖我的写法吧,不知大家明不明白,我的写法还有一些问题,个别的,需要大伙帮忙看看
----------------------------------------------------------------------------------
/* ----------------------------实验环节------------------------------------------------------------------------------- */
Declare @tContract Table(fID char(10),fSaleDate datetime,fNO char(10))
Insert @tContract Select
'001','1900-01-01','AJ' Union select
'001','2003-05-04','GC' Union select
'001','1998-05-04','GPS' Union select
'002','2003-05-04','AJ' Union select
'002','2001-05-04','GC' Union select
'002','1900-01-01','FQ' Union select
'002','1900-01-01','GPS' Union select
'003','1900-01-01','DK' Union select
'003','1985-01-01','FQ' Union select
'003','1900-01-01','BX' Union select
'003','2003-05-04' ,'GPS'
Select * From @tContract
Update a
Set
fSaleDate =
(Case
When left(b.fNO,2) = 'GC' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'AJ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'FQ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,3) = 'GPS' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'BX' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'KG' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'DK' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
End
)
From
@tContract a,
@tContract b
Where
a.fID = b.fID AND a.fSaleDate = '1900-01-01' AND b.fSaleDate <> '1900-01-01'
Select * From @tContract
所影响的行数为 11 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1900-01-01 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 1900-01-01 00:00:00.000 FQ
002 1900-01-01 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1900-01-01 00:00:00.000 BX
003 1900-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
(所影响的行数为 5 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1998-05-04 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 2001-05-04 00:00:00.000 FQ
002 2001-05-04 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1985-01-01 00:00:00.000 BX
003 1985-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
上面的写法还有一点问题,大家看fID是001的,我需要的结果是fNO = 'AJ'的值要变成:
2003-05-04 00:00:00.000
但实际上却变成了:
1998-05-04 00:00:00.000
,不知我这个判断写法有什么问题
----------------------------------------------------------------------------------
/* ----------------------------实验环节------------------------------------------------------------------------------- */
Declare @tContract Table(fID char(10),fSaleDate datetime,fNO char(10))
Insert @tContract Select
'001','1900-01-01','AJ' Union select
'001','2003-05-04','GC' Union select
'001','1998-05-04','GPS' Union select
'002','2003-05-04','AJ' Union select
'002','2001-05-04','GC' Union select
'002','1900-01-01','FQ' Union select
'002','1900-01-01','GPS' Union select
'003','1900-01-01','DK' Union select
'003','1985-01-01','FQ' Union select
'003','1900-01-01','BX' Union select
'003','2003-05-04' ,'GPS'
Select * From @tContract
Update a
Set
fSaleDate =
(Case
When left(b.fNO,2) = 'GC' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'AJ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'FQ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,3) = 'GPS' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'BX' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'KG' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'DK' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
End
)
From
@tContract a,
@tContract b
Where
a.fID = b.fID AND a.fSaleDate = '1900-01-01' AND b.fSaleDate <> '1900-01-01'
Select * From @tContract
所影响的行数为 11 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1900-01-01 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 1900-01-01 00:00:00.000 FQ
002 1900-01-01 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1900-01-01 00:00:00.000 BX
003 1900-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
(所影响的行数为 5 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1998-05-04 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 2001-05-04 00:00:00.000 FQ
002 2001-05-04 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1985-01-01 00:00:00.000 BX
003 1985-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
上面的写法还有一点问题,大家看fID是001的,我需要的结果是fNO = 'AJ'的值要变成:
2003-05-04 00:00:00.000
但实际上却变成了:
1998-05-04 00:00:00.000
,不知我这个判断写法有什么问题
#8
顶一下
#1
已写出了一个,看我的写法到底有没有进步,请高手们先拿出自已的方案
#2
更新成什么阿?
#3
完全不懂意思...
#4
晕,看来要好好学习学习母语了,
也就是一个判断, 属于同一个fID的几条记录中,如果有哪条记录的fDate值是1900-01-01,就需要改变一下,变成这几条记录中fDate值不为1900-01-01的日期,当然这几条记录的fDate值可能不同,到底需要哪一条呢,就需要找这几条中fNO值中是否有“GC”,如果fNO=“GC”且fDate不是1900-01-01,就把fDate值是1900-01-01的改成fNO=“GC”的fDate值,如果没有fNO=“GC”且fDate不是1900-01-01,那就找是不是有fNO=“AJ”且fDate不是1900-01-01,如果有就改.....
不行了,只能描述成这样了
也就是一个判断, 属于同一个fID的几条记录中,如果有哪条记录的fDate值是1900-01-01,就需要改变一下,变成这几条记录中fDate值不为1900-01-01的日期,当然这几条记录的fDate值可能不同,到底需要哪一条呢,就需要找这几条中fNO值中是否有“GC”,如果fNO=“GC”且fDate不是1900-01-01,就把fDate值是1900-01-01的改成fNO=“GC”的fDate值,如果没有fNO=“GC”且fDate不是1900-01-01,那就找是不是有fNO=“AJ”且fDate不是1900-01-01,如果有就改.....
不行了,只能描述成这样了
#5
疯了...
#6
晕了
#7
我帖我的写法吧,不知大家明不明白,我的写法还有一些问题,个别的,需要大伙帮忙看看
----------------------------------------------------------------------------------
/* ----------------------------实验环节------------------------------------------------------------------------------- */
Declare @tContract Table(fID char(10),fSaleDate datetime,fNO char(10))
Insert @tContract Select
'001','1900-01-01','AJ' Union select
'001','2003-05-04','GC' Union select
'001','1998-05-04','GPS' Union select
'002','2003-05-04','AJ' Union select
'002','2001-05-04','GC' Union select
'002','1900-01-01','FQ' Union select
'002','1900-01-01','GPS' Union select
'003','1900-01-01','DK' Union select
'003','1985-01-01','FQ' Union select
'003','1900-01-01','BX' Union select
'003','2003-05-04' ,'GPS'
Select * From @tContract
Update a
Set
fSaleDate =
(Case
When left(b.fNO,2) = 'GC' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'AJ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'FQ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,3) = 'GPS' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'BX' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'KG' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'DK' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
End
)
From
@tContract a,
@tContract b
Where
a.fID = b.fID AND a.fSaleDate = '1900-01-01' AND b.fSaleDate <> '1900-01-01'
Select * From @tContract
所影响的行数为 11 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1900-01-01 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 1900-01-01 00:00:00.000 FQ
002 1900-01-01 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1900-01-01 00:00:00.000 BX
003 1900-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
(所影响的行数为 5 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1998-05-04 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 2001-05-04 00:00:00.000 FQ
002 2001-05-04 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1985-01-01 00:00:00.000 BX
003 1985-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
上面的写法还有一点问题,大家看fID是001的,我需要的结果是fNO = 'AJ'的值要变成:
2003-05-04 00:00:00.000
但实际上却变成了:
1998-05-04 00:00:00.000
,不知我这个判断写法有什么问题
----------------------------------------------------------------------------------
/* ----------------------------实验环节------------------------------------------------------------------------------- */
Declare @tContract Table(fID char(10),fSaleDate datetime,fNO char(10))
Insert @tContract Select
'001','1900-01-01','AJ' Union select
'001','2003-05-04','GC' Union select
'001','1998-05-04','GPS' Union select
'002','2003-05-04','AJ' Union select
'002','2001-05-04','GC' Union select
'002','1900-01-01','FQ' Union select
'002','1900-01-01','GPS' Union select
'003','1900-01-01','DK' Union select
'003','1985-01-01','FQ' Union select
'003','1900-01-01','BX' Union select
'003','2003-05-04' ,'GPS'
Select * From @tContract
Update a
Set
fSaleDate =
(Case
When left(b.fNO,2) = 'GC' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'AJ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'FQ' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,3) = 'GPS' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'BX' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'KG' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
When left(b.fNO,2) = 'DK' AND b.fSaleDate <> '1900-01-01' Then b.fSaleDate
End
)
From
@tContract a,
@tContract b
Where
a.fID = b.fID AND a.fSaleDate = '1900-01-01' AND b.fSaleDate <> '1900-01-01'
Select * From @tContract
所影响的行数为 11 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1900-01-01 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 1900-01-01 00:00:00.000 FQ
002 1900-01-01 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1900-01-01 00:00:00.000 BX
003 1900-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
(所影响的行数为 5 行)
fID fSaleDate fNO
---------- ------------------------------------------------------ ----------
001 1998-05-04 00:00:00.000 AJ
001 1998-05-04 00:00:00.000 GPS
001 2003-05-04 00:00:00.000 GC
002 2001-05-04 00:00:00.000 FQ
002 2001-05-04 00:00:00.000 GPS
002 2001-05-04 00:00:00.000 GC
002 2003-05-04 00:00:00.000 AJ
003 1985-01-01 00:00:00.000 BX
003 1985-01-01 00:00:00.000 DK
003 1985-01-01 00:00:00.000 FQ
003 2003-05-04 00:00:00.000 GPS
(所影响的行数为 11 行)
上面的写法还有一点问题,大家看fID是001的,我需要的结果是fNO = 'AJ'的值要变成:
2003-05-04 00:00:00.000
但实际上却变成了:
1998-05-04 00:00:00.000
,不知我这个判断写法有什么问题
#8
顶一下