Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

bagaimana saya bisa menghitung status yang diubah/diperbarui berdasarkan kategori untuk bulan tertentu?

SQL DEMO

SELECT q.year
     , q.month
     , q.name
     , COUNT(CASE WHEN q.last_status = 2 THEN 1 END) as pre_implementation
     , COUNT(CASE WHEN q.last_status = 3 
                   AND q.previous_status = 2 THEN 1 END) as implementation_operation
     , COUNT(CASE WHEN q.last_status = 3 
                   AND (q.previous_status = 1 or q.previous_status IS NULL)
                  THEN 1 END) as pre_operation
FROM (
    SELECT *, (SELECT MAX(p.status_id)
               FROM project_status p
               WHERE p.time < CONCAT(t.year,'/', t.month,'/1')
                 AND p.project_id = t.project_id
               ) as previous_status
    FROM (
        SELECT YEAR(time) as year
             , MONTH(time) as month
             , c.name
             , ps.project_id
             , MAX(status_id) as last_status
        FROM project_status ps
        JOIN projects p
          ON ps.project_id = p. project_id
        JOIN investments i 
          ON p.investment_id = i.investment_id
        JOIN categories c
          ON i.cat_id = c.cat_id
        GROUP BY YEAR(time), MONTH(time), c.name, ps.project_id
    ) t
) q
GROUP BY q.year, q.month, q.name

KELUARAN

+--------+-------+---------------+--------------------+--------------------------+---------------+
|  year  | month |     name      | pre_implementation | implementation_operation | pre_operation |
+--------+-------+---------------+--------------------+--------------------------+---------------+
| 2019   |     2 | Technology    |                  0 |                        0 |             0 |
| 2019   |     3 | Technology    |                  1 |                        0 |             0 |
| 2019   |     4 | services      |                  0 |                        0 |             0 |
| 2019   |     5 | agriculture   |                  0 |                        0 |             0 |
+--------+-------+---------------+--------------------+--------------------------+---------------+
| 2019   |     8 | Technology    |                  0 |                        1 |             0 |
| 2019   |     8 | agriculture   |                  1 |                        0 |             1 |
| 2019   |     8 | services      |                  0 |                        0 |             1 |
| 2019   |     8 | manufactures  |                  1 |                        0 |             0 |
+--------+-------+---------------+--------------------+--------------------------+---------------+



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bisakah Anda membantu mengubah kueri atau kueri lain untuk mendapatkan hasil yang diharapkan?

  2. Kesalahan fatal:Panggilan ke fungsi yang tidak ditentukan getsqlvaluestring()

  3. Batasi karakter di kolom kueri mysql

  4. Python:ModuleNotFoundError:Tidak ada nama Modul 'mysql'

  5. Hitung Instance di Tabel1 DAN tautan ke Tabel2