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

Permintaan MySQL untuk menghitung harga rata-rata terbaru menggunakan data dari beberapa tabel

Opsinya adalah kueri yang bekerja dengan variabel pengguna untuk menghasilkan peringkat berdasarkan tanggal. Dan hanya pilih tiga tanggal terakhir.

Kueri

SELECT 
 *
FROM (
   SELECT 
   * 
   , CASE 
       WHEN @supplier = supplier
       THEN @rank := @rank + 1 
       ELSE @rank := 1
     END 
      AS rank
   , @supplier := supplier 
   FROM 
     Expenses 
   CROSS JOIN (
    SELECT
       @supplier := NULL
     , @rank := 0
    ) 
    AS
      init_user_params  
   WHERE
      product_id = 1       
   ORDER BY
      supplier ASC 
    , DATE DESC   
)
 AS Expenses_ranked 
WHERE
  Expenses_ranked.rank <= 3

Hasil

    id  product_id  cost    quantity  supplier  date        @supplier := NULL  @rank := 0    rank  @supplier := supplier  
------  ----------  ------  --------  --------  ----------  -----------------  ----------  ------  -----------------------
     4           1  3.98           2         1  2017-09-22  (NULL)                      0       1                        1
     3           1  2.50           1         1  2017-09-20  (NULL)                      0       2                        1
     1           1  2.99           1         1  2017-09-05  (NULL)                      0       3                        1
     6           1  8.00           2         2  2017-09-27  (NULL)                      0       1                        2
     5           1  4.00           1         2  2017-09-25  (NULL)                      0       2                        2
     2           1  3.00           2         2  2017-09-10  (NULL)                      0       3                        2

Menggunakan hasil itu untuk menghasilkan daftar rata-rata per pemasok.

Kueri

SELECT 
   Expenses_ranked.supplier 
 , AVG(Expenses_ranked.cost / Expenses_ranked.quantity) AS AVG
FROM ( 

  SELECT 
   * 
   , CASE 
       WHEN @supplier = supplier
       THEN @rank := @rank + 1 
       ELSE @rank := 1
     END 
      AS rank
   , @supplier := supplier 
   FROM 
     Expenses 
   CROSS JOIN (
    SELECT
       @supplier := NULL
     , @rank := 0
    ) 
    AS
      init_user_params  
   WHERE
      product_id = 1       
   ORDER BY
      supplier ASC 
    , DATE DESC   
)
 AS Expenses_ranked 
WHERE
  Expenses_ranked.rank <= 3
GROUP BY
  Expenses_ranked.supplier 

Hasil

supplier  avg           
--------  --------------
       1  2.4933333333  
       2  3.1666666667  

Sekarang kita dapat menggunakan ORDER BY [] ASC LIMIT 1 yang sederhana untuk mendapatkan pemasok termurah

Kueri

SELECT 
 Expenses_ranked_avg.supplier AS cheapest_supplier
FROM ( 

  SELECT 
     Expenses_ranked.supplier 
   , AVG(Expenses_ranked.cost / Expenses_ranked.quantity) AS AVG
  FROM ( 

    SELECT 
     * 
     , CASE 
         WHEN @supplier = supplier
         THEN @rank := @rank + 1 
         ELSE @rank := 1
       END 
      AS rank
   , @supplier := supplier 
   FROM 
     Expenses 
   CROSS JOIN (
    SELECT
       @supplier := NULL
     , @rank := 0
    ) 
    AS
      init_user_params  
   WHERE
      product_id = 1       
   ORDER BY
      supplier ASC 
    , DATE DESC   
    )
      AS
        Expenses_ranked 
    WHERE
      Expenses_ranked.rank <= 3
    GROUP BY
      Expenses_ranked.supplier 
)
  AS Expenses_ranked_avg 
ORDER BY 
 Expenses_ranked_avg.avg ASC
LIMIT 1

Hasil

cheapest_supplier  
-------------------
                  1

Kueri yang lebih optimal.

Apa juga yang mungkin untuk mendeklarasikan variabel pengguna di dalam pernyataan where. Membuatnya secara langsung memungkinkan untuk menyaring peringkat.

Kueri

  SELECT 
   *
  FROM
   Expenses 
  WHERE
   (
     CASE 
       WHEN @supplier = supplier
       THEN @rank := @rank + 1 
       ELSE @rank := 1
     END  
   ) 
 AND
   (@supplier := supplier )
 AND  
   @rank <= 3  
 AND  
   product_id = 1
ORDER BY 
   supplier ASC
 , DATE ASC  

Hasil

    id  product_id  cost    quantity  supplier  date        
------  ----------  ------  --------  --------  ------------
     1           1  2.99           1         1  2017-09-05  
     3           1  2.50           1         1  2017-09-20  
     4           1  3.98           2         1  2017-09-22  
     2           1  3.00           2         2  2017-09-10  
     5           1  4.00           1         2  2017-09-25  
     6           1  8.00           2         2  2017-09-27 

Sekarang mudah menggunakan set hasil ini untuk menemukan pemasok termurah.

Kueri

SELECT 
   Expenses_ranked.supplier AS cheapest_supplier
FROM (  

  SELECT 
   *
  FROM
   Expenses 
  WHERE
   (
     CASE 
       WHEN @supplier = supplier
       THEN @rank := @rank + 1 
       ELSE @rank := 1
     END  
   ) IS NOT NULL 
 AND
   (@supplier := supplier ) IS NOT NULL
 AND  
   @rank <= 3  
 AND  
   product_id = 1
ORDER BY 
   supplier ASC
 , DATE ASC  
)
 AS Expenses_ranked 
GROUP BY
  Expenses_ranked.supplier
ORDER BY 
  AVG(Expenses_ranked.cost / Expenses_ranked.quantity) ASC
LIMIT 1  

Hasil

cheapest_supplier  
-------------------
                  1


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL menambahkan 12 jam ke bidang waktu

  2. TAMPILKAN TABEL di MySQL

  3. Impor Kolom dari CSV ke Tabel MySQL yang ada

  4. apakah ini mengunci database?

  5. penjumlahan record aktif di codeigniter