读书人

一个计算查询语句。大家帮忙啊多谢

发布时间: 2012-02-16 21:30:36 作者: rapoo

一个计算查询语句。。大家帮忙啊!谢谢
我有个表ht
有字段hth(合同号), gcts(购车台数),dkje(贷款金额),dwdm(单位代码),现有一组模拟数据

hth, gcts, dkje, dwdm, bgsj
s039201 5 5000000 039201 2007.07.21
s039201*01 5 3000000 039201 2007.07.21
s039201*02 4 2000000 039201 2007.07.21
s039201*03 4 2000000 039201 2007.07.25
s059101 5 5000000 059101 2007.08.05
s059101*01 5 4000000 059101 2007.07.01
s059101*02 4 3000000 059101 2007.08.05
s062140 5 3000000 062140 2007.07.10
s062140*01 4 4000000 062140 2007.07.10


没有带*的数据是原始数据。带*的是变更后的数据 。我想到两个数据之间的差值。
*01是第一次变更。*02是第2次变更。*03是第3次变更。
得到的结果:07月变更的数据是:
hth, gcts, dkje, dwdm, bgsj


s039201 1 -3000000 039201 2007.07.25
s059101 0 -1000000 059101 2007.07.01
s062140 1 1000000 062140 2007.07.10


08月变更的数据是:
hth, gcts, dkje, dwdm, bgsj
s059101 1 -2000000 059101 2007.08.05


怎么样得到以上结果。谢谢大家

[解决办法]
接分了。。
[解决办法]
s059101550000000591012007.08.05
s059101*01540000000591012007.07.01

----------------------

没有原始数据就已经变更了?
[解决办法]
那他变更在8月怎么算在7月
[解决办法]
呵呵~可能笔误吧~厄
[解决办法]
看不名明白
[解决办法]
liangyuan198412(天山冰客) ( ) 信誉:100 2007-08-30 17:34:27 得分: 0


s059101 2007.08.05 最新的一次变更日期
s059101*01 2007.07.01 在2007.07.01发生了变更
s059101*02 2007.08.05 在2007.08.05又 发生了变更



那这么说下面一行的日期该是2007.07.25咯:

s039201 5 5000000 039201 2007.07.21
[解决办法]
没有带*的数据是原始数据。带*的是变更后的数据 。
……
s059101 2007.08.05 最新的一次变更日期
s059101*01 2007.07.01 在2007.07.01发生了变更
s059101*02 2007.08.05 在2007.08.05又 发生了变更
-------------------------
凭什么第一条是最新变更,而不是第三条,又凭什么不带*的又变成变更数据了。

我实在是不明白,帮不上忙,撤退,抱歉!
[解决办法]
如楼上所说.

变更数据和日期是否有矛盾.
[解决办法]
create table tb(hth varchar(20),gcts int,dkje int,dwdm varchar(10),bgsj datetime)
insert into tb values( 's039201 ' ,5,5000000, '039201 ', '2007.07.25 ')
insert into tb values( 's039201*01 ',5,3000000, '039201 ', '2007.07.21 ')


insert into tb values( 's039201*02 ',4,2000000, '039201 ', '2007.07.21 ')
insert into tb values( 's039201*03 ',4,2000000, '039201 ', '2007.07.25 ')
insert into tb values( 's059101 ' ,5,5000000, '059101 ', '2007.08.05 ')
insert into tb values( 's059101*01 ',5,4000000, '059101 ', '2007.07.01 ')
insert into tb values( 's059101*02 ',4,3000000, '059101 ', '2007.08.05 ')
insert into tb values( 's062140 ' ,5,3000000, '062140 ', '2007.07.10 ')
insert into tb values( 's062140*01 ',4,4000000, '062140 ', '2007.07.10 ')
go

declare @month as int
set @month = 7

select t1.hth , t1.gcts - t2.gcts gcts , t2.dkje - t1.dkje dkje , t1.dwdm , convert(varchar(10),t1.bgsj,120) bgsj from
tb t1,
(
select a.* from
(
select left(hth , charindex( '* ',hth)-1) hth , cast(substring(hth , charindex( '* ',hth)+1 , len(hth)) as int) hth_cs, gcts,dkje,dwdm,bgsj from tb where month(bgsj) = @month and charindex( '* ',hth)> 0
) a,
(
select hth , max(hth_cs) hth_cs from
(
select left(hth , charindex( '* ',hth)-1) hth , cast(substring(hth , charindex( '* ',hth)+1 , len(hth)) as int) hth_cs, gcts,dkje,dwdm,bgsj from tb where month(bgsj) = @month and charindex( '* ',hth)> 0
) t
group by hth
) b
where a.hth = b.hth and a.hth_cs = b.hth_cs
) t2
where t1.hth = t2.hth
drop table tb

/*
hth gcts dkje dwdm bgsj
-------------------- ----------- ----------- ---------- ----------
s039201 1 -3000000 039201 2007-07-25
s059101 0 -1000000 059101 2007-08-05
s062140 1 1000000 062140 2007-07-10

(所影响的行数为 3 行)
*/
[解决办法]
create table tb(hth varchar(20),gcts int,dkje int,dwdm varchar(10),bgsj datetime)
insert into tb values( 's039201 ' ,5,5000000, '039201 ', '2007.07.25 ')
insert into tb values( 's039201*01 ',5,3000000, '039201 ', '2007.07.21 ')
insert into tb values( 's039201*02 ',4,2000000, '039201 ', '2007.07.21 ')
insert into tb values( 's039201*03 ',4,2000000, '039201 ', '2007.07.25 ')
insert into tb values( 's059101 ' ,5,5000000, '059101 ', '2007.08.05 ')
insert into tb values( 's059101*01 ',5,4000000, '059101 ', '2007.07.01 ')
insert into tb values( 's059101*02 ',4,3000000, '059101 ', '2007.08.05 ')
insert into tb values( 's062140 ' ,5,3000000, '062140 ', '2007.07.10 ')
insert into tb values( 's062140*01 ',4,4000000, '062140 ', '2007.07.10 ')
go

declare @month as int
set @month = 8

select t1.hth , t1.gcts - t2.gcts gcts , t2.dkje - t1.dkje dkje , t1.dwdm , convert(varchar(10),t1.bgsj,120) bgsj from
tb t1,
(
select a.* from
(
select left(hth , charindex( '* ',hth)-1) hth , cast(substring(hth , charindex( '* ',hth)+1 , len(hth)) as int) hth_cs, gcts,dkje,dwdm,bgsj from tb where month(bgsj) = @month and charindex( '* ',hth)> 0
) a,
(
select hth , max(hth_cs) hth_cs from
(
select left(hth , charindex( '* ',hth)-1) hth , cast(substring(hth , charindex( '* ',hth)+1 , len(hth)) as int) hth_cs, gcts,dkje,dwdm,bgsj from tb where month(bgsj) = @month and charindex( '* ',hth)> 0


) t
group by hth
) b
where a.hth = b.hth and a.hth_cs = b.hth_cs
) t2
where t1.hth = t2.hth
drop table tb

/*
hth gcts dkje dwdm bgsj
-------------------- ----------- ----------- ---------- ----------
s059101 1 -2000000 059101 2007-08-05
(所影响的行数为 1 行)
*/

读书人网 >SQL Server

热点推荐