【SQL】结果集操作

字符串操作

连接字符串:SELECT CONCAT(MONTH,'月') FROM tabelA
截取字符串:LEFT(character,integer)RIGHT(character,integer)SUBSTRING(character,start,length)
特定字符串位置:INSTR(character|columName,character)

 

保留两位小数

SELECT ROUND(column_name,decimals) FROM table_name
SELECT CONVERT(DECIMAL(13,2),13.123)

 

日期根据月份分组

SELECT * FROM TABLEA GROUP BY DATE_FORMAT('date', '%Y-%m')

 

计算不同类别所占当天比例,并替换null值

SELECT
    t1.date,(ROUND(t2.great/t1.total,2))AS g_rate,IFNULL((ROUND(t3.`first`/t1.total,2)),0) AS f_rate,IFNULL((ROUND(t4.unknown/t1.total,2)),0) AS u_rate 
FROM 
    (SELECT date,COUNT(*) AS total FROM production_qualification_urea GROUP BY date) AS t1
LEFT JOIN
    (SELECT date,COUNT(*) AS great FROM production_qualification_urea WHERE grade='优等品' GROUP BY date) AS t2
ON 
    t1.date = t2.date
LEFT JOIN
    (SELECT date,COUNT(*) AS `first`  FROM production_qualification_urea WHERE grade='一等品' GROUP BY date) AS t3
ON 
    t1.date = t3.date
LEFT JOIN
    (SELECT date,COUNT(*) AS `unknown`  FROM production_qualification_urea WHERE grade='-' GROUP BY date) AS t4
ON 
    t1.date = t4.date

 
 

未完待续….

你可能感兴趣的:(………数据库)