易截截图软件、单文件、免安装、纯绿色、仅160KB

sql语句的问题 - MS-SQL Server / 基础类

有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
SQL code:
SELECT A.用户编码,用户名称, 本期指数 =COUNT(*)
from YH A,YS B
WHERE A.用户编码=B.用户编码
GROUP BY A.用户编码,用户名称


SQL code:
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.抄表时间)


select c.*,t.抄表时间
from yb c,(
select *
from ys a
where not exists (select 1 
from ys b
where a.用户编码=b.用户编码 and a.超标时间<b.抄表时


相关问答:

sql更新字段路径

如果用sql语句更改lj字段的路径,如下:
lj
7/200781616183095962.jar
8/f_200812111413292.jar
8/f_200812111413291.jar

得到的结果如下:
lj
1/7/200781616183095962.jar
1/8/f_20081 ......

sql 截取字符串

我要得到一个字符串如:
sdfk|||sgts
sdfsfd|||rgreg
wrfw|||sefw
就是要得到|||后面的字符串,有什么函数吗?怎么用呢?谢谢!
SQL code:
select
right(col,len(col)-charindex('|||',col)-2)
f ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号