只需查询计算字段即可

时间:2012-12-25 18:28:31

标签: sql sqlite

我的sqlite3数据库中有2个表。有人可以帮我使用下面的sql命令:

tbltrans:

transid |  transdate | discountpercentage | Bank
12345     10/09/2011     5                  20.00

tbltrans2:

transid   |   itemnr   |price | btwpercentage | qty
12345         205        10.11        12        5
12345         302        15.00        6         7
12345         501        20.00        21        3

所以我想获得一个查询表,其中包含每个transid和计算现金列的总销售额,如:

Select
Sum(tbltrans2.qty * tbltrans2.price) as TotalAmount,
(Totalamount - tbltrans.Bank) as Cash
where 
tbltrans.transid = tbltrans2.transid and transdate = '10/09/12'

有人可以更正这个sql语句吗?

1 个答案:

答案 0 :(得分:1)

Select
Sum(ifnull((tbltrans2.qty * tbltrans2.price),0))-tbltrans.Bank as cash
from tbltrans,tbltrans2
where 
tbltrans.transid = tbltrans2.transid and tbltrans.transdate = '10/09/12'
group by tbltrans.transid 

试试这个

如果您想要选择总金额,请将其包括在内 选择,

Sum(ifnull((tbltrans2.qty * tbltrans2.price),0)) as TotalAmount