0

我有一个用于从 SQL 数据库导出数据的 C# 应用程序。它从视图中提取数据并将其导出到 Excel 以导入我们的工资系统。我需要计算加班费。我有以下代码,但它不计算加班量。我已经在互联网上搜索了解决方案,但没有任何运气。这是我得到的输出

SELECT  [Co Code]
    , [Empl No]
    , [Task CD]
    , [Day No]
    , [Fund Co Code]
    , [Job No]
    , [Equip Cost Cde]
    , SUM([Reg Hrs Jc]) AS [Reg Hrs Jc]
    ,(CASE WHEN [Reg Hrs Jc] > 40 THEN [Reg Hrs Jc] - 40 ELSE 0 END) AS [Ot Hrs Jc]
FROM         dbo.MIS_FTTIMECARD_OT
WHERE     ([Date Worked] BETWEEN '8/11/2013' AND '8/17/2013')
GROUP BY [Co Code]
    , [Empl No]
    , [Task CD]
    , [Day No]
    , [Fund Co Code]
    , [Job No]
    , [Equip Cost Cde]
    , (CASE WHEN [Reg Hrs Jc] > 40 THEN [Reg Hrs Jc] - 40 ELSE 0 END)
4

2 回答 2

0

为了扩展其他人的答案,您的命名约定可能是导致混淆的原因。

考虑以下代码片段:

, SUM([Reg Hrs Jc]) AS [Reg Hrs Jc]
,(CASE WHEN [Reg Hrs Jc] > 40 THEN [Reg Hrs Jc] - 40 ELSE 0 END) AS [Ot Hrs Jc]

通过重命名,很清楚发生了什么:

, SUM([Reg Hrs Jc]) AS [Reg Hrs Jc Sum]
,(CASE WHEN [Reg Hrs Jc] > 40 THEN [Reg Hrs Jc] - 40 ELSE 0 END) AS [Ot Hrs Jc]

我认为您已经有效地尝试执行以下操作,如果它不共享实际的列名,这将不起作用

, SUM([Reg Hrs Jc]) AS [Reg Hrs Jc Sum]
,(CASE WHEN [Reg Hrs Jc Sum] > 40 THEN [Reg Hrs Jc Sum] - 40 ELSE 0 END) AS [Ot Hrs Jc]

您不能为列命名,然后在同一范围内通过该新标题引用它,因此您应该按照其他人的建议将 Ot Hrs Jc 基于 SUM([Reg Hrs Jc]) 。

于 2013-08-21T12:43:59.603 回答
0

您应该使用子查询,这将允许您首先总结工作时间:

SELECT t.*, (CASE WHEN [Reg Hrs Jc] > 40 THEN [Reg Hrs Jc] - 40 ELSE 0 END) AS [Ot Hrs Jc]
FROM 
   (SELECT [Co Code], [Empl No], [Task CD], [Day No], [Fund Co Code], [Job No], [Equip Cost Cde], SUM([Reg Hrs Jc]) AS [Reg Hrs Jc] FROM dbo.MIS_FTTIMECARD_OT
   WHERE ([Date Worked] BETWEEN '8/11/2013' AND '8/17/2013')
   GROUP BY [Co Code], [Empl No], [Task CD], [Day No], [Fund Co Code], [Job No], [Equip Cost Cde]) AS t
于 2013-08-21T12:39:16.193 回答