有这样两个表:
站点参数表AutoStationID,结构如下:
StationID,   站名数据表Rain_hour(每个站点每小时一条数据),结构如下:
StationID(与前面表对应),   雨量
平时往Rain_hour表中插入数据是这样操作的:如果某个StationID当前小时有数据并为0的话,则插入一条记录,其雨量为NULL,如果该StationID当前小时没有数据,则不插入记录。如今查询Rain_hour中遇到这样的问题:
想查询出某个StationID在某个时间段的数据,当然这其中肯定有某几个站点缺少数据的,
select 
A.StationID,
A.站名,
B.雨量,
from Station as A
left join Rain_hour as B
on A.StationID=B.StationID 
where B.观测时间='2006-06-01 08:00:00'这样查询出来的话,数据为0的与没有数据的StationID里的雨量都会显示为NULL,有没有什么办法可以区分开?比如将原本NULL的数据显示为0,缺数据的改为////
?大师帮忙啦,很急呀~~~

解决方案 »

  1.   

    这个SQL语句该怎么写?实在是没弄过,
      

  2.   

    判断是否为NULL用ISNULL(),为空则是""
      

  3.   

    create table station(stationid int,站名 varchar(10))
    insert station values(8,'AAA')
    insert station values(9,'BBB')
    select * from stationcreate table Rain_hour(StationID int,雨量 int,观测时间 datetime)
    insert Rain_hour
    select 9,null,'2006-06-01 08:00:00'
    select * from Rain_hour-- 比如将原本NULL的数据显示为0,缺数据的改为////
    -- 楼主是什么意思,不太明白
    select 
    A.StationID,
    A.站名,
    B.雨量
    from Station as A
    left join Rain_hour as B
    on A.StationID=B.StationID 
    where B.观测时间='2006-06-01 08:00:00'drop table station,Rain_hour
      

  4.   

    select 
    A.StationID,
    A.站名,
    isnull(B.雨量,0) 
    from Station as A
    left join Rain_hour as B
    on A.StationID=B.StationID 
    where B.观测时间='2006-06-01 08:00:00'
      

  5.   

    select 
    StationID,
    观测时间,
    isnull(雨量,'')
    from Rain_hour
    where StationID=30102 
    and 观测时间>'2006-04-10 15:00:00'
    and 观测时间<'2006-04-10 20:00:00'比如这样的语句,如果只有2条记录,那只能查询出2条数据来,如果其雨量为NULL,那则显示为0,这样用ISNULL(雨量,0)可以做到。
    现在想将其他2条也显示出来,并且将其雨量设置为///,如何做?
      

  6.   

    select 
    A.StationID,
    A.站名,
    (case when b.站名 is null then '////' else b.雨量 end)[雨量]
    from Station as A
    left join Rain_hour as B
    on A.StationID=B.StationID 
    where B.观测时间='2006-06-01 08:00:00'
      

  7.   

    不好意思,写错一个字段,应该为b.StationID 
    select 
    A.StationID,
    A.站名,
    (case when b.StationID is null then '////' else b.雨量 end)[雨量]
    from Station as A
    left join Rain_hour as B
    on A.StationID=B.StationID 
    where B.观测时间='2006-06-01 08:00:00'
      

  8.   

    specialsoldier(雪地撒野) 你的做法是对的,但雨量的数据类型是INT,还需改点,呵。
      

  9.   

    len(b.StationID)=0 then '////' else
      

  10.   

    不好意思,写错一个字段,应该为b.StationID 
    select 
    A.StationID,
    A.站名,
    (case when b.StationID is null then '-1' else b.雨量 end)[雨量]
    from Station as A
    left join Rain_hour as B
    on A.StationID=B.StationID 
    where B.观测时间='2006-06-01 08:00:00'是你自己说要'////'的,呵呵.
    那没有数据就写成-1,到时统计的时候判断一下,不是-1的就累加.如果你有统计总雨量需求的话.
      

  11.   

    楼上的基础上+isnull
    select 
    A.StationID,
    A.站名,
    (case when b.StationID is null then '////' else isnull(b.雨量,0) end)[雨量]
    from Station as A
    left join Rain_hour as B
    on A.StationID=B.StationID 
    where B.观测时间='2006-06-01 08:00:00'
    ==================================
    =  CSDN助手 全面支持CSDN论坛     =
    =  监视、收藏、历史、签名走马灯  =
    ==================================