简体   繁体   English

SSRS 从总和列中的列表中减去一个负数

[英]SSRS subtract a minus number from list in a sum column

I have an SSRS dataset query that has a SUM on a column of values, which represent decimal time of a persons absence, which the admin staff enter every time an absence is recorded for a student.我有一个 SSRS 数据集查询,它在一列值上有一个 SUM,它表示一个人缺勤的十进制时间,每次为学生记录缺勤时,管理人员都会输入该值。 In the uni database the staff seem to add absence records that also show hours that should be removed from a record, and they do this by simply putting a minus in front of the number.在 uni 数据库中,工作人员似乎添加了缺勤记录,这些记录也显示了应该从记录中删除的小时数,他们只需在数字前面加上一个减号就可以做到这一点。 Is it possible to get this column to recognise the minus as a subtraction and remove it from the total?是否可以让此列将减号识别为减号并将其从总数中删除? You can see at the bottom of the list a record was entered for 187.50 hours absent, but then another record added for that same absence period which noted that 150.00 of those hours should be subtracted (they fulfilled the shortfall in placement hours another way).您可以在列表底部看到一条记录缺席 187.50 小时,但随后为同一缺勤期添加了另一条记录,其中指出应减去其中的 150.00 小时(他们以另一种方式填补了安置小时数的不足)。 Hope I've explained that query, have attached a screenshot of the dataset query.希望我已经解释了该查询,并附上了数据集查询的屏幕截图。 I'm not sure if I added the screenshot correctly.我不确定我是否正确添加了屏幕截图。 Thanks.谢谢。

在此处输入图片说明

I think you can just add a CASE statement to check for the minus sign in the SUM and use 0 if it is a negative number.我认为您可以添加一个 CASE 语句来检查 SUM 中的减号,如果它是负数,则使用 0。

CAST(dbo.srs_enl.enl_udfb AS DECIMAL(27, 2))

would become会成为

CAST(CASE WHEN LEFT(dbo.srs_enl.enl_udfb, 1) = '-' THEN 0 ELSE dbo.srs_enl.enl_udfb END AS DECIMAL(27, 2))

or you could use Boolean logic, multiplying the value by 0 if it's less than 0:或者您可以使用布尔逻辑,如果小于 0,则将值乘以 0:

CAST(dbo.srs_enl.enl_udfb AS DECIMAL(27, 2)) * CASE WHEN CAST(dbo.srs_enl.enl_udfb AS INT) < 0 THEN 0 ELSE 1 END

I assume you would want to see the final two records appearing as a single record with 37.50 in the final column?我假设您希望看到最后两条记录在最后一列中显示为 37.50 的单个记录? If that's correct then it looks like your grouping by too many columns.如果这是正确的,那么看起来您的分组列太多了。

You seem to be grouping by你似乎在分组

  • sab_stuc - OK sab_stuc - 好的
  • sab_begd - OK sab_begd - 好的
  • sab_endf - OK sab_endf - 好的
  • enl_eltc - does not appear in output, can probably be removed from GROUP BY clause enl_eltc - 没有出现在输出中,可能可以从GROUP BY子句中删除
  • enl_udfd - does not appear in output, can probably be removed from GROUP BY clause enl_udfd - 没有出现在输出中,可能可以从GROUP BY子句中删除
  • enl_ufdb - the value being summed so should be removed from GROUP BY clause enl_ufdb - 求和的值应该从GROUP BY子句中删除

If you remove the 3 columns from the group by clause the sum should 'just work' as expected.如果您从 group by 子句中删除 3 列,则总和应该按预期“正常工作”。


One more thing... Try to alias table names, it will make you SQL much easier to read.还有一件事...尝试为表名设置别名,这将使您的 SQL 更易于阅读。

So something like所以像

SELECT 
      dbo.TableA.columnA, dbo.TableB.columnB
      FROM dbo.TableA
          JOIN dbo.TableB on dbo.TableA.myKeyColumn = dbo.TableB.myKeyColumn
      ORDER BY dbo.TableA.columnA

using aliases would look like this使用别名看起来像这样

SELECT 
      a.columnA, b.columnB
      FROM dbo.TableA a
          JOIN dbo.TableB b on a.myKeyColumn = b.myKeyColumn
      ORDER BY a.columnA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM