2012-03-29 56 views
1

在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条记录汇总为每日总计。

回答

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 

UPDATE:

所以我推测, MS Query在查询中使用参数时出现问题,无法以图形方式显示。解决方法是用一些常量替换参数,将工作簿保存为XML并将常量更改为“?”。 There is a link showing VBA code which does replacement on-site,但你将不得不弄清楚它是如何工作的,因为我不知道VBA那么多。

更新2:

在另一方面最简单的办法就是创建视图在数据库中。

+0

谢谢尼古拉,但不能在Excel 2007查询中工作。标准错误 – 2012-03-29 13:55:20

+0

我试过MS Query中的case ... end语句。有用。你如何执行这个查询?什么是错误? – 2012-03-29 14:08:13

+0

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