您现在的位置是:

mysql 多个站点的近30天平均每天加油金额订单数

发布时间:2020-04-30作者:小灵龙来源:未知点击:179

SELECT
	j.title,
	(
	SELECT		
                sum( realoil_price ) 
	FROM
		user 
	WHERE
		confirm_date >= UNIX_TIMESTAMP( CAST( SYSDATE( ) AS DATE ) - INTERVAL 30 DAY ) 
		AND confirm_date < now( ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
	) AS '近30天总加油额',
	round((
	SELECT
		sum( realoil_price ) 
	FROM
		user 
	WHERE
		confirm_date >= UNIX_TIMESTAMP( CAST( SYSDATE( ) AS DATE ) - INTERVAL 30 DAY ) 
		AND confirm_date < now( ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
	)/30,2) AS '平均每天加油额',
	(
	SELECT
		count( id ) 
	FROM
		user 
	WHERE
		confirm_date >= UNIX_TIMESTAMP( CAST( SYSDATE( ) AS DATE ) - INTERVAL 30 DAY ) 
		AND confirm_date < now( ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
	) AS '30天总加油笔数',
	round((
	SELECT
		count( id ) 
	FROM
		user 
	WHERE
		confirm_date >= UNIX_TIMESTAMP( CAST( SYSDATE( ) AS DATE ) - INTERVAL 30 DAY ) 
		AND confirm_date < now( ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
	) / 30,2) AS '平均每天加油笔数',
	(
	SELECT
		sum( realoil_price ) 
	FROM
		user 
	WHERE
		date_format( from_unixtime( confirm_date ), '%Y-%m-%d' ) = date_format( now( ), '%Y-%m-%d' ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
	) AS '今日加油额',
	round((
	SELECT
		sum( realoil_price ) 
	FROM
		user 
	WHERE
		date_format( from_unixtime( confirm_date ), '%Y-%m-%d' ) = date_format( now( ), '%Y-%m-%d' ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
		) - (
		(
		SELECT
			sum( realoil_price ) 
		FROM
			user 
		WHERE
			confirm_date >= UNIX_TIMESTAMP( CAST( SYSDATE( ) AS DATE ) - INTERVAL 30 DAY ) 
			AND confirm_date < now( ) 
			AND is_type = 3 
			AND order_state = 2 
			AND site_id = j.id 
		) / 30 
	),2) AS '今日-平均(加油额)',
	(
	SELECT
		count( id ) 
	FROM
		user 
	WHERE
		date_format( from_unixtime( confirm_date ), '%Y-%m-%d' ) = date_format( now( ), '%Y-%m-%d' ) 
		AND is_type = 3 
		AND order_state = 2 
		AND site_id = j.id 
	) AS '今日加油笔数',
	round((
		(
		SELECT
			count( id ) 
		FROM
			user 
		WHERE
			date_format( from_unixtime( confirm_date ), '%Y-%m-%d' ) = date_format( now( ), '%Y-%m-%d' ) 
			AND is_type = 3 
			AND order_state = 2 
			AND site_id = j.id 
			) - (
		SELECT
			count( id ) 
		FROM
			user 
		WHERE
			confirm_date >= UNIX_TIMESTAMP( CAST( SYSDATE( ) AS DATE ) - INTERVAL 30 DAY ) 
			AND confirm_date < now( ) 
			AND is_type = 3 
			AND order_state = 2 
			AND site_id = j.id 
		) / 30 
	),2) AS '今日-平均订单数' 
FROM
	zhan AS j 
WHERE
	j.id IN (
		1001,
		1002,
		1004,
		1006,
		1010,
		1086,
		1095,
		1118,
		1122,
		1123,
		1140,
		1160,
		1169,
		1177 
	) 
ORDER BY	j.id DESC

round(x,d) :用于数据的四舍五入,round(x)  ,其实就是round(x,0),也就是默认d为0;

这里有个值得注意的地方是,d可以是负数,这时是指定小数点左边的d位整数位为0,同时小数位均为0;

SELECT ROUND(100.3465,2),ROUND(100,2),ROUND(0.6,2),ROUND(114.6,-1);

结果分别:100.35,100,0.6,110

如果帮到了您,请打赏一二!

标签: