一张表中有images2,images,cph,xm,rq、flag,kh等字段,并且images2和images插入记录时只能填一个字段,我现在想通过一条sql语句把images和images2同时在一行显示出来并且要把一定时间段内满足条件的记录全部显示出来我是这样写的:select max(case when  flag='进口机' then images2 end)images2,max(case when flag='出口机' then images end)images,cph from jcjl2 where datediff(day,rq,getdate())>=tpbcts and images2 is not null and images is not null  group by cph但是这样写的话只能查询出一条记录,比如cph='粤C47484'的最近几天的一条记录。不会把车牌号为'粤C47484'的所有记录都统计出来,应该怎样才能全部统计出来。在线等。
其中tpbcts为图片保存天数。

解决方案 »

  1.   

    select  (case when  flag='进口机' then images2 end)images2,
            (case when flag='出口机' then images end)images,cph 
    from jcjl2 where datediff(day,rq,getdate())>=tpbcts 
         and images2 is not null and images is not null 
      

  2.   

    select cph,rq,max(case when  flag='进口机' then images2 end)images2,
    max(case when flag='出口机' then images end)images 
    from jcjl2 
    where datediff(day,rq,getdate())>=tpbcts and images2 is not null and images is not null  group by cph,rq
      

  3.   

     如果按一楼哪样的话也要把flag字段加进group by 子句中.这样也不行啊.不能把images 和images2放在一行啊
      

  4.   

    declare @t  table(images2 varchar(10),images varchar(10),cph int,xm varchar(10),rq datetime,
    flag varchar(10),kh varchar(10))select  (case when  flag='进口机' then images2 end)images2,
            (case when flag='出口机' then images end)images,cph 
    from @t
    where datediff(day,rq,getdate())>=1 
         and images2 is not null and images is not null /*images2    images     cph         
    ---------- ---------- ----------- (所影响的行数为 0 行)
    */
      

  5.   

    这样查出来的话要么images2为null,要么images为null,一行中都有一个为null我要的不是这个效果啊,我要的是每行都有数据.