sql >> データベース >  >> RDS >> Mysql

MySQLは、月ごとのグループを含む、他の2つの計算された合計のパーセンテージを計算します

    条件付き集計を使用します。過去12/24か月を確認するのか、2017年と2016年の同じ月を確認するのかは明確ではありません。また、パーセンテージの計算方法もわかりません。以下のクエリで、今年の利益を昨年の利益で割ります。ニーズに合うように調整してください。

    select
      b_emp_id,
      month,
      turnover_this_year,
      profit_this_year,
      turnover_last_year,
      profit_last_year,
      profit_this_year / profit_last_year * 100 as diff
    from
    (
      select
        b_emp_id,
        month(b_date) as month,
        sum(case when year(b_date) = year(curdate()) then b_turnover end) as turnover_this_year,
        sum(case when year(b_date) = year(curdate()) then b_profit end) as profit_this_year,
        sum(case when year(b_date) < year(curdate()) then b_turnover end) as turnover_last_year,
        sum(case when year(b_date) < year(curdate()) then b_profit end) as profit_last_year
      from bookings
      where year(b_date) in (year(curdate()), year(curdate()) - 1)
        and month(b_date) <= month(curdate())
      group by b_emp_id, month(b_date)
    ) figures
    order by b_emp_id, month;
    



    1. OracleトリガーエラーORA-04091

    2. 複数の基準を持つ製品を検索する

    3. MySQLで動的SQLを使用してカーソルを作成する

    4. 制約チェック:TRY / CATCH vs Exists()