Langsung ke konten utama

Menghitung Sub Total Menggunakan WITH ROLLUP Pada Query MySQL

Pada MySQL, klausa WITH ROLLUP digunakan bersamaan dengan klausa GROUP BY,  WITH ROLLUP dapat digunakan baik untuk menghitung total maupun sub total sesuai dengan kolom yang kita gunakan dalam klausa GROUP BY.
Kelebihan klausa ini adalah simpel dan dapat menghasilkan total dan subtotal hanya dalam satu query, sehingga membuat pekerjaan kita jauh lebih mudah dan sederhana.
Misal, untuk menyelesaikan pembuatan total dan subtotal sebagaimana contoh sebelumnya, kita hanya perlu menambahkan WITH ROLLUP pada akhir query, sehingga querynya menjadi seperti ini:


SELECT thn_byr, id_pelanggan, id_produk, SUM(jml_byr) AS jml_byr FROM `sales` GROUP BY thn_byr, id_pelanggan, id_produk WITH ROLLUP

ketika kita jalankan query tersebut (menggunakan HEIDI SQL), hasil yang kita peroleh adalah: 

Menghitung Total dan Subtotal Pada MySQL dengan With Rollup

 jika kita perhatikan, MySQL melakukan tiga fungsi penjumlahan berdasarkan kolom yang ada di group by
Menghitung Total dan Subtotal Pada MySQL
  1. thn_byr. MySQL menambahkan baris baru untuk menjumlahkan kolom jml_byr per thn_byr dengan memberikan nilai NULL pada kolom id_pelanggan dan id_produk
  2. thn_byr dan id_pelanggan. MySQL menambahkan baris baru untuk menjumlahkan kolom jml_byr per (thn_byr dan id_pelanggan) dengan memberikan nilai NULL pada kolom id_produk
  3. thn_byr, id_pelanggan dan id_produk. MySQL menambahkan baris baru untuk menjumlahkan kolom jml_byr untuk per (id_produk, id_pelanggan dan thn_byr) dengan memberikan nilai NULL pada kolom thn_byr, id_pelanggan dan id_produk.
Perhatikan kolom yang diberikan tanda kotak merah, kenapa nilainya seperti itu? Nilai pada kolom tersebut, dan kolom sejenis yang dihasilkan oleh klausa GROUP BY dipilih secara bebas oleh MySQL, namun biasanya mengikuti nilai pada baris sebelumnya.

 

ini sumbernya:

http://jagowebdev.com/menghitung-total-dan-subtotal-pada-mysql/

Komentar

Postingan populer dari blog ini

REGEX: Regular Expressions

  Standards [ edit ] The  IEEE   POSIX  standard has three sets of compliance:  BRE  (Basic Regular Expressions), [29]   ERE  (Extended Regular Expressions), and  SRE  (Simple Regular Expressions). SRE is  deprecated , [30]  in favor of BRE, as both provide  backward compatibility . The subsection below covering the  character classes  applies to both BRE and ERE. BRE and ERE work together. ERE adds  ? ,  + , and  | , and it removes the need to escape the metacharacters  ( )  and  { } , which are  required  in BRE. Furthermore, as long as the POSIX standard syntax for regexes is adhered to, there can be, and often is, additional syntax to serve specific (yet POSIX compliant) applications. Although POSIX.2 leaves some implementation specifics undefined, BRE and ERE provide a "standard" which has since been adopted as the default syntax of many tools, where the choi...

Error when install new Instance or SSIS SQL Server 2008

Error : attributes do not match. present attributes (directory compressed) included attributes (0) Cause:  Sql server will validate the installation folder during the setup process to check if no Archive attribute as set on the installation folder The installation folder includes “C:\Program Files\Microsoft SQL Server and if you have it C:\Program Files (x86)\Microsoft SQL Server.”  Solution: - Go to SQL server installation directory - C:\Program Files\Microsoft SQL Server - C:\Program Files (x86)\Microsoft SQL Server - Right click on the folder and go to properties - Click on general tab and click on Advanced and uncheck the option “Folder is ready for archiving” - Try doing the installation again it will be successful

Difference: SQL UNION vs UNION ALL?

  Difference: SQL UNION vs UNION ALL ? UNION and UNION ALL are both used to retrieve records from multiple tables. This article will detail the differences between the two, allowing you to make the best choice for each unique scenario. You can use SQL’s  UNION  and  UNION ALL  commands to get data from multiple tables in your database. It’s a common use case, considering that most databases have many tables. Both  UNION  and  UNION ALL  are known as set operators. In SQL, set operators combine the results of two or more queries into a single result. You can  read more about set operators in this article . When comparing  UNION  vs.  UNION ALL , there is one major difference: UNION  only returns  unique UNION ALL  returns  all  records, including duplicates. If you feel like you would benefit from a well-structured, comprehensive course that covers foundational SQL, consider this  SQL...