数据库--查询--按小时查询&在where里面使用sum函数&同时显示多个查询条件下的数据...

查询:将数据库-表中-若干日期-按一天24小时,将所有日期同一时间的数据求和。

学习点:将数据按小时分组求和

1 declare @BgainTime varchar(50)='2017-11-01'  
2 declare @EndTime varchar(50)  ='2017-12-01'
3 --每小时发药处方量
4 select '时间'=datepart(hh,ReceiveTime),'发药量'=count(*) from Prescription 
5 where ReceiveTime between  @BgainTime and @EndTime
6 group by datepart(hh,ReceiveTime)
7 order by datepart(hh,ReceiveTime)

如果想在where条件使用sum函数,通过having来实现:

select  PrescCode from PrescriptionDetail group by PrescCode having sum(OutQuantity)>=sum(Quantity)

 存在要将不同查询条件下的多个数据同时显示,使用相关子查询,下面代码做参考:

--总处理处方数量  总发药盒数  总补药盒数
select '总处理处方数量'=count(*) ,
'总发药盒数'=(select sum(OutQuantity/ConversionRate) from PrescriptionDetail  
inner join DrugInfo on DrugInfo.DrugCode=PrescriptionDetail.DrugCode
inner join Prescription on Prescription.PrescCode=PrescriptionDetail.PrescCode
where ReceiveTime between @BgainTime and @EndTime),
'总补药盒数'=(select sum(Quantity) from ReplenishAuditInfo  
where OperateTime between @BgainTime and @EndTime )
from Prescription where ReceiveTime between @BgainTime and @EndTime

备注:此查询语句将三种不同查询条件下的数据同时显示,可以来自不同表

 

转载于:https://www.cnblogs.com/EasonDongH/p/8023240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值