工作需求第一次写千行SQL语句

25 篇文章 0 订阅

加油,新时代打工人!

查询某个县区数据,没有的数据用0补充
需求:全年12个月,每个月显示县区信息,为空的用0补充。
也就是把上面的sql拼接起来,第一个解决方案,用上面的sql,加上JAVA代码。
第二个解决方案下面的方式,一个sql完成,这样好处,在前端直接拿起来就用了。

(SELECT
             '1月' AS title,
             t.regionCode,
            t.regionName,
						t.cropName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 t4.title AS cropName,
                        FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
                          
														   <where>
	   t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 1
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
        </where>
       
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						 	 0 AS cropName,
                           0 AS	torganic,
             0 AS  tn,
            0 AS	op,
            0 AS	ok,
					  0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
														   <where>
	   t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 1
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
        </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
				union all(SELECT
             '2月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 2
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						  	 0 AS cropName,
                           0 AS	torganic,
             0 AS  tn,
            0 AS	op,
            0 AS	ok,
						            0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 2
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all(SELECT
             '3月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 3
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						  	 0 AS cropName,
                           0 AS	torganic,
             0 AS  tn,
            0 AS	op,
            0 AS	ok,
						            0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 3
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all
			(SELECT
			'4月' AS title,
		  t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
		FROM
			(
				(
					SELECT
						t1.`code` AS regionCode,
						t1.full_name AS regionName,
							 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
					FROM
						t_adminregion t1
							LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
							LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
							 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 4
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
					GROUP BY
					t1.`code`
					ORDER BY
					t1.`code`
			) UNION ALL
		SELECT
         t1.CODE,
             t1.full_name,
						  	 0 AS cropName,
              0 AS	torganic,
             0 AS  tn,
            0 AS	op,
            0 AS	ok,
						0 AS	slk,
            0 AS	ph
		FROM
			t_adminregion t1
				LEFT JOIN (
				SELECT
					t1.`code` AS regionCode,
					t1.full_name AS regionName,
						 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
				FROM
					t_adminregion t1
						LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
						LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
						 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 4
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
		GROUP BY
			t1.`code`
		ORDER BY
			t1.`code`
			) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
union all (SELECT
             '5月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 5
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						  0 AS cropName,
              0 AS	torganic,
             0 AS  tn,
            0 AS	op,
            0 AS	ok,
						0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 5
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all
			(SELECT
			'6月' AS title,
				t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
		FROM
			(
				(
					SELECT
						t1.`code` AS regionCode,
						t1.full_name AS regionName,
							 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
					FROM
						t_adminregion t1
							LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
							LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
							 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 6
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
					GROUP BY
					t1.`code`
					ORDER BY
					t1.`code`
			) UNION ALL
		SELECT
			t1.CODE,
			t1.full_name,
			 	 0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
		FROM
			t_adminregion t1
				LEFT JOIN (
				SELECT
					t1.`code` AS regionCode,
					t1.full_name AS regionName,
						 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
				FROM
					t_adminregion t1
						LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
						LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
						 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 6
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
		GROUP BY
			t1.`code`
		ORDER BY
			t1.`code`
			) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all
			(SELECT
             '7月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 7
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						  	 0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 7
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
				union all(SELECT
             '8月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 8
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						  	 0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 8
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all(SELECT
             '9月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
                          FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 9
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name, 	
							0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
                       FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
                        FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
                        FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
                        FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
                        FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
                        FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 9
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all
			(SELECT
			'10月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
		FROM
			(
				(
					SELECT
						t1.`code` AS regionCode,
						t1.full_name AS regionName,
							 t4.title AS cropName,
					 FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
						FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
						FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
						FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
						FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
						FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
					FROM
						t_adminregion t1
							LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
							LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
							 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 10
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
					GROUP BY
					t1.`code`
					ORDER BY
					t1.`code`
			) UNION ALL
		SELECT
			t1.CODE,
			t1.full_name,
			 	 0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
		FROM
			t_adminregion t1
				LEFT JOIN (
				SELECT
					t1.`code` AS regionCode,
					t1.full_name AS regionName,
						 t4.title AS cropName,
				 FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
					FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
					FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
					FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
					FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
					FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
				FROM
					t_adminregion t1
						LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
						LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
						 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 10
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
		GROUP BY
			t1.`code`
		ORDER BY
			t1.`code`
			) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
union all (SELECT
             '11月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
         FROM
             (
                 (
                     SELECT
                         t1.`code` AS regionCode,
                         t1.full_name AS regionName,
												 	 t4.title AS cropName,
				 FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
					FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
					FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
					FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
					FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
					FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                     FROM
                         t_adminregion t1
                             LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                             LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
														  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 11
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>

                     GROUP BY
                     t1.`code`
                     ORDER BY
                     t1.`code`
             ) UNION ALL
         SELECT
             t1.CODE,
             t1.full_name,
						  	 0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
         FROM
             t_adminregion t1
                 LEFT JOIN (
                 SELECT
                     t1.`code` AS regionCode,
                     t1.full_name AS regionName,
										 	 t4.title AS cropName,
				 FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
					FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
					FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
					FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
					FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
					FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
                 FROM
                     t_adminregion t1
                         LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
                         LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
												  LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 11
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
         GROUP BY
             t1.`code`
         ORDER BY
             t1.`code`
        ) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
			union all
			(SELECT
			'12月' AS title,
             t.regionCode,
            t.regionName,
            t.organic,
            t.tn,
            t.op,
            t.ok,
            t.slk,
            t.ph
		FROM
			(
				(
					SELECT
						t1.`code` AS regionCode,
						t1.full_name AS regionName,
							 t4.title AS cropName,
				 FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
					FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
					FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
					FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
					FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
					FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
					FROM
						t_adminregion t1
							LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
							LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
							 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 12
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
					GROUP BY
					t1.`code`
					ORDER BY
					t1.`code`
			) UNION ALL
		SELECT
			t1.CODE,
			t1.full_name,
			 	 0 AS cropName,
            0 AS	torganic,
            0 AS  tn,
            0 AS	op,
            0 AS	ok,
            0 AS	slk,
            0 AS	ph
		FROM
			t_adminregion t1
				LEFT JOIN (
				SELECT
					t1.`code` AS regionCode,
					t1.full_name AS regionName,
						 t4.title AS cropName,
				 FORMAT(IFNULL(AVG( t3.organic),0),2) as organic,
					FORMAT(IFNULL(AVG( t3.T_N ),0),2) as tn,
					FORMAT(IFNULL(AVG( t3.O_P ),0),2) as op,
					FORMAT(IFNULL(AVG( t3.O_K ),0),2) as ok,
					FORMAT(IFNULL(AVG( t3.SLK ),0),2) as slk,
					FORMAT(IFNULL(AVG( t3.ph ),0),2)  as ph
				FROM
					t_adminregion t1
						LEFT JOIN t_test_main t2 ON SUBSTRING( t2.village, 1, 6 ) = SUBSTRING( t1.`code`, 1, 6 )
						LEFT JOIN t_test_crops t3 ON t3.test_id = t2.id
						 LEFT JOIN t_param_crops t4 ON t.id = t3.crops_id
										 <where>
															t1.`level` = 22
                             AND t2.is_del = 0
                             AND MONTH ( t2.create_time ) = 12
            <if test="stt1tDate != null  and stt1tDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &gt;=  #{stt1tDate}</if>
            <if test="endDate != null  and endDate != ''"> and DATE_FORMAT(t2.real_test_time,'%Y-%m-%d') &lt;= #{endDate}</if>
                  </where>
		GROUP BY
			t1.`code`
		ORDER BY
			t1.`code`
			) a ON t1.CODE = a.regionCode
		WHERE
		       t1.`level` = 22
			) t
		GROUP BY
			t.`regionCode`
		ORDER BY
			t.`regionCode`)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello World呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值