MYCAT 分库分表使用问题(数据问题)


		SELECT 
				t_result.date_mark, 
				t_result.region_code,
				t_result.area_code,  
				t_result.bill_code,
				t_result.holding_unit_id,
				t_result.holding_unit_code,
				IFNULL(SUM(t_result.valid_amount),0) valid_amount,
				IFNULL(SUM(t_result.valid_count),0) valid_count,
				IFNULL(SUM(t_result.invalid_amount),0) invalid_amount,
				IFNULL(SUM(t_result.invalid_count), 0) invalid_count,
				IFNULL(SUM(t_result.print_count), 0) print_count,
				IFNULL(SUM(t_result.print_amount), 0) print_amount,
				0 refund_count,
				0.00 refund_amount
		FROM (
			/*正票*/
			SELECT
		 DATE_FORMAT(bill_common.create_time,"%Y%m%d%H") date_mark,
				SUBSTRING(bill_common.id1, 1, 4) region_code,
				bill_common.holding_unit_id,
				bill_common.holding_unit_code,
			bill_common.area_code,
			bill_common.format_code bill_code,
				COUNT(1) valid_count,
				SUM(bill_common.total_amount) valid_amount,
				NULL invalid_count,
				NULL invalid_amount,
				NULL print_count,
				NULL print_amount
			FROM
				feb_bill_common bill_common
		 		
			 GROUP BY 
			date_mark,
			region_code,
			bill_common.holding_unit_id,
			bill_common.holding_unit_code,
			bill_common.area_code,
			bill_common.format_code
			
			UNION  ALL
		/*废票*/		
			SELECT
		 DATE_FORMAT(bill_common.cancel_dt,"%Y%m%d%H") date_mark,
				SUBSTRING(bill_common.id1, 1, 4) region_code,
			bill_common.holding_unit_id,
				bill_common.holding_unit_code,
			bill_common.area_code,
			bill_common.format_code bill_code,
				NULL valid_count,
				NULL valid_amount,
				COUNT(1) invalid_count,
				SUM(bill_common.total_amount) invalid_amount,
				NULL print_count,
				NULL print_amount
			FROM
				feb_bill_common bill_common
			WHERE
				bill_common.cancel_flag =1

			GROUP BY 
			date_mark, 
			region_code,
			bill_common.holding_unit_id,
			bill_common.holding_unit_code,
			bill_common.area_code,  
			bill_common.format_code
			
			UNION ALL
			/*打印*/
			SELECT
				DATE_FORMAT(bill_common.print_dt,"%Y%m%d%H") date_mark,
				SUBSTRING(bill_common.id1, 1, 4) region_code,
				bill_common.holding_unit_id,
				bill_common.holding_unit_code,
				bill_common.area_code,
				bill_common.format_code bill_code,
				NULL valid_count,
				NULL valid_amount,
				NULL invalid_count,
				NULL invalid_amount,
				COUNT(1) print_count,
				SUM(bill_common.total_amount) print_amount
			FROM
				feb_bill_common bill_common
			WHERE
				bill_common.print_num>0

			GROUP BY 
			date_mark, 
			region_code,
			bill_common.holding_unit_id,
			bill_common.holding_unit_code,
			bill_common.area_code,  
			bill_common.format_code
			
		)t_result
		WHERE t_result.date_mark={date_mark}
		GROUP BY 
			t_result.date_mark, 
			t_result.region_code,
			t_result.area_code,  
			t_result.bill_code,
			t_result.holding_unit_id,
			t_result.holding_unit_code

在mycat环境下查询分库分表 feb_bill_common结果 数据多|| 少 ,db说不能使用 查询最外层不能使用group by ,否则会有问题

SELECT 'MEDICAL' industry_type,
r.date_mark, 
r.region_code,
r.area_code,  
r.bill_code,
r.holding_unit_id,
r.holding_unit_code,
r.valid_amount,
r.valid_count,
r.invalid_amount,
r.invalid_count,
r.print_count,
r.print_amount,
r.refund_count,
r.refund_amount FROM (
		
		SELECT 
				t_result.date_mark, 
				t_result.region_code,
				t_result.area_code,  
				t_result.bill_code,
				t_result.holding_unit_id,
				t_result.holding_unit_code,
				IFNULL(SUM(t_result.valid_amount),0) valid_amount,
				IFNULL(SUM(t_result.valid_count),0) valid_count,
				IFNULL(SUM(t_result.invalid_amount),0) invalid_amount,
				IFNULL(SUM(t_result.invalid_count), 0) invalid_count,
				IFNULL(SUM(t_result.print_count), 0) print_count,
				IFNULL(SUM(t_result.print_amount), 0) print_amount,
				0 refund_count,
				0.00 refund_amount
		FROM (
			/*正票*/
			SELECT
		 DATE_FORMAT(bill_common.create_time,"%Y%m%d%H") date_mark,
				SUBSTRING(bill_common.id1, 1, 4) region_code,
				bill_common.holding_unit_id,
				bill_common.holding_unit_code,
			bill_common.area_code,
			bill_common.format_code bill_code,
				COUNT(1) valid_count,
				SUM(bill_common.total_amount) valid_amount,
				NULL invalid_count,
				NULL invalid_amount,
				NULL print_count,
				NULL print_amount
			FROM
				feb_bill_common bill_common
		 		
			 GROUP BY 
			date_mark,
			region_code,
			bill_common.holding_unit_id,
			bill_common.holding_unit_code,
			bill_common.area_code,
			bill_common.format_code
			
			UNION  ALL
		/*废票*/		
			SELECT
		 DATE_FORMAT(bill_common.cancel_dt,"%Y%m%d%H") date_mark,
				SUBSTRING(bill_common.id1, 1, 4) region_code,
			bill_common.holding_unit_id,
				bill_common.holding_unit_code,
			bill_common.area_code,
			bill_common.format_code bill_code,
				NULL valid_count,
				NULL valid_amount,
				COUNT(1) invalid_count,
				SUM(bill_common.total_amount) invalid_amount,
				NULL print_count,
				NULL print_amount
			FROM
				feb_bill_common bill_common
			WHERE
				bill_common.cancel_flag =1

			GROUP BY 
			date_mark, 
			region_code,
			bill_common.holding_unit_id,
			bill_common.holding_unit_code,
			bill_common.area_code,  
			bill_common.format_code
			
			UNION ALL
			/*打印*/
			SELECT
				DATE_FORMAT(bill_common.print_dt,"%Y%m%d%H") date_mark,
				SUBSTRING(bill_common.id1, 1, 4) region_code,
				bill_common.holding_unit_id,
				bill_common.holding_unit_code,
				bill_common.area_code,
				bill_common.format_code bill_code,
				NULL valid_count,
				NULL valid_amount,
				NULL invalid_count,
				NULL invalid_amount,
				COUNT(1) print_count,
				SUM(bill_common.total_amount) print_amount
			FROM
				feb_bill_common bill_common
			WHERE
				bill_common.print_num>0

			GROUP BY 
			date_mark, 
			region_code,
			bill_common.holding_unit_id,
			bill_common.holding_unit_code,
			bill_common.area_code,  
			bill_common.format_code
			
		)t_result
		WHERE t_result.date_mark={date_mark}
		GROUP BY 
			t_result.date_mark, 
			t_result.region_code,
			t_result.area_code,  
			t_result.bill_code,
			t_result.holding_unit_id,
			t_result.holding_unit_code
)r		

解决方案是在最外层包上一层 SELECT ... FROM ()r 把原SQL 放入 子查询中

你可能感兴趣的:(小目标,学习记录,mycat,db)