SQL统计数据之总结

一、查询SQL

复制代码
SELECT
    t1.规则编号 AS 编码,
    t1.规则描述 AS 名称,
    SUM( CASE WHEN t3.DATA_SOURCES = '00' THEN 1 ELSE 0 END ) AS '类型01',
    SUM( CASE WHEN t3.DATA_SOURCES = '01' THEN 1 ELSE 0 END ) AS '类型02',
    SUM( CASE WHEN t3.DATA_SOURCES = '02' THEN 1 ELSE 0 END ) AS '类型03',
    SUM( CASE WHEN t3.DATA_SOURCES = '03' THEN 1 ELSE 0 END ) AS '类型04' 
FROM
    (
SELECT
    'A_M_0001' AS 规则编号,
    '规则01' AS 规则描述 UNION ALL
SELECT
    'A_M_0002' AS 规则编号,
    '规则02' AS 规则描述 UNION ALL
SELECT
    'A_M_0003' AS 规则编号,
    '规则03' AS 规则描述 UNION ALL
SELECT
    'A_M_0005' AS 规则编号,
    '规则04' AS 规则描述 UNION ALL
SELECT
    'A_M_0007' AS 规则编号,
    '规则05' AS 规则描述 UNION ALL
SELECT
    'A_M_0006' AS 规则编号,
    '规则06' AS 规则描述 UNION ALL
SELECT
    'A_M_0008' AS 规则编号,
    '规则07' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_01' AS 规则编号,
    '规则08' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_12' AS 规则编号,
    '规则09' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_02' AS 规则编号,
    '规则10' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_03' AS 规则编号,
    '规则11' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_13' AS 规则编号,
    '规则12' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_05' AS 规则编号,
    '规则13' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_11' AS 规则编号,
    '规则14' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_06' AS 规则编号,
    '规则15' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_14' AS 规则编号,
    '规则16' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_07' AS 规则编号,
    '规则17' AS 规则描述 UNION ALL
SELECT
    'A_J_0001_15' AS 规则编号,
    '规则18' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_01' AS 规则编号,
    '规则19' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_02' AS 规则编号,
    '规则20' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_03' AS 规则编号,
    '规则21' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_04' AS 规则编号,
    '规则22' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_05' AS 规则编号,
    '规则23' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_06' AS 规则编号,
    '规则24' AS 规则描述 UNION ALL
SELECT
    'A_J_0002_07' AS 规则编号,
    '规则25' AS 规则描述 UNION ALL
SELECT
    'A_J_0003_01' AS 规则编号,
    '规则26' AS 规则描述 UNION ALL
SELECT
    'A_J_0003_02' AS 规则编号,
    '规则27' AS 规则描述 UNION ALL
SELECT
    'A_J_0003_05' AS 规则编号,
    '规则28' AS 规则描述 
    ) t1
    LEFT JOIN RAMS_TRIAL_CHECKLIST t2 ON t2.RULE_CODE like concat('%',t1.规则编号,'%')
    LEFT JOIN RAMS_TRIAL_CHECKLIST_EXT t3 ON t2.CHECKLIST_ID = t3.CHECKLIST_ID 
WHERE
    DATE( t2.UPDATE_TIME ) = CURDATE( ) - INTERVAL 1 DAY 
GROUP BY t1.规则编号,t1.规则描述;

二、查询结果

三、总结

1.数据库表中不存在的字段,可以利用以下sql进行处理:
SELECT '60019311' AS code, '北京' AS name

union all

SELECT '60019312' AS code, '上海' AS name

union all

SELECT '60019313' AS code, '广州' AS name

union all

SELECT '60019314' AS code, '重庆' AS name

2.量表关联查询,利用【Like】进行条件关联:

复制代码
RAMS_TRIAL_CHECKLIST t2 ON t2.RULE_CODE like concat('%',t1.规则编号,'%')

3.case when sql语句:

复制代码
CASE WHEN t3.DATA_SOURCES = '00' THEN 1 ELSE 0 END

4.查询系统当前时间的前一天数据的数量:

复制代码
SELECT COUNT(ID) FROM DATA WHERE DATE( UPDATE_TIME ) = CURDATE( ) - INTERVAL 1 DAY
相关推荐
m0_748255262 小时前
运维实战---多种方式在Linux中部署并初始化MySQL
linux·运维·mysql
weisian1512 小时前
Mysql--基础篇--多表查询(JOIN,笛卡尔积)
数据库·mysql
天堂的恶魔9464 小时前
MySQL —— 在CentOS9下安装MySQL
数据库·mysql
多喝清晨的粥5 小时前
Mac MySQL 8.0.30的安装(保姆级教程)
数据库·mysql·macos
Smile丶凉轩6 小时前
MySQL使用C语言连接
c语言·mysql·adb
Rinai_R6 小时前
MySQL学习记录1【DQL和DCL】
数据库·笔记·学习·mysql
难以触及的高度6 小时前
linux下MySQL的数据存放
linux·运维·mysql
Linux运维老纪6 小时前
MySQL常用命令之汇总(Summary of Commonly Used Commands in MySQL)
服务器·数据库·计算机网络·mysql·云计算·运维开发
厦0046 小时前
【MySQL】MVCC详解, 图文并茂简单易懂
数据库·sql·mysql·mvcc·并发控制·undo日志