我需要一些帮助。
我想得到每笔销售的总成本:
select * from (select salesid, sum(price * qty) as netsales, sum(qty * (select cost from tblproducts)) as totalcost from tblsales_details group by salesid) a inner join tblsales b on b.salesid = a.salesid
我有3个表,分别是tblsales,tblsales_details,tblproducts
我正在使用MS Access。
在tblsales列上是:salesid
在tblsales_details列上有:salesid,productid,price,qty
在tblproducts列上有:productid,cost
我已经有了这个可以运行的sql代码,但我无法获得总费用
sum(qty * (select cost from tblproducts))
a.salesid - netsales - totalcost
1 - 1800 - ?
我知道它不起作用,但我想做这样的事情:
sum(qty * (select cost from tblproducts where pid = pid))
答案 0 :(得分:0)
我相信你想要以下
select salesid,
sum(d.qty * d.price) as netsales,
sum(d.qty * p.cost) as totalcost
from tblsales_details as d
join tblproducts as p on d.pid = p.pid
group by salesid
答案 1 :(得分:0)
使用以下代码进行游戏,您将看到它们如何挂在一起
--try replacing
--SELECT *
--instead of the below select statement and you will see how the SUM part, FROM part
--and the GROUP BY work together
SELECT
s.salesid
,SUM(d.qty * d.price) as netsales
,SUM(d.qty * p.cost) as totalcost
FROM
tblsales AS s
INNER JOIN tblsales_details AS d ON s.salesid = d.salesid
INNER JOIN tblproducts AS p ON d.productid = p.productid
GROUP BY s.salesid