2012-03-29 5 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는 0이 수 있지만 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를 많이 모르기 때문에 어떻게 작동하는지 파악해야합니다.

업데이트 2 : 반면에

밖으로 쉬운 방법은 데이터베이스에서보기를 만드는 것입니다.

+0

감사합니다 Nikola,하지만 Excel 2007 쿼리에서 작동하지 않습니다. 표준 오류 –

+0

나는 MS Query에서 case ... end 문을 시도했습니다. 그것은 작동합니다. 이 쿼리를 어떻게 실행합니까? 그리고 그 오류는 무엇입니까? –

+0

Nikola, MS Query에서 SQL 대화 상자를 열고 TAX_Amount의 Sum 문을 다음과 같이 바꿉니다. 합계 ("_bvSTTransactionsFull". 신용) AS 'TOTALCredit', 합계 ("_bvSTTransactionsFull".ActualQuantity> = 0 인 경우) 또 "_bvSTTransactionsFull".tax_amount - "_ bvSTTransactionsFull"TOTALTax_Amount AS .tax_amount 말) ', 오류가 이 –