按产品分类进行销售统计的sql
2010-05-16 11:09
351 查看
if object_id('[product]') is not null drop table [product] create table [product] (id int,pid int,code varchar(4),name varchar(1)) insert into [product] select 1,0,null,'A' union all select 2,1,'01','B' union all select 3,1,'02','C' union all select 4,2,'0101','D' union all select 5,3,'0201','E' union all select 6,1,'0001','F' --> 测试数据: [sale] if object_id('[sale]') is not null drop table [sale] create table [sale] (id int,productId int,num int,price numeric(8,1)) insert into [sale] select 1,4,2,5.5 union all select 2,5,4,6.5 union all select 3,6,1,20 ;with wsp as ( select a.id,pid,name,num,totalprice=num*price from product a,sale b where a.id=b.productid union all select a.id,a.pid,a.name,num=b.num,totalprice=b.totalprice from product a,wsp b where a.id=b.pid ) select name,totalNum=sum(num),totalPrice=sum(totalprice) from wsp group by name /*结果: name totalNum totalPrice ---- ----------- --------------------------------------- A 7 57.0 B 2 11.0 C 4 26.0 D 2 11.0 E 4 26.0 F 1 20.0 */
相关文章推荐
- 大家好,我有表A中字段content,我现在按content值的前四位进行分组统计,请问,这SQL要怎样写?
- OJ中----识别有效的IP地址和掩码并进行分类统计
- 感受移动互联的力量!手机移动销售软件—纷享销客来访进行公司及产品介绍(图文)
- sql根据身份证号得到年龄进行统计
- 分类统计数量sql
- 识别有效的IP地址和掩码并进行分类统计
- sql分类汇总统计,字符串类型的区间数值
- SQL进行排序、分组、统计的10个新技巧
- SQL:根据销售点表统计销售情况
- 识别有效的IP地址和掩码并进行分类统计
- 识别有效的IP地址和掩码并进行分类统计
- Asp.net树形产品结构菜单,根据产品分类获取产品及其子类的所有产品和数量统计
- 华为机试 识别有效的ip地址和掩码并进行分类统计
- 每个渠道销售情况统计sql
- 销售统计SQL
- SQL数据进行排序、分组、统计10技巧
- sql根据多个不同字段进行分组,并统计总数,求和。tp求和,分页
- 华为Oj--识别有效的IP地址和掩码并进行分类统计
- 产品分类SQL语句
- 【华为OJ】【018-识别有效的IP地址和掩码并进行分类统计】