SQL查询数学

时间:2017-12-13 11:57:04

标签: sql sql-server tsql

正如您在图片中看到的那样,我正在从事员工薪酬支付项目。

但我有一个关于TOTAL的问题。我想这样做:(HOURS*perHOUR)+(SALES/100)*COMMISSION = TOTAL

enter image description here

SELECT DISTINCT dbo.TBL_NEWDAY.NAME,
       SUM(CAST(dbo.TBL_NEWDAY.HOURS AS FLOAT)) AS HOURS,
       SUM(CAST(dbo.TBL_NEWDAY.SALES AS FLOAT)) AS SALES,
       AVG(CAST(dbo.TBL_NEWEMPLOYEE.NEPHOUR AS FLOAT)) AS PHOUR,
       AVG(CAST(dbo.TBL_NEWEMPLOYEE.NECOMISSION AS FLOAT)) AS COMISSION, 
       SUM(CAST(dbo.TBL_NEWDAY.HOURS AS FLOAT)) * 
       SUM(CAST(dbo.TBL_NEWEMPLOYEE.NEPHOUR AS FLOAT)) + 
       SUM(CAST(dbo.TBL_NEWDAY.SALES AS FLOAT)) / 100 * 
       SUM(CAST(dbo.TBL_NEWEMPLOYEE.NECOMISSION AS FLOAT)) AS TOTAL
FROM dbo.TBL_NEWDAY INNER JOIN
     dbo.TBL_NEWEMPLOYEE
     ON dbo.TBL_NEWDAY.NAME = dbo.TBL_NEWEMPLOYEE.NENAME
GROUP BY dbo.TBL_NEWDAY.NAME

我做了一切,只是数学计算的优先级是错误的,这就是为什么我的道达尔错了。

1 个答案:

答案 0 :(得分:2)

您将PHOUR和COMISSION列列为AVG,但在总计中使用SUM。 您的总列公式应该是这样的。

 ( SUM(CAST(dbo.TBL_NEWDAY.HOURS AS FLOAT)) *
     AVG(CAST(dbo.TBL_NEWEMPLOYEE.NEPHOUR AS FLOAT)) ) 
 + ( SUM(CAST(dbo.TBL_NEWDAY.SALES AS FLOAT)) / 100 ) 
 * AVG(CAST(dbo.TBL_NEWEMPLOYEE.NECOMISSION AS FLOAT)) AS TOTAL