sql语句不能显示完全

龙龙16 2008-04-09 04:25:56
一张表中有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为图片保存天数。
...全文
132 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙龙16 2008-04-09
  • 打赏
  • 举报
回复
这样查出来的话要么images2为null,要么images为null,一行中都有一个为null我要的不是这个效果啊,我要的是每行都有数据.
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
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 行)
*/
龙龙16 2008-04-09
  • 打赏
  • 举报
回复
如果按一楼哪样的话也要把flag字段加进group by 子句中.这样也不行啊.不能把images 和images2放在一行啊
正宗老冉 2008-04-09
  • 打赏
  • 举报
回复
一楼正解!
bqb 2008-04-09
  • 打赏
  • 举报
回复
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
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
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

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧