紧急求助!!!这个SQL语句该怎么写???(100分)

  • 主题发起人 chuanwang
  • 开始时间
C

chuanwang

Unregistered / Unconfirmed
GUEST, unregistred user!
有下面的两个表:

CommList:
CommCode CommName
01 类别01
0101 商品0101
0102 商品0102
02 类别02
0201 商品0201
0202 商品0202

Sales CommCode Amount
0101 100.00
0102 200.00
0201 300.00
0202 400.00
0101 500.00
0102 600.00
0201 700.00
0202 800.00

想得到下面的统计表:

Stats CommName Amount
合计 3600.00
类别01 1400.00
商品0101 600.00
商品0102 800.00
类别02 2200.00
商品0201 1000.00
商品0202 1200.00

请问如何设置SQL语句?
 
CommList 的表结构有问题

类别 和 商品 之间没有父子关系
 
你的统计表结构不是太合理,本来不是一类的东东不该这样组织
可以分三步实现
1,合计
insert into Stats( CommName, Amount)
select '合计' count(Amount) from CommList,Sales
where CommList.CommCode=Sales.CommCode;
2,类别合计
insert into Stats( CommName, Amount)
select CommList.CommName,count(Sales.Amount) from CommList,Sales
where CommList.CommCode=substr(Sales.CommCode,1,2)
group by CommList.CommName;
3,商品
insert into Stats( CommName, Amount)
select CommList.CommName,Sales.Amount from CommList,Sales
where CommList.CommCode=Sales.CommCode;


 
select '合计',sum(B.Amount)
from Sales B
union
select A.CommName,sum(B.Amount)
from CommList A, Sales B
where A.CommCode = substr(B.CommCode,1,2)
and length(A.CommCode) = 2
group by A.CommName
union
select A.CommName,sum(B.Amount)
from CommList A, Sales B
where A.CommCode = B.CommCode
and length(A.CommCode) = 4
group by A.CommName

 
select '合计',sum(B.Amount)
from Sales B
union
select A.CommName,sum(B.Amount)
from CommList A, Sales B
where A.CommCode = substr(B.CommCode,1,2)
and length(A.CommCode) = 2
group by A.CommName
union
select A.CommName,sum(B.Amount)
from CommList A, Sales B
where A.CommCode = B.CommCode
and length(A.CommCode) = 4
group by A.CommName
 
劳驾各位,再给看看。
我用的是Access,各位的语句怎么不好使?
 
改表结构~~~~~~~~~~~~~~~~~~~~
 
同意,你的表结构不合理
 
顶部