SQL分组查询最大值最小值 10
如下图:表结构和数据期望展示效果:Bshi中‘00’表示成功的记录,其它表示失败的记录,testMax列展示该ID和ZID下成功的记录中最大值,testMin列展示该ID...
如下图:表结构和数据
期望展示效果:Bshi 中‘00’表示成功的记录,其它表示失败的记录,testMax 列展示该ID和ZID下成功的记录中最大值,testMin 列展示该ID和ZID下成功的记录中最小值,success表示该ID和ZID下成功的记录总数,fail表示该ID和ZID下失败的记录总数 展开
期望展示效果:Bshi 中‘00’表示成功的记录,其它表示失败的记录,testMax 列展示该ID和ZID下成功的记录中最大值,testMin 列展示该ID和ZID下成功的记录中最小值,success表示该ID和ZID下成功的记录总数,fail表示该ID和ZID下失败的记录总数 展开
4个回答
展开全部
查询语句参考如下:
select
UserName,--
Convert(char(8),dealTm,112) as date,--日期
min(dealTm) as earliestTime ,--最大
max(dealTm) as latestTime--最小
from t2
group by Convert(char(8),dealTm,112) ,UserName
select
UserName,--
Convert(char(8),dealTm,112) as date,--日期
min(dealTm) as earliestTime ,--最大
max(dealTm) as latestTime--最小
from t2
group by Convert(char(8),dealTm,112) ,UserName
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
--语句如下:
select ID,ZID,count(CASE Bshi when '00' then 1 else 0 end) as success,count(CASE Bshi when '00' then 0 else 1 end) as fail,max(Qzhi) as testMax,min(Qzhi) as testMin from 表名 GROUP BY ID,ZID
追问
分组是对的,成功总数,失败总数,最大值和最小值都不对
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在没有好的办法前,我用了临时表
select ID, ZID, bshi, count(Bshi) success, max(cast(Qzhi as int)) testMax, min(cast(Qzhi as int)) testMin
into #tempsuccess
from zhidao
where Bshi = '00'
group by ID, ZID, Bshi
select ID, ZID, count(Bshi) fail
into #tempfail
from zhidao
where Bshi <> '00'
group by ID, ZID
select distinct ID, ZID
into #tempid
from zhidao
select A.ID,A.ZID, isnull(B.success,0), isnull(C.fail,0), B.testmax, B.testmin from #tempid A
full outer join #tempsuccess B on A.ID = B.ID and A.ZID = B.ZID
full outer join #tempfail C on A.ID = C.ID and A.ZID = C.ZID
select ID, ZID, bshi, count(Bshi) success, max(cast(Qzhi as int)) testMax, min(cast(Qzhi as int)) testMin
into #tempsuccess
from zhidao
where Bshi = '00'
group by ID, ZID, Bshi
select ID, ZID, count(Bshi) fail
into #tempfail
from zhidao
where Bshi <> '00'
group by ID, ZID
select distinct ID, ZID
into #tempid
from zhidao
select A.ID,A.ZID, isnull(B.success,0), isnull(C.fail,0), B.testmax, B.testmin from #tempid A
full outer join #tempsuccess B on A.ID = B.ID and A.ZID = B.ZID
full outer join #tempfail C on A.ID = C.ID and A.ZID = C.ZID
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select ID,ZID,count(CASE Bshi when '00' then 1 else 0 end) as success,count(CASE Bshi when '00' then 0 else 1 end) as fail,max(CASE Bshi when '00' then CaQzhi else 0 end) as testMax,min(CASE Bshi when '00' then CaQzhi else CaQzhi end) as testMin from 表名 GROUP BY ID,ZID
试试
试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询