sqlite 查询数据,sum, 大小数量,和总价

本文介绍了一种使用SQL语言来统计特定时间段内食品销售情况的方法。主要包括创建用于存储食品价格及销售信息的数据表,并通过复杂的SQL查询来获取每种食品的销售总量、大小份的数量以及销售额等关键指标。

1.create table fooodPrice(id int  not null,foodTypeId integer primary key autoincrement,
foodName char(50) not null,foodPrice0 real not null,foodPrice1 real not null)
 ;

2. create table saledInfo(id integer primary key autoincrement,foodTypeId int not null,
num int not null,model int not null default '0',createTime datetime not null default (datetime('now','localtime')),
comments text not null default '');

3.//分别统计出大小数量,和总价
select  foodName , sum(num) , sum(case when model='0' then num else 0 end) ,sum(case when model='0' then num* s1.foodPrice0 else num* s1.foodPrice1 end) as small   from fooodPrice s1 , saledInfo s2 where s1.foodTypeId=s2.foodTypeId group by foodName;

select * from (select foodName,sum(num) as '总数量', createTime, sum(case when model='0' then num else 0 end) as '小份数量',sum(case when model='1' then num else 0 end) as '大份数量',sum(case when model='0' then num* s1.foodPrice0 else num* s1.foodPrice1 end) as '单品种总金额' from fooodPrice s1 , saledInfo s2 where s1.foodTypeId=s2.foodTypeId and createTime>'2021-07-21' and createTime<'2021-07-23' group by foodName ) as t1
 group by 单品种总金额 order by createTime desc 

select sum(单品种总金额) as '总金额' from (select foodName,sum(num) as '总数量', createTime, sum(case when model='0' then num else 0 end) as '小份数量',sum(case when model='1' then num else 0 end) as '大份数量',sum(case when model='0' then num* s1.foodPrice0 else num* s1.foodPrice1 end) as '单品种总金额' from fooodPrice s1 , saledInfo s2 where s1.foodTypeId=s2.foodTypeId and createTime>'2021-07-21' and createTime<'2021-07-23' group by foodName ) as t1
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值