写了几个难一点的sql

写了几个难一点的sql

sql 复制代码
SELECT
bn.id AS book_node_id,
t.version_id,
bn.textbook_id,
s.id AS subject_id,
s.stage_id,
COUNT( CASE WHEN d.document_type_id = 1 AND d.scope IS NULL AND p.document_id IS NOT NULL THEN 1 END ) AS type_1_count,
COUNT(
CASE

WHEN d.document_type_id = 1 
AND d.scope IS NULL 
AND p.document_id IS NOT NULL 
AND d.modified_time >= '2023-03-14' 
AND d.modified_time <= '2024-03-14' THEN
1 
END 
) AS type_1_count_year,
COUNT( CASE WHEN d.document_type_id = 2 AND d.scope IS NULL AND p.document_id IS NOT NULL THEN 1 END ) AS type_2_count,
COUNT(
CASE

WHEN d.document_type_id = 2 
AND d.scope IS NULL 
AND p.document_id IS NOT NULL 
AND d.modified_time >= '2023-03-14' 
AND d.modified_time <= '2024-03-14' THEN
1 
END 
) AS type_2_count_year,
COUNT( CASE WHEN d.document_type_id = 3 AND d.scope IS NULL AND p.document_id IS NOT NULL THEN 1 END ) AS type_3_count,
COUNT(
CASE

WHEN d.document_type_id = 3 
AND d.scope IS NULL 
AND p.document_id IS NOT NULL 
AND d.modified_time >= '2023-03-14' 
AND d.modified_time <= '2024-03-14' THEN
1 
END 
) AS type_3_count_year,
COUNT( CASE WHEN d.document_type_id = 4 AND d.scope IS NULL AND p.document_id IS NOT NULL THEN 1 END ) AS type_4_count,
COUNT(
CASE

WHEN d.document_type_id = 4 
AND d.scope IS NULL 
AND p.document_id IS NOT NULL 
AND d.modified_time >= '2023-03-14' 
AND d.modified_time <= '2024-03-14' THEN
1 
END 
) AS type_4_count_year,
COUNT(
CASE

WHEN d.document_type_id IN ( 5, 6 ) 
AND d.scope IS NULL 
AND p.document_id IS NOT NULL THEN
1 
END 
) AS type_5_count,
COUNT(
CASE
	
	WHEN d.document_type_id IN ( 5, 6 ) 
	AND d.scope IS NULL 
	AND p.document_id IS NOT NULL 
	AND d.modified_time >= '2023-03-14' 
	AND d.modified_time <= '2024-03-14' THEN
		1 
	END 
	) AS type_5_count_year,
	COUNT( CASE WHEN d.scope IS NOT NULL AND p.document_id IS NOT NULL THEN 1 END ) AS paper_count,
	COUNT(
	CASE
			
			WHEN d.scope IS NOT NULL 
			AND p.document_id IS NOT NULL 
			AND d.modified_time >= '2023-03-14' 
			AND d.modified_time <= '2024-03-14' THEN
				1 
			END 
			) AS paper_count_year,
			COUNT( CASE WHEN r.id IS NOT NULL AND p.document_id IS NOT NULL THEN 1 END ) AS all_count,
			COUNT( CASE WHEN d.modified_time >= '2023-03-14' AND d.modified_time <= '2024-03-14' AND p.document_id IS NOT NULL THEN 1 END ) AS all_count_year 
		FROM
			basic_book_node bn
			LEFT JOIN basic_relation_document_book_node r ON bn.id = r.book_node_id
			LEFT JOIN basic_document d ON r.document_id = d.id
			LEFT JOIN basic_textbook t ON bn.textbook_id = t.id
			LEFT JOIN basic_version v ON t.version_id = v.id
			LEFT JOIN basic_subject s ON v.subject_id = s.id
			LEFT JOIN nrsc_document_publish_state p ON d.id = p.document_id 
		WHERE
			d.document_source IS NULL 
			AND d.is_deleted = 0 
	GROUP BY
bn.id
sql 复制代码
SELECT COALESCE
	( cd.count, 0 ) AS count,
	kp.id,
	kp.NAME,
	ty.id,
	ty.NAME 
FROM
	(
	SELECT
		a.id AS ab_point_id,
		b.id AS ab_type_id 
	FROM
		( SELECT id FROM zj_basic_knowledge_point WHERE basic_subject_id = 27 ) a
		INNER JOIN ( SELECT id FROM zj_basic_question_type WHERE subject_id = 27 ) b 
	) AS ab
	LEFT JOIN (
	SELECT
		COUNT( c.id ) AS count,
		t.id AS type_id,
		t.NAME AS type_name,
		d.id AS knowledge_point_id,
		d.NAME AS knowledge_point_name 
	FROM
		zj_jkx_basic_relation_question_knowledge_point p
		LEFT JOIN zj_jkx_basic_question c ON p.question_id = c.id
		LEFT JOIN zj_basic_knowledge_point d ON p.knowledge_point_id = d.id
		LEFT JOIN zj_basic_question_type t ON c.question_type_id = t.id 
	GROUP BY
		t.id,
		d.id 
	) AS cd ON ab.ab_point_id = cd.knowledge_point_id 
	AND ab.ab_type_id = cd.type_id
	LEFT JOIN zj_basic_question_type ty ON ab.ab_type_id = ty.id
	LEFT JOIN zj_basic_knowledge_point kp ON ab.ab_point_id = kp.id
相关推荐
dazhong20121 小时前
PLSQL 客户端连接 Oracle 数据库配置
数据库·oracle
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
了一li3 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
码农君莫笑3 小时前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio
别致的影分身4 小时前
使用C语言连接MySQL
数据库·mysql
京东零售技术5 小时前
“慢”增长时代的企业数据体系建设:超越数据中台
数据库
sdaxue.com6 小时前
帝国CMS:如何去掉帝国CMS登录界面的认证码登录
数据库·github·网站·帝国cms·认证码
o(╥﹏╥)6 小时前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
阿里嘎多学长7 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
Yuan_o_7 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端