如何将两个表与条件连接起来并总结起来?

时间:2014-03-10 03:10:07

标签: sql ms-access-2007

我有sql命令成功显示产品的收入报告,其中包含特定年份的增值税和非增值税总额和总额。现在我想要一份年收入显示收入的报告。我试图修改现有的sql命令,但执行时,结果不正确。那么你能帮我检查一下吗?凭借我有限的sql技能,我被困在这里好几个月。

我的表位于

之下

tbl_bill_total

bill_id | bill_total | cust_id | shown date
  1     |   500      |   12    |  6/6/12
  2     |   500      |   14    |  8/8/12
  3     |   1000     |   13    |  10/11/12
  4     |   1000     |   12    |  12/10/13
  5     |   1200     |   13    |  1/11/13
  6     |   500      |   12    |  3/11/13

tbl_vat_bill_total

vat_id  |  vat_total |  if_paid| showndate | cust_id 
  1     |    100     |   false |  1/6/12   |  10
  2     |    200     |   true  |  2/6/12   |  11
  3     |    100     |   true  |  7/8/12   |  12
  1     |    400     |   false |  13/10/13 |  14
  2     |    500     |   true  |  14/11/13 |  12
  3     |    100     |   false |  15/11/13 |  11
  4     |    200     |   true  |  20/11/13 |  12

预期结果应该是这样的

bill_total  |  vat_total | Sum_of_all | month  | year
  500       |   200      |   700      |   6    |  2012
  1000      |   100      |   1100     |   8    |  2012
  1000      |   0        |   1000     |  11    |  2012       
  1000      |    0       |   1000     |  10    |  2013
  1700      |    700     |   2400     |  11    |  2013

这是我的sql命令无效...

select 
    sum(tbt.bill_total) as bill_total_
    ,iif(sum_vat_total is null, 0, sum_vat_total) as vat_total_
    ,iif(sum_vat_total is null, 0, sum_vat_total) + sum(bill_total) as sum_of_all
    ,month(showndate) as month
    ,year(showndate) as yearall
from tbl_bill tbt
left join (
    select 
        sum(vat_total) as sum_vat_total
        ,month(showndate) as month
        ,year(showndate) as yearall
    from tbl_vat
    where if_paid = true

    group by 
        month(showndate)
        ,year(showndate)
    ) tvt
        on tvt.month = month(tbt.showndate) 

group by 
    month(showndate)
    ,sum_vat_total
    ,year(showndate)

2 个答案:

答案 0 :(得分:1)

所以这是解决您问题的方法。这个SQL应该在MS Access中工作,但是我用SQL Server测试它,因为没有Access可用:(。If_Paid在我的版本中是varchar,所以你应该删除引号。

select 
sum(bill_total) as bill_total_, 
sum(vat_total) as vat_total_,
sum(bill_total) + sum(vat_total) as sum_of_all,
month, yearall
from 
  (select 
  bill_total, 0 as vat_total, month(showndate) as month, year(showndate) as yearall
  from tbl_bill_total
  union all
  select 
  0 as bill_total, vat_total, month(showndate) as month, year(showndate) as yearall
  from tbl_vat_bill_total where if_paid='true') data
group by month, yearall

在内部选择中,我会收集所有bill_total和所有vat_total值。这里没有分组。这种情况发生在外部选择monthyearall

有了这个,我得到了你希望的结果,除了2012/08我觉得你输了一个错字bill_total应该在这里500。

这是构建测试数据的SQL:

CREATE TABLE tbl_bill_total
    (bill_id int, bill_total int, cust_id int, showndate datetime)
;

INSERT INTO tbl_bill_total
    (bill_id, bill_total, cust_id, showndate)
VALUES
    (1, 500, 12, '2012-06-06 02:00:00'),
    (2, 500, 14, '2012-08-08 02:00:00'),
    (3, 1000, 13, '2012-10-11 02:00:00'),
    (4, 1000, 12, '2013-12-10 01:00:00'),
    (5, 1200, 13, '2013-01-11 01:00:00'),
    (6, 500, 12, '2013-03-11 01:00:00')
;



CREATE TABLE tbl_vat_bill_total
    (vat_id int, vat_total int, if_paid varchar(5), showndate datetime, cust_id int)
;

INSERT INTO tbl_vat_bill_total
    (vat_id, vat_total, if_paid, showndate, cust_id)
VALUES
    (1, 100, 'false', '2012-01-06 01:00:00', 10),
    (2, 200, 'true', '2012-02-06 01:00:00', 11),
    (3, 100, 'true', '2012-07-08 02:00:00', 12),
    (1, 400, 'false', '2013-13-10 01:00:00', 14),
    (2, 500, 'true', '2013-14-11 01:00:00', 12),
    (3, 100, 'false', '2013-15-11 01:00:00', 11),
    (4, 200, 'true', '2013-20-11 02:00:00', 12)
;

答案 1 :(得分:0)

请尝试以下方法

select b.bill_total, v.vat_total, b.bill_total + v.vat_total as 'sum_of_all', month(v.showndate) as 'month', year(v.showndate) as 'Year'
from tbl_vat_bill_total v join tbl_bill_total b
on b.cust_id = v.cust_id
where v.if_paid=true