【问题标题】:SQL Server Query retrieves incorrect valueSQL Server 查询检索到不正确的值
【发布时间】:2018-12-05 17:31:41
【问题描述】:

下面是交易项目表。

现在我想检索交易总额、折扣总额、折扣总额和销售税,我为此编写了以下查询:

select t1.av_transaction_id,
    round(SUM((t1.gross_line_amount - t1.pos_discount_amount)), 2) AS total_amount_with_discount,
    round(SUM((t1.gross_line_amount)), 2) AS total_amount,
    round(SUM((t1.pos_discount_amount)), 2)    AS total_discount,
    round(SUM((t2.gross_line_amount)), 2)    AS total_sales_tax
from transaction_detail as t1 
inner join transaction_detail as t2 on     t1.av_transaction_id=t2.av_transaction_id
    and t1.transaction_date=t2.transaction_date
where (t1.sku_id is not null or t1.line_action_display_descr='sold')
    and t2.line_object_description='S6 Sales Tax'
    and t1.av_transaction_id='581280193'
group by t1.av_transaction_id

但我得到以下输出:

av_transaction_id:581280193 || total_amount_with_discount:5.01 || total_amount:6.67 || total_discount:1.66 || total_Sales_tax:0.66 

正如您在屏幕截图中看到的,销售税应该是 0.22,但不知何故,查询返回 0.66。

有人可以帮我优化这个查询并告诉我为什么它返回一个不正确的值吗?

【问题讨论】:

  • 好吧,我需要为多个事务运行此查询。为了简单起见,我只用一个交易 ID 发布了它
  • 嗯,这次交易有 3 条记录,其中 sku_id 不为空或显示描述为“已售出”。所有这三个都将与连接的销售税记录相匹配...然后您获取 SUM()。所以 .22 + .22 + .22 = .66
  • 你为什么要在这里加入?这是对相同数据的自联接。这里似乎有些不对劲。
  • @JoelCoehoorn 有道理,您知道如何检索销售税吗?我需要为多个事务运行此查询。
  • 问得好问题,预期结果和问题描述清晰。投票赞成。

标签: sql sql-server


【解决方案1】:

此交易有 3 条记录,其中 sku_id 不为空或显示描述为“已售出”。所有这三个都将与连接的销售税记录相匹配...然后您获取 SUM()。所以 .22 + .22 + .22 = .66

为了解决这个问题,我会使用条件聚合而不是自联接:

select t1.av_transaction_id,
    round(SUM(CASE WHEN t1.sku_id is not null or t1.line_action_display_descr='sold' THEN t1.gross_line_amount - t1.pos_discount_amount ELSE 0 END)), 2) AS total_amount_with_discount,
    round(SUM(CASE WHEN t1.sku_id is not null or t1.line_action_display_descr='sold' THEN t1.gross_line_amount ELSE 0 END), 2) AS total_amount,
    round(SUM(CASE WHEN t1.sku_id is not null or t1.line_action_display_descr='sold' THEN t1.pos_discount_amount ELSE 0 END), 2)    AS total_discount,
    round(SUM(CASE WHEN t1.line_object_description='S6 Sales Tax' THEN t1.gross_line_amount ELSE 0 END), 2)    AS total_sales_tax
from transaction_detail as t1 
where  t1.av_transaction_id='581280193'
group by t1.av_transaction_id

虽然这会重复相同的条件,但您可以进一步将其包装起来,只解决一次条件。

【讨论】:

  • 这行得通,但不知何故,结果乘以 10,所以我得到的不是 0.22,而是 2.2。不过谢谢。
  • @user9230890 事务有 10 行。您是否完全删除了连接(如本例所示),还是它仍然连接到自身?
  • 在编辑您的回复之前,您的回答中提到了 t2,因此我保留了加入,但在删除它之后,您的解决方案似乎有效。谢谢你。同时我也发布了我的解决方案。
  • @user9230890 我喜欢这个答案,因为它不会进行自我加入。投票赞成。通过对原始查询进行最小的更改,本着纠正 OP 原始查询问题的精神查看我的答案。
【解决方案2】:

感谢之前的回复,这对我有用:

select t1.av_transaction_id,
round(SUM((t1.gross_line_amount - t1.pos_discount_amount)), 2)    AS    total_amount_with_discount,
round(SUM((t1.gross_line_amount)), 2)    AS total_amount,
round(SUM((t1.pos_discount_amount)), 2) AS total_discount,
round(SUM((t2.gross_line_amount))/count(t1.gross_line_amount), 2) AS total_sales_tax
  from transaction_detail as t1 inner join
    transaction_detail as t2 on t1.av_transaction_id=t2.av_transaction_id
and t1.transaction_date=t2.transaction_date
where (t1.sku_id is not null or t1.line_action_display_descr='sold')
and t2.line_object_description='S6 Sales Tax'
and t1.av_transaction_id='581280193'
group by t1.av_transaction_id

【讨论】:

    【解决方案3】:

    我认为您需要做的就是不计算销售税,因此您的查询将如下所示:

    select t1.av_transaction_id,
        round(SUM((t1.gross_line_amount - t1.pos_discount_amount)), 2) AS total_amount_with_discount,
        round(SUM((t1.gross_line_amount)), 2) AS total_amount,
        round(SUM((t1.pos_discount_amount)), 2)    AS total_discount,
        round(t2.gross_line_amount, 2)    AS total_sales_tax
    from transaction_detail as t1 
    inner join transaction_detail as t2 on     t1.av_transaction_id=t2.av_transaction_id
        and t1.transaction_date=t2.transaction_date
    where (t1.sku_id is not null or t1.line_action_display_descr='sold')
        and t2.line_object_description='S6 Sales Tax'
        and t1.av_transaction_id='581280193'
    group by t1.av_transaction_id
    

    我用这个脚本模拟了你的桌子:

    CREATE TABLE SO_TEST
    (
        intTransID INT
        , objDesc VARCHAR(MAX)
        , displayDesc VARCHAR(MAX)
        , lintAmt FLOAT
        , discAmount FLOAT
    )
    
    INSERT INTO SO_TEST (intTransID, objDesc, displayDesc, lintAmt, discAmount)
    VALUES
    (1,'emp merch','sold',2.29,.57)
    , (1,'emp merch','sold',1.89,.47)
    , (1,'emp merch tax','sold',2.49,.62)
    , (1,'sales tax','charged',.22,0.0)
    , (1,'blah','blah',1.0,2.0)
    , (2,'emp merch','sold',3.29,1.57)
    , (2,'emp merch','sold',2.89,1.47)
    , (2,'emp merch tax','sold',3.49,1.62)
    , (2,'sales tax','charged',1.22,0.0)
    , (2,'blah','blah',1.0,2.0)
    

    并提出了这个查询,它显示了我认为您希望它显示的内容:

    SELECT t1.intTransID
    , ROUND(SUM(t1.lintAmt - t1.discAmount),2) tot_amt_disc
    , ROUND(SUM(t1.lintAmt), 2) tot_amt
    , ROUND(SUM(t1.discAmount),2) disc_amt
    , ROUND(t2.lintAmt,2) sales_tax
    FROM SO_TEST t1
    JOIN SO_TEST t2 ON 1=1
        AND t1.intTransID = t2.intTransID
    WHERE t1.displayDesc IN ('sold','charged')
        AND t1.objDesc <> 'sales tax'
        AND t2.objDesc = 'sales tax'
    GROUP BY t1.intTransID, t2.lintAmt
    

    查询结果如下:

    这适用于要求的多笔交易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2021-07-23
      • 1970-01-01
      • 2022-11-16
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多