i want to order by column Tgl
and then order by column Usia
and i want my null row to display row with 0 value instead of skip it, i want to record all my rows even if they null
Lets say: I dont have row data on date ='2017-09-02' I want dispaly it as 0 value instead if skip it code:
SELECT CONCAT(DAY(`date`),' ',MONTHNAME(`date`),' ',YEAR(`date`)) AS Tgl,t.Usia, t.jmlCust
FROM(
SELECT '<20' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust, `date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE (YEAR(NOW())-YEAR(cs.tanggallahir))<20
AND nominalbelanja <> 0
GROUP BY DAY(gp.date)
UNION
SELECT '20-35' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust, `date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE (YEAR(NOW())-YEAR(cs.tanggallahir)) BETWEEN 20 AND 35
AND nominalbelanja <> 0
GROUP BY DAY(gp.date)
UNION
SELECT '36-50' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust, `date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE (YEAR(NOW())-YEAR(cs.tanggallahir)) BETWEEN 36 AND 50
AND nominalbelanja <> 0
GROUP BY DAY(gp.date)
UNION
SELECT 'TOTAL' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust,`date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE nominalbelanja <> 0
GROUP BY DAY(gp.date)
ORDER BY `date`
) t
create one temp table as below,
create table test1(
Usia varchar(10));
insert into test1 values ('<20'),('20-35'),('36-50'),('TOTAL');
now apply join with your result table.
SELECT CONCAT(DAY(`date`),' ',MONTHNAME(`date`),' ',YEAR(`date`)) AS Tgl,test1.Usia, ifnull(t.jmlCust,0)
FROM(
SELECT '<20' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust, `date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE (YEAR(NOW())-YEAR(cs.tanggallahir))<20
AND nominalbelanja <> 0
GROUP BY DAY(gp.date)
UNION
SELECT '20-35' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust, `date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE (YEAR(NOW())-YEAR(cs.tanggallahir)) BETWEEN 20 AND 35
AND nominalbelanja <> 0
GROUP BY DAY(gp.date)
UNION
SELECT '36-50' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust, `date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE (YEAR(NOW())-YEAR(cs.tanggallahir)) BETWEEN 36 AND 50
AND nominalbelanja <> 0
GROUP BY DAY(gp.date)
UNION
SELECT 'TOTAL' AS Usia ,COALESCE(COUNT(*),0) AS jmlCust,`date` FROM getpoint gp
LEFT JOIN customer cs ON gp.CardID = cs.CardID
WHERE nominalbelanja <> 0
GROUP BY DAY(gp.date)
ORDER BY `date`
) t right join test1 on (test1.Usia = t.Usia);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.