Click here to Skip to main content
15,888,803 members
Articles / Database Development
Tip/Trick

Use SQL to generate large data sequence

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Feb 2015CPOL 22.1K   6   2
Generate large data series by using Cartesian Product and small data table

The proposed solution demonstrates the concept of generating a large data series by using structured query language (SQL) and relatively small data tables, thus dramatically reducing the overall 'digital footprint' (rather important design consideration for any mobile/compact/embedded solutions).
 
Sample coding technique to demonstrate the concept utilizes a small table (Table1) with just 10 records, containing integer numbers: 0,1,2,3,...9, and a single SQL query to generate a virtual time line, which spans for 10,000 days, or roughly 27 years moving backward from the current date (Access SQL, Listing 1):
 

SQL
SELECT Now()-CLng([Table1].[Num] & [Table1_1].[Num] & [Table1_2].[Num] & [Table1_3].[Num]) AS TimeSeries
FROM Table1, Table1 AS Table1_1, Table1 AS Table1_2, Table1 AS Table1_3
ORDER BY Now()-CLng([Table1].[Num] & [Table1_1].[Num] & [Table1_2].[Num] & [Table1_3].[Num]) DESC;

 
The logic behind the solution is rather straightforward. The query creates 2 virtual tables (Table_1, Table_2 and Table_3) in addition to the 'concrete' permanent Table1, and then applies the so-called 'Cartesian product' technique, concatenating every record from all 4 tables (permanent and virtual as well). Then it applies the data type conversion to long integer, which represents the date, and finally, sorts the date series of 10,000 records, going from current date backward in descending order.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
United States United States
Dr. Alexander Bell (aka DrABell), a seasoned full-stack DevOps and Data Engineer holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 100+ technical articles, including those popular at CodeProject totaling 4M+ views. Alex pioneered AI/NLP, Cloud development, .NET/Java technology stacks, advanced SQL extensions, HTML5/CSS3 and other important Web technologies; developed multiple award-winning Web/Win apps submitted to App Innovation Contests (AIC 2012/2013). Currently focused on Microsoft Azure Cloud and GitHub Copilot AI-enabled DevOps.

  1. Quiz Engine powered by Azure Cloud (Dev Challenge article)
  2. 'enRoute': Real-time NY City Bus Tracking Web App (IoT on Azure)
  3. Azure web app: Engineering Calculator VOLTMATTER
  4. Azure: NYC real-time bus tracking app
  5. HTML5/CSS3 graphic enhancement: buttons, inputs
  6. Aggregate Product function extends SQL
  7. HTML5 Tables Formatting: Alternate Rows, Color Gradients, Shadows
  8. YouTube™ API for ASP.NET

Comments and Discussions

 
QuestionSQL Server Pin
Member 1041007620-Feb-15 14:33
Member 1041007620-Feb-15 14:33 
GeneralFYI: short URL to the full article: http://exm.nr/SQLSeq Pin
DrABELL4-Jan-11 9:36
DrABELL4-Jan-11 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.