mysql 多个站点的近30天平均每天加油金额订单数
未知
2020-04-30 17:52:29
0
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

相关内容

MySQL变量的生命周期管...
MySQL变量管理关键在于生命周期控制。需了解全局、会话及用户自定...
2024-11-20 20:46:37
MySQL变量类型与用途
MySQL支持多种数据类型,包括数值、字符串、日期和时间等类型,以...
2024-11-20 20:23:37
MySQL变量的作用域与访...
摘要: MySQL变量具有作用域和访问控制,局部变量限于特定上下...
2024-11-20 20:00:40
MySQL变量类型列表
MySQL变量类型包括数值、字符串、日期和时间等类型,用于存储不同...
2024-11-20 19:46:45
MySQL变量数据类型介绍
MySQL支持多种数据类型,包括数值、字符串、日期时间及枚举和集合...
2024-11-20 19:23:39
MySQL变量定义与初始化
MySQL变量分为系统变量和用户定义变量,用于存储数据和配置参数。...
2024-11-20 19:00:42

热门资讯

mysql 多个站点的近30天... SELECT j.title, ( SELECT sum( realoil_price ) FROM...
mysql Please DI... mysql插入数据表时总是提示 Please DISCARD the tablespace befo...
sql 批量修改表前缀 示例:将dede_前缀修改成xiong_前缀 先查询以 dede_ 前缀的表: 需要修改的部分:re...
phpstudy在linux上... 开始配置 在线安装phpstudy一键包: 1.在Xshell里面输入wget -c http://...
mysql把表中数据插入到其他... 两张字段相同的表,一张作为产品库,一张作为自定义库,每增一个新客户就把产品库中的所有数据插入到...
tp5中MySQL如何获取JS... 第一种: // 假设$user是从数据库中查询出的用户信息 $user = Db::name(use...
mysql 一次查询,返回多个... 问题描述: 在一个表中有多个条件,其中两个条件是共同的,另外两个条件是不同,一条sql语句返回多个统...
navicate测试登录Acc... 安全组3306已经放行。宝塔【安全】中也放行3306。使用账号密码在navicate上登录数据库,出...
sql注入方法及防御危害 SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服...
使用Linux安装phpstu... 问题描述: 买了阿里云,配置好PHPstudy后好久没用,后来登录mysql,忘记密码,所以登录不上...