oracle—SQL技巧之(一)连续记录查询sql案例测试
有这样一个需求:需要查询出某个客户某一年那些天是有连续办理过业务,本文给予sql实现语句并测试,感兴趣的朋友可以了解下
需求说明:需要查询出某个客户某一年那些天是有连续办理过业务
实现SQL如下:
创建表:
代码如下:
create table test_num
(tyear number,
tdate date);
测试数据:
insert into test_num
select 2014,trunc(sysdate)-1 from dual union all
select 2014,trunc(sysdate)-002 from dual union all
select 2014,trunc(sysdate)-003 from dual union all
select 2014,trunc(sysdate)-004 from dual union all
select 2014,trunc(sysdate)-005 from dual union all
select 2014,trunc(sysdate)-007 from dual union all
select 2014,trunc(sysdate)-008 from dual union all
select 2014,trunc(sysdate)-009 from dual union all
select 2013,trunc(sysdate)-120 from dual union all
select 2013,trunc(sysdate)-121 from dual union all
select 2013,trunc(sysdate)-122 from dual union all
select 2013,trunc(sysdate)-124 from dual union all
select 2013,trunc(sysdate)-125 from dual union all
select 2013,trunc(sysdate)-127 from dual union all
select 2015,trunc(sysdate)-099 from dual union all
select 2015,trunc(sysdate)-100 from dual union all
select 2015,trunc(sysdate)-101 from dual union all
select 2015,trunc(sysdate)-102 from dual union all
select 2015,trunc(sysdate)-104 from dual union all
select 2015,trunc(sysdate)-105 from dual;
写SQL:
代码如下:
SELECT TYEAR, MIN(TDATE) AS STARTDATE, MAX(TDATE), COUNT(TYEAR) AS ENDNUM
FROM (SELECT A.*, A.TDATE - ROWNUM AS GNUM
FROM (SELECT * FROM TEST_NUM ORDER BY TYEAR, TDATE) A)
GROUP BY TYEAR, GNUM
ORDER BY TYEAR, MIN(TDATE)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL's slow query log is a log record provided by MySQL. It is used to record statements in MySQL whose query time exceeds (greater than) the set threshold (long_query_time) and records them in the slow query log.

PHP and PDO: How to execute complex SQL query statements When processing database operations, PHP provides a powerful extension library PDO (PHPDataObjects) to simplify interaction with the database. PDO supports a variety of databases, such as MySQL, SQLite, etc., and also provides a wealth of functions and methods to facilitate developers to perform various database operations. This article will introduce how to use PDO to execute complex SQL query statements, and attach corresponding code examples. Connect to the database

In web development, tables are the most basic and commonly used elements, and PHP is a popular server-side programming language. There are many common techniques and methods in table operations. This article will introduce common table operations in PHP programming. Displaying data tables In PHP, you can use the table tag in HTML to display data tables. It is worth noting that the table must be generated in a PHP script. Here is an example of a basic HTML table tag: <table><tr>

PHP is a powerful server-side scripting language that is widely used in web development. In web development, we often need to interact with the database and execute query statements to obtain data. This article will introduce you to how to write query statements and usage examples in PHP. 1. Connect to the database Before using PHP to query the database, you first need to establish a connection with the database. Generally, we will use the MySQL database as an example. The code to connect to the database is as follows: $servername=

In Go programming, using SQL queries is a common task. However, sometimes errors occur when executing SQL queries, causing the program to fail to execute correctly. In order to solve these errors, we need to have a deep understanding of how SQL queries and the Go language interact. Below are some possible errors and corresponding solutions. Lack of database driver In Go language, you need to use a specific database driver to connect and operate the database. If you try to perform a database query and the database driver is not properly installed and configured

With the development of network technology, PHP programming has become the mainstream of website development for many companies. In PHP programming, SQL query efficiency is an issue that requires every programmer to pay attention and deal with. Inefficient SQL query will lead to slow website response, high system load or other unfriendly effects. Therefore, this article will focus on introducing various SQL query efficiency optimization practices in PHP programming to improve the execution efficiency of the program and the response speed of the entire system. Database index Database index is a basic method to improve the speed of database query

How to optimize SQL query statements and index usage in PHP development? In PHP development, database query is a very common operation. However, when the amount of data increases, query performance may be affected, causing the application to slow down. In order to improve query performance, we need to optimize the use of SQL query statements and indexes. This article will introduce some optimization tips and best practices to help you improve SQL query performance in PHP development. 1. Use the correct index: Index is an important part of the database to improve query performance. in design data

PHP language is a commonly used server-side scripting language used in fields such as website development. In PHP language development, SQL query is a common operation, but due to various reasons, SQL query may cause errors. Therefore, developers need to handle these errors in their code to ensure the stability and correctness of the application. Generally speaking, there are several common ways to handle SQL query errors in PHP language: 1. Use try/catch block to handle exceptions. In PHP language, you can use try/catch
