在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
我需要选择查询看一场名为“ActualQuantity
”(表中的“_bvSTTransactionsFull
”),如果该字段为< 0,然后Tax_Amount = -(Tax_Amount)
,或if ActualQuantity >=0, then Tax_Amount = Tax_Amount
。
请注意查询是“求和”的,所以我认为这个条件方面需要在求和之前进行处理。查询将大约100 000条记录汇总为每日总计。
谢谢尼古拉,但不能在Excel 2007查询中工作。标准错误 – 2012-03-29 13:55:20
我试过MS Query中的case ... end语句。有用。你如何执行这个查询?什么是错误? – 2012-03-29 14:08:13
Nikola,我在MS Query中打开SQL对话框,并将TAX_Amount的Sum语句替换为: Sum(“_ bvSTTransactionsFull”.Credit)AS'TOTALCredit',Sum(“_bvSTTransactionsFull”.ActualQuantity> = 0时的情况“_bvSTTransactionsFull” .tax_amount其他 - “_ bvSTTransactionsFull” .tax_amount结束)AS TOTALTax_Amount”, 的错误是 – 2012-03-29 14:28:06