需要访问DB查询帮助

时间:2016-01-14 16:40:03

标签: vb.net ms-access

我有这个ACCESS查询:

SELECT trans, sum(total) as tax 
FROM PURCHASE 
WHERE matType LIKE 'ad-tax' 
      and trans IN (SELECT trans FROM PURCHASE WHERE matType LIKE 'P-%' Group by trans) 
group by trans

这个查询:

SELECT trans, sum(total) as Total 
FROM PURCHASE 
WHERE matType not LIKE 'ad-tax' 
      and trans IN (SELECT trans FROM PURCHASE WHERE matType LIKE 'P-%' Group by trans)
group by trans

我必须搜索包含正确matType的交易,因为我只需要将税项作为项目

反式------ -----项------ matType总

66 ............. 1 ...........广告税......... 9.00

66 ............. 2 ........... P-944 .......... 60.00

67 ............. 1 ...........广告税......... 6.00

67 ............. 2 ........... P-903 .......... 40.00

68 ............. 1 ........... P-998 .......... 29.00

69 ............. 1 ........... P-921 .......... 10.00

等。

我想知道如何查询:Trans |总计|一份声明中的税。它必须来自两个日期之间,但我可以自己解决这个问题。我从vb.net查询是否有所作为。

1 个答案:

答案 0 :(得分:1)

也许:

SELECT 'tax', trans, sum(total) as tax FROM PURCHASE 
    WHERE matType LIKE 'ad-tax' and trans 
    IN (SELECT trans FROM PURCHASE WHERE matType LIKE 'P-%' Group by trans) 
    group by trans

UNION ALL

SELECT 'total', trans, sum(total) as Total FROM PURCHASE 
WHERE matType not LIKE 'ad-tax' and trans IN (SELECT trans 
FROM PURCHASE WHERE matType LIKE 'P-%' Group by trans)
 group by trans

我这里没有你的桌子所以我不能更好地尝试它

补充:这不能按你的意愿运作,所以尝试类似的东西:

SELECT * FROM 
( SELECT trans, sum(total) as tax FROM PURCHASE WHERE matType LIKE 'ad-tax' and trans IN (SELECT trans FROM PURCHASE WHERE matType LIKE 'P-%' Group by trans) group by trans ) [FIRST] 
JOIN 
( SELECT trans, sum(total) as Tot FROM PURCHASE WHERE matType not LIKE 'ad-tax' and trans IN (SELECT trans FROM PURCHASE WHERE matType LIKE 'P-%' Group by trans) group by trans ) [SECOND]
ON [FIRST].[TRANS] = [SECOND].[TRANS]