前言
那些年我写过的最长sql语句,在此写成博客,以此备份,便于后续工作参考
目录
1.开通用户数
2.开通用户数明细
3.注册用户数
4.注册用户数明细
5.相似博客推荐
1.开通用户数
SELECT W.REGION_NAME AREANAME, A.PROVINCE_NO areaNo, SUBSTR(A.PROVINCE_NO,0,2) cityNO, NVL(A.COUNT, 0) BUSI_HISSUM, NVL(B.COUNT, 0) BUSI_TOD, NVL(C.COUNT, 0) BUSI_YES, D.HISMAX BUSI_HISMAX, E.HISAVG BUSI_HISAVG FROM (SELECT T.PROVINCE_NO, COUNT(T.PROVINCE_NO) COUNT FROM W_BUSI_USER_BIND T WHERE T.SERVICE_TYPE = '01' AND T.ORGAN_CODE IS NOT NULL AND T.PROVINCE_NO IS NOT NULL AND T.BIND_TIME IS NOT NULL GROUP BY T.PROVINCE_NO) A, (SELECT T.PROVINCE_NO, COUNT(T.USER_NO) COUNT FROM W_BUSI_USER_BIND T WHERE T.SERVICE_TYPE = '01' AND T.ORGAN_CODE IS NOT NULL AND T.BIND_TIME IS NOT NULL AND T.PROVINCE_NO IS NOT NULL AND TO_CHAR(T.BIND_TIME, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD') GROUP BY T.PROVINCE_NO) B, (SELECT T.PROVINCE_NO, COUNT(T.USER_NO) COUNT FROM W_BUSI_USER_BIND T WHERE T.SERVICE_TYPE = '01' AND T.ORGAN_CODE IS NOT NULL AND T.BIND_TIME IS NOT NULL AND T.PROVINCE_NO IS NOT NULL AND TO_CHAR(T.BIND_TIME, 'YYYY-MM-DD') = TO_CHAR(SYSDATE - 1, 'YYYY-MM-DD') GROUP BY T.PROVINCE_NO) C, (SELECT A.PROVINCE_NO, MAX(A.USER_NO) HISMAX FROM (SELECT T.PROVINCE_NO, COUNT(T.USER_NO) USER_NO, TO_CHAR(T.BIND_TIME, 'YYYY-MM-DD') FROM W_BUSI_USER_BIND T WHERE T.SERVICE_TYPE = '01' AND T.ORGAN_CODE IS NOT NULL AND T.BIND_TIME IS NOT NULL AND T.PROVINCE_NO IS NOT NULL GROUP BY T.PROVINCE_NO, TO_CHAR(T.BIND_TIME, 'YYYY-MM-DD')) A GROUP BY A.PROVINCE_NO) D, (SELECT A.PROVINCE_NO, FLOOR(AVG(A.USER_NO)) HISAVG FROM (SELECT T.PROVINCE_NO, COUNT(T.USER_NO) USER_NO, TO_CHAR(T.BIND_TIME, 'YYYY-MM-DD') FROM W_BUSI_USER_BIND T WHERE T.SERVICE_TYPE = '01' AND T.ORGAN_CODE IS NOT NULL AND T.BIND_TIME IS NOT NULL AND T.PROVINCE_NO IS NOT NULL GROUP BY T.PROVINCE_NO, TO_CHAR(T.BIND_TIME, 'YYYY-MM-DD')) A GROUP BY A.PROVINCE_NO) E, W_SYS_REGION W WHERE W.REGION_TYPE = '02' AND W.ISACTIVE = '1' AND A.PROVINCE_NO = W.REGION_CODE(+) AND A.PROVINCE_NO = B.PROVINCE_NO(+) AND A.PROVINCE_NO = C.PROVINCE_NO(+) AND A.PROVINCE_NO = D.PROVINCE_NO(+) AND A.PROVINCE_NO = E.PROVINCE_NO(+) ORDER BY W.REGION_CODE
2.开通用户数明细查询
SELECT a.area_no areaNo, a.count busi_hisSum, NVL(b.count, 0) busi_tod, NVL(c.count, 0) busi_yes, d.count busi_hisMax, e.count busi_hisAvg from (select area_no, count(*) count from w_sync_organ_areas a, w_busi_user_bind b where a.org_no = b.organ_code and exists (select 1 from p_code p where code_type = '60004200' and valid_flag = '1' and a.area_no = p.value) group by area_no) A, (select area_no, count(0) count from w_sync_organ_areas a, w_busi_user_bind b where a.org_no = b.organ_code and to_char(b.bind_time, 'YYYY-MM-DD') = to_char(SYSDATE, 'YYYY-MM-DD') and exists (select 1 from p_code p where code_type = '60004200' and valid_flag = '1' and a.area_no = p.value) group by area_no) B, (select area_no area_no, count(*) count from w_sync_organ_areas a, w_busi_user_bind b where a.org_no = b.organ_code and to_char(b.bind_time, 'YYYY-MM-DD') = to_char(SYSDATE - 1, 'YYYY-MM-DD') and exists (select 1 from p_code p where code_type = '60004200' and valid_flag = '1' and a.area_no = p.value) group by area_no) C, (select s.area_no, Max(s.count) count from (select area_no area_no, TO_CHAR(b.bind_time, 'YYYY-MM-DD'), count(*) count from w_sync_organ_areas a, w_busi_user_bind b where a.org_no = b.organ_code and exists (select 1 from p_code p where code_type = '60004200' and valid_flag = '1' and a.area_no = p.value) group by area_no, to_char(b.bind_time, 'YYYY-MM-DD')) s group by s.area_no) D, (select s.areaNo area_no, floor(AVG(s.count)) count from (select area_no areaNo, to_char(b.bind_time, 'YYYY-MM-DD'), count(*) count from w_sync_organ_areas a, w_busi_user_bind b where a.org_no = b.organ_code and exists (select 1 from p_code p where code_type = '60004200' and valid_flag = '1' and a.area_no = p.value) group by area_no, to_char(b.bind_time, 'YYYY-MM-DD')) s group by s.areaNo) e where a.area_no = b.area_no(+) and a.area_no = c.area_no(+) and a.area_no = d.area_no(+) and a.area_no = e.area_no(+) and a.cityNo is not null order by busi_hisSum DESC
3.注册用户数
SELECT A.AREA_NO, SUBSTR(a.area_no,0,2) cityNo, A.NAME AREANAME, NVL(A.CT, 0) HISSUM, NVL(B.CT, 0) TOD, NVL(C.CT, 0) YES, NVL(D.CT, 0) HISAVG, NVL(E.CT, 0) HISMAX FROM (SELECT TMP.NAME, A.AREA_NO, A.CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.AREA_NO, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.CITY_NO IS NOT NULL GROUP BY WU.AREA_NO) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.AREA_NO) A, (SELECT TMP.NAME, A.AREA_NO, A.CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.AREA_NO, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.CITY_NO IS NOT NULL AND TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD') GROUP BY WU.AREA_NO) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.AREA_NO) B, (SELECT TMP.NAME, A.AREA_NO, A.CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.AREA_NO, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.CITY_NO IS NOT NULL AND TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD') = TO_CHAR(SYSDATE - 1, 'YYYY-MM-DD') GROUP BY WU.AREA_NO) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.AREA_NO) C, (SELECT TMP.NAME, A.AREA_NO, MAX(A.CT) CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.AREA_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD') REG_DATE, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.CITY_NO IS NOT NULL GROUP BY WU.AREA_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD')) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.AREA_NO GROUP BY TMP.NAME, A.AREA_NO) D, (SELECT TMP.NAME, A.AREA_NO, FLOOR(AVG(A.CT)) CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.AREA_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD') REG_DATE, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.CITY_NO IS NOT NULL GROUP BY WU.AREA_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD')) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.AREA_NO GROUP BY TMP.NAME, A.AREA_NO) E WHERE A.AREA_NO = B.AREA_NO(+) AND A.AREA_NO = C.AREA_NO(+) AND A.AREA_NO = D.AREA_NO(+) AND A.AREA_NO = E.AREA_NO(+) ORDER BY A.AREA_NO
4.注册用户数明细
SELECT A.CITY_NO area_no, A.NAME AREANAME, SUBSTR(a.city_no,0,2) cityNo, NVL(A.CT, 0) HISSUM, NVL(B.CT, 0) TOD, NVL(C.CT, 0) YES, NVL(D.CT, 0) HISAVG, NVL(E.CT, 0) HISMAX FROM (SELECT TMP.NAME, A.CITY_NO, A.CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.CITY_NO, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.AREA_NO = #areaNo# AND WU.CITY_NO IS NOT NULL GROUP BY WU.CITY_NO) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.CITY_NO) A, (SELECT TMP.NAME, A.CITY_NO, A.CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.CITY_NO, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.AREA_NO = #areaNo# AND WU.CITY_NO IS NOT NULL AND TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD') GROUP BY WU.CITY_NO) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.CITY_NO) B, (SELECT TMP.NAME, A.CITY_NO, A.CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.CITY_NO, COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.AREA_NO = #areaNo# AND WU.CITY_NO IS NOT NULL AND TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD') = TO_CHAR(SYSDATE - 1, 'YYYY-MM-DD') GROUP BY WU.CITY_NO) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.CITY_NO) C, (SELECT TMP.NAME, A.CITY_NO, MAX(A.CT) CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.CITY_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD'), COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.AREA_NO = #areaNo# AND WU.CITY_NO IS NOT NULL GROUP BY WU.CITY_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD')) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.CITY_NO GROUP BY TMP.NAME, A.CITY_NO) D, (SELECT TMP.NAME, A.CITY_NO, FLOOR(AVG(A.CT)) CT FROM CSWEB_GLOBAL.P_CODE TMP, (SELECT WU.CITY_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD'), COUNT(WU.LOGIN_NAME) CT FROM W_USERS WU WHERE WU.AREA_NO = #areaNo# AND WU.CITY_NO IS NOT NULL GROUP BY WU.CITY_NO, TO_CHAR(WU.REG_DATE, 'YYYY-MM-DD')) A WHERE TMP.CODE_TYPE = '60004200' AND TMP.VALUE = A.CITY_NO GROUP BY TMP.NAME, A.CITY_NO) E WHERE A.CITY_NO = B.CITY_NO(+) AND A.CITY_NO = C.CITY_NO(+) AND A.CITY_NO = D.CITY_NO(+) AND A.CITY_NO = E.CITY_NO(+) ORDER BY A.CITY_NO
5.相似博客推荐
相关推荐
**泛微系统SQL语句大全** 在IT行业中,泛微系统是一种广泛应用的企业级协同办公软件,主要用于提升组织的管理效率和工作流程自动化。本资源集合了泛微系统中与SQL Server数据库交互时常用的各种SQL语句,涵盖了组织...
在Oracle数据库管理中,了解SQL语句的执行性能和频率对于系统优化至关重要。本文将详细介绍如何查看Oracle数据库中执行最慢和查询次数最多的SQL语句,以帮助DBA(数据库管理员)识别潜在的性能瓶颈。 首先,我们来...
查询语句是SQL中最常用的,包括基础查询、连接查询、子查询、聚合函数等,如: ```sql SELECT column1, column2,... FROM table_name WHERE condition ``` 3. 更新数据: ```sql UPDATE table_name SET column1 = ...
### DB2 SQL语句性能分析方法详解 #### 一、引言 在现代数据库管理系统(DBMS)中,SQL语句的性能优化对于提高整体应用程序的响应速度和资源利用率至关重要。IBM DB2 Universal Database (UDB) 作为一款成熟且功能...
9. **优化最昂贵的SQL语句**:找出耗时最长或资源消耗最大的SQL语句进行优化,可能的策略包括创建合适的索引、调整查询语句、使用绑定变量、避免全表扫描等。 10. **反复迭代**:性能优化往往不是一次性的,需要...
这是因为,在大量的SQL语句中,只有一部分是经常被执行的,而这些语句往往对性能的影响最大。调优时,我们通常利用计数器等工具来跟踪语句执行的情况,并通过柱状图等可视化方式找出执行次数最多、耗时最长的语句。 ...
在SQL Server中,监控SQL语句的执行时间是优化数据库性能和提高代码效率的重要环节。以下是如何在SQL Server上查看SQL语句执行时间的几种方法: 1. **简单时间差计算法**: 这是最基础的方法,通过记录查询开始和...
### SQL Server 查询优化:识别与分析高资源消耗的SQL语句 #### 背景介绍 在数据库管理系统(DBMS)中,SQL Server 是一种广泛使用的解决方案,它为各种规模的企业提供强大的数据存储和处理能力。然而,在实际应用...
SQL(Structured Query Language)是用于管理关系数据库的编程语言,这里我们详细解析一下与图书管理系统相关的SQL语句。 首先,创建数据库`db_library`,这是整个系统的基础: ```sql CREATE DATABASE db_library...
在这里,会列出整体执行时间最长的SQL语句,并按照它们的总执行时间进行排序。这种排序方式帮助DBA快速识别出那些占用资源较多、执行时间较长的SQL语句。 在“SQL ordered by Elapsed Time”部分中,DBA可以查看到...
在本示例中,我们探讨如何使用SQL语句在MySQL数据库中实现成语接龙功能。首先,我们需要创建两个表,分别为`title`和`title_copy`,这两个表用于存储成语数据。 1. **表结构创建**: - `title`表:这个表包含了...
", 所有SQL语句的执行信息便会被写到一个数据文件中,该数据文件位于子目录sql_dump下面。 假定你的数据库位于目录/home/postgres/database 下,那么你可以在/home/postgres/database/sql_dump下找到该数据文件。 ...
本篇文章将探讨如何快速定位导致MSSQL CPU占用高的SQL语句,以帮助优化数据库性能。 首先,我们可以采用方法一来定位问题: 1. 登录到SQL Server Management Studio (SSMS),右键点击数据库实例,选择“活动和监视...
2. SQL执行统计:提供详细的SQL执行统计信息,包括执行次数、平均执行时间、最长时间等,帮助我们了解SQL语句的使用频率和性能状况。 3. 资源占用监测:监控数据库的内存使用、磁盘I/O等资源占用情况,以便在资源...
本文将详细介绍如何使用SQL语句来监控IBM DB2数据库中的关键性能指标,包括运行时间最长、执行次数最多以及执行成本最高的SQL查询。 #### 一、运行时间最长的SQL查询 首先,我们来看看如何找出那些运行时间最长的...
在这一结构中,每个节点都代表SQL语句的一个组成部分,包括关键字、表名、列名、操作符等。通过这样的结构,复杂的SQL语句被分解为更易于计算机理解的层次化数据。而基于SQL语法树的过滤方法,则是利用这种结构来...
以下是从提供的文件内容中提炼出的一些SQL语句精华和知识点: 1. **ORDER BY 数值型灵活使用**: 使用`DECODE`函数可以灵活控制排序。例如,`DECODE(函数, 'asc', 1, 'desc', -1)`可以将升序和降序转换为正负数值...
- **SQL语句**: ```sql SELECT username AS 用户名 FROM card WHERE password = username OR password = ID; ``` - **知识点解析**:这里涉及到的是基本的SELECT查询语句,用来找出`card`表中`password`字段与`...
2. **SQL语句分析**:工具可以列出执行时间最长、占用资源最多的SQL语句,便于分析和优化。通过查看SQL文本,DBA可以找出可能导致性能问题的复杂查询,并对其进行重构。 3. **等待事件诊断**:等待事件是Oracle性能...