mysql如何在同一列中对某些值进行乘法运算,但如果满足某个条件,则不能对其他值进行乘法运算

7vux5j2d  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(619)

如果某个值满足某个条件,是否可以在同一列中对某些值进行多重化,而不能对其他值进行多重化?我不想再创建一个列。
我正在处理的查询:

SELECT
    name ,
    ROUND(SUM(orderline_sales.amount * orderline_sales.price) * orders_sales.discount * customers.annual_discount) AS total_revenue
FROM
    orderline_sales 
        JOIN
    orders_sales  ON orders_sales.id = orderline_sales.orders_sales_id
        JOIN
    employee ON orders_sales.empoyee_id = employee.id
        JOIN 
    customers ON orders_sales.customer_id = customers.id
WHERE
    date BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 365 DAY) AND CURRENT_DATE
GROUP BY employee.name
ORDER BY totale_omzet DESC
LIMIT 1;

orders\u sales表包含日期属性类型,orders\u sales表与orderline\u sales具有1:n基数关系。我只想将总和结果与客户相乘。如果订单年份高于2017年,则为年折扣。我该怎么做呢?

5n0oy7gb

5n0oy7gb1#

您可以使用case。

SELECT 
     CASE WHEN YEAR > 2017 THEN
      ROUND(SUM(orderline_sales.amount * orderline_sales.price) * 
  orders_sales.discount * 
        customers.annual_discount)
   ELSE 
   (orderline_sales.price * orders_sales.discount * customers.annual_discount) 
   END  AS total_revenue FROM orderline_sales 
        JOIN
    orders_sales  ON orders_sales.id = orderline_sales.orders_sales_id
        JOIN
    employee ON orders_sales.empoyee_id = employee.id
        JOIN 
    customers ON orders_sales.customer_id = customers.id
   WHERE
      date BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 365 DAY) AND CURRENT_DATE
     GROUP BY employee.name
   ORDER BY totale_omzet DESC

相关问题