SQL query case: Search for incomplete data _ Cartesian product in a continuous date

Source: Internet
Author: User
From: http://hi.baidu.com/wangzhiqing999/blog/item/8db9453dafb73bfc3b87ce51.html
Test Table and test data

Create Table testdatacheck (
Id varchar (5), -- device ID
Dates datetime -- Date
)
Insert into testdatacheck values ('device 1', '2017-10-01 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-02 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-03 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-04 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-06 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-07 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-08 00:00:00 ');
Insert into testdatacheck values ('device 1', '2017-10-09 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-01 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-02 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-03 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-07 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-08 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-09 00:00:00 ');
Insert into testdatacheck values ('device 2', '2017-10-10 00:00:00 ');
Requirements

Retrieve records missing from all devices between and
Ideas

First, a 10-day date sequence is required.
Then, a 10-day device arrangement and combination are required.
Finally, you can use an permutation and combination to join the target table to retrieve the missing data.
Implementation

Step 1: Create a date sequence
Currently, 1-10 is used to store date changes. At last, the date is processed when it is associated with the primary table.
Select top 10
Row_number () over (order by (select 0) as no
From SYS. Tables
No
--------------------
1
2
3
4
5
6
7
8
9
10
Step 2: Date and device arrangement and combination
Select
All_id.id,
All_day_no.no
From
(
Select top 10
Row_number () over (order by (select 0) as no
From SYS. Tables
) As all_day_no,
(
Select distinct
ID
From
Testdatacheck
) As all_id
ID No
-------------------------
Device 1 1
Device 2 1
Device 1 2
Device 2 2
Device 1 3
Device 2 3
Device 1 4
Device 2 4
Device 1 5
Device 2 5
Device 1 6
Device 2 6
Device 1 7
Device 2 7
Device 1 8
Device 2 8
Device 1 9
Device 2 9
Device 1 10
Device 2 10
Step 3 arrange the association between the combination and the target table
Select
Convert (datetime, '2017-09-30 00:00:00 ') + alltestdatacheck. No as date,
Alltestdatacheck. ID
From
(
Select
All_id.id,
All_day_no.no
From
(
Select top 10
Row_number () over (order by (select 0) as no
From SYS. Tables
) As all_day_no,
(
Select distinct
ID
From
Testdatacheck
) As all_id
) Alltestdatacheck
Left join testdatacheck
On (alltestdatacheck. ID = testdatacheck. ID
And convert (datetime, '2017-09-30 00:00:00 ') + alltestdatacheck. No = testdatacheck. Dates)
Where
Testdatacheck. dates is null
Execution result

Date ID
----------------------------
2010-10-04 00:00:00. 000 device 2
00:00:00. 000 device 1
2010-10-05 00:00:00. 000 device 2
2010-10-06 00:00:00. 000 device 2
2010-10-10 00:00:00. 000 device 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.