Query untuk menggabungkan menjadi 1 data

Hallo temen-temen semua,

saya punya query seperti ini :

SELECT
	DATE_FORMAT(tglinput_si,'%m') as tgl,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='01',grandtot,0)) AS jan,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='02',grandtot,0)) AS feb,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='03',grandtot,0)) AS mar,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='04',grandtot,0)) AS apr,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='05',grandtot,0)) AS may,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='06',grandtot,0)) AS jun,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='07',grandtot,0)) AS jul,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='08',grandtot,0)) AS ags,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='09',grandtot,0)) AS sep,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='10',grandtot,0)) AS okt,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='11',grandtot,0)) AS nov,
	sum(if(DATE_FORMAT(tglinput_si,'%m')='12',grandtot,0)) AS des
FROM
	pastibisa_service_so
WHERE
	tglinput_si !=''
GROUP BY
	tgl

union
SELECT
	DATE_FORMAT(tanggal,'%m') as tgl,
	sum(if(DATE_FORMAT(tanggal,'%m')='01',total,0)) AS jan,
	sum(if(DATE_FORMAT(tanggal,'%m')='02',total,0)) AS feb,
	sum(if(DATE_FORMAT(tanggal,'%m')='03',total,0)) AS mar,
	sum(if(DATE_FORMAT(tanggal,'%m')='04',total,0)) AS apr,
	sum(if(DATE_FORMAT(tanggal,'%m')='05',total,0)) AS may,
	sum(if(DATE_FORMAT(tanggal,'%m')='06',total,0)) AS jun,
	sum(if(DATE_FORMAT(tanggal,'%m')='07',total,0)) AS jul,
	sum(if(DATE_FORMAT(tanggal,'%m')='08',total,0)) AS ags,
	sum(if(DATE_FORMAT(tanggal,'%m')='09',total,0)) AS sep,
	sum(if(DATE_FORMAT(tanggal,'%m')='10',total,0)) AS okt,
	sum(if(DATE_FORMAT(tanggal,'%m')='11',total,0)) AS nov,
	sum(if(DATE_FORMAT(tanggal,'%m')='12',total,0)) AS des
FROM
	service_pengeluaran_part
GROUP BY tgl

dan hasilnya seperti link gambar berikut ini : <a href='https://drive.google.com/file/d/1xKDIgelU_ky3HZa7jEkd2SALsLxytfAE/view'>capture1.png</a>

nah saya ingin mengabungkan jadi satu data tersebut termasuk jumlahnya, ada yg bisa bantu engga. terima kasih sebelumnya.

avatar sugiarto27
@sugiarto27

2 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

2 Jawaban:

Jawaban Terpilih

Subquery aja..

<pre> SELECT tgl, SUM(jan) AS jan, SUM(feb) AS feb, SUM(mar) AS mar, SUM(apr) AS apr, SUM(may) AS may, SUM(jun) AS jun, SUM(jul) AS jul, SUM(ags) AS ags, SUM(sep) AS sep, SUM(okt) AS okt, SUM(nov) AS nov, SUM(des) AS des FROM ( SELECT DATE_FORMAT(tglinput_si,'%m') as tgl, sum(if(DATE_FORMAT(tglinput_si,'%m')='01',grandtot,0)) AS jan, sum(if(DATE_FORMAT(tglinput_si,'%m')='02',grandtot,0)) AS feb, sum(if(DATE_FORMAT(tglinput_si,'%m')='03',grandtot,0)) AS mar, sum(if(DATE_FORMAT(tglinput_si,'%m')='04',grandtot,0)) AS apr, sum(if(DATE_FORMAT(tglinput_si,'%m')='05',grandtot,0)) AS may, sum(if(DATE_FORMAT(tglinput_si,'%m')='06',grandtot,0)) AS jun, sum(if(DATE_FORMAT(tglinput_si,'%m')='07',grandtot,0)) AS jul, sum(if(DATE_FORMAT(tglinput_si,'%m')='08',grandtot,0)) AS ags, sum(if(DATE_FORMAT(tglinput_si,'%m')='09',grandtot,0)) AS sep, sum(if(DATE_FORMAT(tglinput_si,'%m')='10',grandtot,0)) AS okt, sum(if(DATE_FORMAT(tglinput_si,'%m')='11',grandtot,0)) AS nov, sum(if(DATE_FORMAT(tglinput_si,'%m')='12',grandtot,0)) AS des FROM pastibisa_service_so WHERE tglinput_si !='' GROUP BY tgl

union
SELECT 
	DATE_FORMAT(tanggal,&#039;%m&#039;) as tgl,
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;01&#039;,total,0)) AS jan, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;02&#039;,total,0)) AS feb, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;03&#039;,total,0)) AS mar, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;04&#039;,total,0)) AS apr, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;05&#039;,total,0)) AS may, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;06&#039;,total,0)) AS jun, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;07&#039;,total,0)) AS jul, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;08&#039;,total,0)) AS ags, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;09&#039;,total,0)) AS sep, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;10&#039;,total,0)) AS okt, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;11&#039;,total,0)) AS nov, 
	sum(if(DATE_FORMAT(tanggal,&#039;%m&#039;)=&#039;12&#039;,total,0)) AS des 
FROM
	service_pengeluaran_part
GROUP BY tgl

) temp GROUP BY tgl </pre>

Semoga terbantu

avatar meij0hn
@meij0hn

2 Kontribusi 2 Poin

Dipost 4 tahun yang lalu

terima kasih @meij0hn sudah bisa.

avatar sugiarto27
@sugiarto27

2 Kontribusi 0 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban