【问题标题】:Case, Select conditional requirments案例,选择条件要求
【发布时间】:2012-03-29 22:47:37
【问题描述】:

在 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”中),如果该字段为 Tax_Amount = -(Tax_Amount) 或 if ActualQuantity &gt;=0, then Tax_Amount = Tax_Amount。

请注意查询是“求和”的,所以我认为这个条件方面需要在求和发生之前处理。该查询将大约 100 000 条记录汇总为每日总数。

【问题讨论】:

    标签: sql excel


    【解决方案1】:
    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:

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

    【讨论】:

    • 感谢 Nikola,但在 Excel 2007 查询中不起作用。标准错误
    • 我已经在 MS Query 中尝试过 case ... end 语句。有用。你如何执行这个查询?错误是什么?
    • Nikola,我在 MS Query 中打开 SQL 对话框,并将 TAX_Amount 的 Sum 语句替换为: Sum("_bvSTTransactionsFull".Credit) AS 'TOTALCredit', Sum(case when "_bvSTTransactionsFull"。 ActualQuantity >= 0 then "_bvSTTransactionsFull".tax_amount else -"_bvSTTransactionsFull".tax_amount end) AS TOTALtax_Amount',错误是“Parameters are not allowed in queries that cannot be displayed in graphics”
    • 感谢 Nikola 的努力,我会继续寻找。我只有对数据库的读取权限,它是我们的会计系统,以高昂的美元费用购买现成的:-(
    • 如果您可以访问服务器,您可以在那里创建另一个数据库并将视图放在那里。如果没有,there is option to use linked servers.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多