简单sql字符串更正

简单sql字符串更正,sql,sqlite,Sql,Sqlite,我的sqlite3数据库中有两个表。有人能帮我使用下面的sql命令吗: tbltrans: tbltrans2: 我的第一个问题是: 我想得到一个查询表,其中包含每个transid的销售总额和计算的现金,如: Select Sum(tbltrans2.qty * tbltrans2.price) as TotalAmount, (Totalamount - tbltrans.Bank) as Cash where tbltrans.transid = tbltrans2

我的sqlite3数据库中有两个表。有人能帮我使用下面的sql命令吗:

tbltrans:

tbltrans2:

我的第一个问题是:

我想得到一个查询表,其中包含每个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语句吗

-下面这个问题已经解决了-

那么,是否有人可以更正我的sql代码以使用此表布局:

select 
    sum(price * qty - (price * qty) * (tbltrans.discountpercentage / 100)  
from 
    tbltrans2 
where 
    btwpercentage = 6) as total6 ,
  sum(price * qty - (price*qty)*(tbltrans.discountpercentage /100) from tbltrans2 where btwpercentage =12) as total12,
sum(price * qty - (price*qty)*(tbltrans.discountpercentage /100) from tbltrans2 where btwpercentage =21) as total21
 where transdate = date('10/09/2011')

您应该能够连接表并使用如下内容:

select 
  sum(case when t2.btwpercentage =6 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total6,
  sum(case when t2.btwpercentage =12 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total12,
  sum(case when t2.btwpercentage =21 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total21
from tbltrans t1
left join tbltrans2 t2
  on t1.transid = t2.transid
where transdate = date('10/09/2011')

根据您的评论,您还可以使用:

select count(t1.transid) Total,
  sum(case when t2.btwpercentage =6 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total6,
  sum(case when t2.btwpercentage =12 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total12,
  sum(case when t2.btwpercentage =21 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total21
from tbltrans t1
left join tbltrans2 t2
  on t1.transid = t2.transid
where transdate = date('10/09/2011')

如果希望查询基本上按照编写的方式工作,则需要添加from子句:

然而,BlueFoots提供了一个更简洁的版本,尽管我将选择逻辑编写为:

sum(case when t2."btwpercentage" =6
         then  t2."price"*t2."qty" * (1 - t1."discountpercentage" /100.0)
     end) Total6,

您是否收到错误消息?您需要的输出是什么?如果您不告诉我们什么是错误的以及您正在尝试完成什么,我们将无法帮助您修复它。是的,可能是因为我没有在tbltrans.discountpercentage.中指定足够的信息。。我认为这应该是类似于从tbltrans中选择折扣百分比,其中transid='12345',但我实际上不知道如何在这个语句中实现这一点。我需要得到3种不同btw类型的总和*btw=增值税或税收百分比我无法使它与我的vb应用程序一起工作。我试着用single更改双引号以使其在vb.net中工作,但仍然没有成功。这是我的文件:您可能可以删除SQL Fiddle放置的单引号/双引号。我这样做了,但仍然无法获得任何输出。是的,它在我的数据库中起作用。我是以行还是表的形式获取输出?也许这是一个愚蠢的问题,但我在编程方面的知识很低。不幸的是,我不是一个vb的人,所以我不能帮助解决这个问题。如果您的代码无法显示此查询的结果,那么我的建议是询问另一个问题。然后你将从了解该领域的人那里得到答案。
select count(t1.transid) Total,
  sum(case when t2.btwpercentage =6 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total6,
  sum(case when t2.btwpercentage =12 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total12,
  sum(case when t2.btwpercentage =21 then
        t2.price * t2.qty - (t2.price * t2.qty* t1.discountpercentage /100) end) Total21
from tbltrans t1
left join tbltrans2 t2
  on t1.transid = t2.transid
where transdate = date('10/09/2011')
Select 
sum(price * qty - (price*qty)*(tbltrans.discountpercentage /100) from tbltrans2 where btwpercentage =6) as total6 ,
 sum(price * qty - (price*qty)*(tbltrans.discountpercentage /100) from tbltrans2 where btwpercentage =12) as total12,
sum(price * qty - (price*qty)*(tbltrans.discountpercentage /100) from tbltrans2 where btwpercentage =21) as total21
from tbltrans
 where transdate = date('10/09/2011')
sum(case when t2."btwpercentage" =6
         then  t2."price"*t2."qty" * (1 - t1."discountpercentage" /100.0)
     end) Total6,