有2个表啊: 表名:yh 用户编码 用户名称
001 a
002 b
003 c 表名:ys
用户编码 本期指数 抄表时间
001 11 2009-01-13
001 2 2009-02-26
002 3 2009-01-13
002 1 2009-02-26
两个表的通过用户编码 来关联:
希望得到下面的结构 用户编码 用户名称 本期指数
001 a 2
002 b 1
003 c 0
001 a
002 b
003 c 表名:ys
用户编码 本期指数 抄表时间
001 11 2009-01-13
001 2 2009-02-26
002 3 2009-01-13
002 1 2009-02-26
两个表的通过用户编码 来关联:
希望得到下面的结构 用户编码 用户名称 本期指数
001 a 2
002 b 1
003 c 0
解决方案 »
- 简单的问题
- 简单的查询语句优化,只要你进来就可以拿分!
- 关于排序的问题
- sql 存储过程,同时向两个表插入记录
- 求工作日计算的SQL函数,有参照
- ]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket
- 警告: 已创建表 'mytuser',但其最大行大小(28925)超过了每行的最大字节数(8060)。如果结果行长度超过 8060 字节,则此表中行的 INSE
- 疑难问题
- 查找最新记录的问题
- 如何在select触发时获取select的语句
- 请问如何把现有表的自增长的Fid 设置从1开始?
- 这条SQL如何写
FROM YH A,YS B
WHERE A.用户编码=B.用户编码
GROUP BY A.用户编码,用户名称
yh.*,
isnull(ys.本期指数,0) as 本期指数
from
yh
left join
ys
on yh.用户编码 =ys.用户编码
and not exists(select 1 from ys t where t.用户编码=ys.用户编码 and t.抄表时间>ys.抄表时间)
from yb c,(
select *
from ys a
where not exists (select 1
from ys b
where a.用户编码=b.用户编码 and a.超标时间<b.抄表时间)
)t
where c.用户编码=b.用户编码
from yb c left join (
select *
from ys a
where not exists (select 1
from ys b
where a.用户编码=b.用户编码 and a.超标时间<b.抄表时间)
)t on c.用户编码=b.用户编码
FROM yh a inner join
ys b on a.用户编码=b.用户编码
GROUP BY a.用户编码,a.用户名称
insert @yh
select '001', 'a' union all
select '002', 'b' union all
select '003', 'c'declare @ys table(用户编码 char(3),本期指数 int,抄表时间 nvarchar(12))
insert @ys
select '001',11,'2009-01-13' union all
select '001',2, '2009-02-26' union all
select '002',3, '2009-01-13' union all
select '002',1, '2009-02-26'select
a.用户编码,用户名称,本期指数=isnull(本期指数,0)
from
@yh a left join @ys b
on
a.用户编码=b.用户编码 and
b.抄表时间='2009-02-26'/*
用户编码 用户名称 本期指数
---- -------------------- -----------
001 a 2
002 b 1
003 c 0(3 行受影响)*/
FROM yh a inner join
ys b on a.用户编码=b.用户编码
GROUP BY a.用户编码,a.用户名称
;With china as
(
select * from ys A where 抄表时间= (select max(抄表时间) from ys where a.用户编号=用户编号)
)
select a.* ,isnull(b.本期指数,0) from yh a left join china b on a.用户编号=b.用户编号
create table #yh(用户编码 char(3),用户名称 nvarchar(20))
insert #yh
select '001', 'a' union all
select '002', 'b' union all
select '003', 'c'create table #ys(用户编码 char(3),本期指数 int,抄表时间 nvarchar(12))
insert #ys
select '001',11,'2009-01-13' union all
select '001',2, '2009-02-26' union all
select '002',3, '2009-01-13' union all
select '002',1, '2009-02-26'select #yh.*,
isnull(#ys.本期指数,0) as 本期指数
from #yh left join #ys on #yh.用户编码 =#ys.用户编码
and not exists(select 1
from #ys t
where t.用户编码=#ys.用户编码 and t.抄表时间>#ys.抄表时间)
用户编码 用户名称 本期指数
---- -------------------- -----------
001 a 2
002 b 1
003 c 0(所影响的行数为 3 行)
insert yh
select '001' , 'a' union all
select '002' , 'b' union all
select '003' , 'c'
create table ys (bm varchar(10),num int,times datetime)
insert ys
select '001' , 11 , '2009-01-13' union all
select '001' , 2 , '2009-02-26' union all
select '002' , 3 , '2009-01-13' union all
select '002' , 1 , '2009-02-26'
select a.bm,a.name,isnull(b.num,0) from yh a left join(
select bm,sum(num) as num from ys where times='2009-02-26 'group by bm)b on a.bm=b.bm
insert #yh
select '001', 'a' union all
select '002', 'b' union all
select '003', 'c'create table #ys(用户编码 char(3),本期指数 int,抄表时间 nvarchar(12))
insert #ys
select '001',11,'2009-01-13' union all
select '001',2, '2009-02-26' union all
select '002',3, '2009-01-13' union all
select '002',1, '2009-02-26'select a.用户编码,a.用户名称,isnull(本期指数,0)as 本期指数 from #yh a full outer join
(select 用户编码 ,本期指数 from #ys t where not exists
(select 1 from #ys where 用户编码=t.用户编码 and 抄表时间>t.抄表时间)) b
on a.用户编码=b.用户编码001 a 2
002 b 1
003 c 0
if(object_id('yh') > 0)
drop table yh
go
create table yh(
usercode nchar(3),
username nchar(1)
)
insert into yh select '001','a' union all select '002','b' union all select '003','c'
if(object_id('ys') > 0)
drop table ys
go
create table ys(
usercode nchar(3),
lennum int,
date datetime
)
insert into ys select '001','11','2009-01-31'
union all select '001',2,'2009-02-26'
union all select '002',3,'2009-01-13'
union all select '002',1,'2009-02-26'
select a.usercode,a.username,isnull(b.lennum,0) as lennum from yh a left join (select * from ys a where not exists(select 1 from ys where usercode = a.usercode and date > a.date)) b
on a.usercode = b.usercode