目录

写了几个难一点的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
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
程序猿熊跃晖5 分钟前
Excel 数据导入与 SQL 生成:基于 Hutool 和 Apache POI 的优雅实践
sql·apache·excel
kfepiza21 分钟前
Debian用二进制包安装mysql8.0.41 笔记250401
数据库·笔记·mysql·debian·database
在努力的韩小豪31 分钟前
B树和B+树的区别(B Tree & B+ Tree)
数据结构·数据库·b树·b+树·索引·数据库索引
Watink Cpper42 分钟前
[MySQL初阶]MySQL(8)索引机制:下
android·数据库·b树·mysql·b+树·myisam·innodedb
freejackman1 小时前
MySQL 基础入门
数据库·后端·sql·mysql
二年级程序员1 小时前
SQL语句(一)—— DDL
数据库·sql·mysql
邴越1 小时前
不同向量数据库(FAISS / Pinecone / Weaviate)在 RAG 中的优缺点
数据库·faiss
Allen Bright1 小时前
【MySQL基础-21】MySQL事务机制详解:原理、实现与最佳实践
数据库·mysql
movie__movie2 小时前
Spring AI MCP 客户端实战:轻松连接高德地图等工具
数据库·人工智能·spring
清风19812 小时前
kafka消息可靠性传输语义
数据库·分布式·kafka