2012-03-29 52 views
1

Excelクエリで動作しているので、1つのフィールドを読み込み、その値に基づいて別のフィールドをマイナスに設定する必要があります。次のようにケース、条件付き条件を選択

私の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 

このフィールドは< 0であれば、私は、その後、(表中の「_bvSTTransactionsFull」)「ActualQuantity」という名前のフィールドを見て、選択クエリを必要と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はあまりにもあなたがTAX_AMOUNTの符号を変更するsign functionを使用することができますゼロの場合:

sign(ActualQuantity) * tax_amount 

UPDATE:

だから私はことを集めますMSクエリは、グラフィカルに表示することができないクエリのパラメータを使用する際に問題があります。回避策は、パラメータをいくつかの定数に置き換え、ワークブックをXMLとして保存し、定数を "?"に変更することです。 There is a link showing VBA code which does replacement on-siteしかし、私はVBAをそれほど知らないので、どのように動作するのか把握する必要があります。

UPDATE 2:一方

うち最も簡単な方法は、データベース内のビューを作成することです。

+0

Nikolaに感謝しますが、Excel 2007クエリーでは機能しません。標準エラー –

+0

私はMSクエリでcase ... endステートメントを試しました。できます。どのようにこのクエリを実行しますか?そして、エラーは何ですか? –

+0

Nikola、MS QueryでSQLダイアログを開き、TAX_AmountのSum文を次のように置き換えます。 合計( "_ bvSTTransactionsFull" .Credit)AS 'TOTALCredit'、Sum( "_bvSTTransactionsFull" .ActualQuantity> = 0の場合他.tax_amount「_bvSTTransactionsFull」 - TOTALTax_Amount」、 AS)「bvSTTransactionsFull _」.tax_amount最後にエラーが こんにちはブライアンは、私は私の答えを更新しました –