在Excel查询中操作,我需要一个条件语句来读取一个字段,并根据该值将另一个字段设置为减号(或不是)。
我的SQl代码如下:
SELECT "_bvSTTransactionsFull".txdate,
SUM("_bvSTTransactionsFull".debit) AS 'TOTALDebit',
SUM("_bvSTTransactionsFull".credit) AS 'TOTALCredit',
SUM("_bvSTTransactionsFull".tax_amount) AS 'TOTALTax_Amount',
SUM("_bvSTTransactionsFull".VALUE) AS 'TOTALValue',
SUM("_bvSTTransactionsFull".actualvalue) AS 'TOTALActualValue',
SUM("_bvSTTransactionsFull".actualsalesvalue) AS 'TOTALActualSalesValue',
SUM("_bvSTTransactionsFull".profit) AS 'TOTALProfit'
FROM sqlschema.dbo."_bvSTTransactionsFull" "_bvSTTransactionsFull"
WHERE ( "_bvSTTransactionsFull".txdate >=?
AND "_bvSTTransactionsFull".txdate <=? )
GROUP BY "_bvSTTransactionsFull".txdate,
"_bvSTTransactionsFull".description
HAVING ( "_bvSTTransactionsFull".description LIKE 'POS Sale' )
OR ( "_bvSTTransactionsFull".description LIKE 'POS Return' )
ORDER BY "_bvSTTransactionsFull".txdate
我需要select查询来查看名为“ActualQuantity
”的字段(在表“_bvSTTransactionsFull
”中),如果此字段为&lt; 0,则为Tax_Amount = -(Tax_Amount)
,或者if ActualQuantity >=0, then Tax_Amount = Tax_Amount
。
请注意查询是“求和”的,所以我假设需要在求和之前处理这个条件方面。该查询将大约100 000条记录汇总为每日总计。
答案 0 :(得分:0)
SELECT "_bvSTTransactionsFull".txdate,
SUM("_bvSTTransactionsFull".debit) AS 'TOTALDebit',
SUM("_bvSTTransactionsFull".credit) AS 'TOTALCredit',
SUM(case when "_bvSTTransactionsFull".ActualQuantity >= 0
then "_bvSTTransactionsFull".tax_amount
else - "_bvSTTransactionsFull".tax_amount
end) AS 'TOTALTax_Amount',
SUM("_bvSTTransactionsFull".VALUE) AS 'TOTALValue',
SUM("_bvSTTransactionsFull".actualvalue) AS 'TOTALActualValue',
SUM("_bvSTTransactionsFull".actualsalesvalue) AS 'TOTALActualSalesValue',
SUM("_bvSTTransactionsFull".profit) AS 'TOTALProfit'
FROM sqlschema.dbo."_bvSTTransactionsFull" "_bvSTTransactionsFull"
WHERE ( "_bvSTTransactionsFull".txdate >=?
AND "_bvSTTransactionsFull".txdate <=? )
GROUP BY "_bvSTTransactionsFull".txdate,
"_bvSTTransactionsFull".description
HAVING ( "_bvSTTransactionsFull".description LIKE 'POS Sale' )
OR ( "_bvSTTransactionsFull".description LIKE 'POS Return' )
ORDER BY "_bvSTTransactionsFull".txdate
如果ActualQuantity可能为零,但同一行中的taxAmount也为零,则可以使用sign function更改tax_amount的符号:
sign(ActualQuantity) * tax_amount
<强>更新强>
因此我认为MS Query在查询中使用无法以图形方式显示的参数时出现问题。解决方法是用一些常量替换参数,将工作簿另存为XML并将常量更改为“?”。 There is a link showing VBA code which does replacement on-site,但你必须弄清楚它是如何工作的,因为我不太了解VBA。
更新2:
另一方面,最简单的方法是在数据库中创建视图。