#mysql查询以乘以结果字段

42fyovps  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(324)
SELECT *,   
           CASE 
             WHEN(max(`priod_id`) > 4) THEN 
               "0.2" 
             ELSE 
               "0.25" 
           END AS PERCENTAGE,   
           COUNT(priod_id) as HOURS,
           COUNT(CASE 
                   WHEN nota_id = '1' THEN 
                     1 
                 END) AS PR
      FROM `attendance_tbl` A 
     WHERE DATE(attend_date) BETWEEN DATE('2018-08-01') AND DATE('2018-08-31') 
       AND sid = '60' 
  GROUP BY DATE(attend_date)

我们需要将百分比和pr(percentage*pr)相乘为总数
请告诉我们解决问题的方法。

jjhzyzn0

jjhzyzn01#

一种方法是从现有的 SELECT 查询的结果集。之后,您可以在一个外部的 SELECT 条款 FROM 派生表。请尝试以下操作:

SELECT inner_nest.*, 
       inner_nest.PERCENTAGE * inner_nest.PR AS TOTAL 
FROM
(
  SELECT *,   
         CASE 
           WHEN(max(`priod_id`) > 4) THEN 
             "0.2" 
           ELSE 
             "0.25" 
         END AS PERCENTAGE,   
         COUNT(priod_id) as HOURS,
         COUNT(CASE 
                 WHEN nota_id = '1' THEN 
                   1 
               END) AS PR
  FROM `attendance_tbl` A 
  WHERE DATE(attend_date) BETWEEN DATE('2018-08-01') AND DATE('2018-08-31') 
    AND sid = '60' 
  GROUP BY DATE(attend_date)
) AS inner_nest

相关问题