Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created January 12, 2013 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickmaciel/4520123 to your computer and use it in GitHub Desktop.
Save patrickmaciel/4520123 to your computer and use it in GitHub Desktop.
Recursive select with conditions
CREATE DATABASE teste001;
USE teste001;
CREATE TABLE invoices (invoice not null serial, add_date datetime);
INSERT INTO invoices VALUES
(1, '2010-01-21 00:00:00.000'),
(2, '2010-02-11 00:00:00.000'),
(3, '2010-01-30 00:00:00.000'),
(4, '2010-03-11 00:00:00.000'),
(5, '2010-04-21 00:00:00.000'),
(6, '2010-05-05 00:00:00.000'),
(7, '2010-06-02 00:00:00.000'),
(8, '2010-07-03 00:00:00.000'),
(9, '2011-08-04 00:00:00.000'),
(10, '2011-09-07 00:00:00.000'),
(11, '2011-10-12 00:00:00.000'),
(12, '2011-11-05 00:00:00.000'),
(13, '2011-11-01 00:00:00.000'),
(14, '2011-12-02 00:00:00.000'),
(15, '2011-01-01 00:00:00.000'),
(16, '2011-07-15 00:00:00.000'),
(17, '2011-08-01 00:00:00.000'),
(18, '2011-02-13 00:00:00.000'),
(19, '2011-12-01 00:00:00.000'),
(20, '2011-01-01 00:00:00.000'),
(21, '2011-09-29 00:00:00.000'),
(31, '2011-01-21 00:00:00.000'),
(32, '2011-02-11 00:00:00.000'),
(33, '2011-01-30 00:00:00.000'),
(34, '2011-03-11 00:00:00.000'),
(35, '2011-04-21 00:00:00.000'),
(36, '2011-05-05 00:00:00.000'),
(37, '2011-06-02 00:00:00.000'),
(38, '2011-07-03 00:00:00.000'),
(39, '2011-08-04 00:00:00.000'),
(40, '2011-09-07 00:00:00.000'),
(41, '2011-10-12 00:00:00.000'),
(42, '2012-11-05 00:00:00.000'),
(43, '2012-11-01 00:00:00.000'),
(44, '2012-12-02 00:00:00.000'),
(45, '2012-01-01 00:00:00.000'),
(46, '2012-07-15 00:00:00.000'),
(47, '2012-08-01 00:00:00.000'),
(48, '2012-02-13 00:00:00.000'),
(49, '2012-12-01 00:00:00.000'),
(41, '2012-09-29 00:00:00.000'),
(51, '2012-01-21 00:00:00.000'),
(52, '2012-02-11 00:00:00.000'),
(53, '2012-01-30 00:00:00.000'),
(54, '2012-03-11 00:00:00.000'),
(55, '2012-04-21 00:00:00.000'),
(56, '2012-05-05 00:00:00.000'),
(57, '2012-06-02 00:00:00.000'),
(58, '2012-07-03 00:00:00.000'),
(59, '2012-08-04 00:00:00.000'),
(60, '2012-09-07 00:00:00.000'),
(61, '2012-10-12 00:00:00.000'),
(62, '2012-11-05 00:00:00.000'),
(63, '2012-11-01 00:00:00.000'),
(64, '2012-12-02 00:00:00.000'),
(65, '2012-01-01 00:00:00.000'),
(66, '2012-07-15 00:00:00.000'),
(67, '2012-08-01 00:00:00.000'),
(68, '2012-02-13 00:00:00.000'),
(69, '2012-12-01 00:00:00.000'),
(60, '2012-01-01 00:00:00.000'),
(71, '2012-01-21 00:00:00.000'),
(72, '2012-02-11 00:00:00.000'),
(73, '2012-01-30 00:00:00.000'),
(74, '2012-03-11 00:00:00.000'),
(75, '2012-04-21 00:00:00.000'),
(76, '2012-05-05 00:00:00.000'),
(77, '2012-06-02 00:00:00.000'),
(78, '2012-07-03 00:00:00.000'),
(79, '2012-08-04 00:00:00.000'),
(80, '2012-09-07 00:00:00.000'),
(81, '2013-10-12 00:00:00.000'),
(82, '2013-11-05 00:00:00.000'),
(83, '2013-11-01 00:00:00.000'),
(84, '2013-12-02 00:00:00.000'),
(85, '2013-01-01 00:00:00.000'),
(86, '2013-07-15 00:00:00.000'),
(87, '2013-08-01 00:00:00.000'),
(88, '2013-02-13 00:00:00.000'),
(89, '2013-12-01 00:00:00.000'),
(90, '2013-01-01 00:00:00.000'),
(91, '2013-09-29 00:00:00.000');
CREATE TABLE invoice_products (invoice not null serial, product int not null, price, quantity);
INSERT INTO invoice_products VALUES
(1, 1, 1, 1),
(2, 2, 2, 2),
(3, 3, 3, 3),
(4, 4, 4, 4),
(5, 5, 5, 5),
(6, 1, 6, 6),
(7, 2, 7, 7),
(8, 3, 8, 8),
(9, 1, 9, 9),
(10, 1, 10 ,10),
(11, 1, 11 ,11),
(12, 1, 12 ,12),
(13, 5, 13 ,13),
(14, 3, 14 ,14),
(15, 4, 15 ,15),
(16, 5, 16 ,16),
(17, 5, 17 ,17),
(18, 5, 18 ,18),
(19, 2, 19 ,19),
(20, 1, 20 ,20),
(21, 5, 21, 21),
(31, 1, 1, 1),
(32, 2, 2, 2),
(33, 3, 3, 3),
(34, 4, 4, 4),
(35, 5, 5, 5),
(36, 3, 6, 6),
(37, 3, 7, 7),
(38, 3, 8, 8),
(39, 1, 9, 9),
(40, 1, 10 ,10),
(41, 4, 11 ,11),
(42, 4, 12 ,12),
(43, 5, 13 ,13),
(44, 3, 14 ,14),
(45, 4, 15 ,15),
(46, 5, 16 ,16),
(47, 2, 17 ,17),
(48, 2, 18 ,18),
(49, 2, 19 ,19),
(41, 1, 20 ,20),
(51, 5, 21, 21),
(52, 1, 1, 1),
(53, 5, 2, 2),
(54, 2, 3, 3),
(55, 4, 4, 4),
(56, 5, 5, 5),
(57, 2, 6, 6),
(58, 2, 7, 7),
(59, 3, 8, 8),
(60, 1, 9, 9),
(61, 1, 10 ,10),
(62, 3, 11 ,11),
(63, 4, 12 ,12),
(64, 5, 13 ,13),
(65, 5, 14 ,14),
(66, 5, 15 ,15),
(67, 5, 16 ,16),
(68, 5, 17 ,17),
(69, 3, 18 ,18),
(60, 5, 19 ,19),
(71, 1, 20 ,20),
(72, 5, 21, 21),
(73, 1, 1, 1),
(74, 1, 2, 2),
(75, 1, 3, 3),
(76, 1, 4, 4),
(77, 5, 5, 5),
(78, 1, 6, 6),
(79, 2, 7, 7),
(80, 3, 8, 8),
(81, 5, 9, 9),
(82, 1, 10 ,10),
(83, 2, 11 ,11),
(84, 3, 12 ,12),
(85, 2, 13 ,13),
(86, 2, 14 ,14),
(87, 2, 15 ,15),
(88, 2, 16 ,16),
(89, 2, 17 ,17),
(90, 2, 18 ,18),
(91, 3, 19 ,19);
CREATE TABLE products (product, name);
INSERT INTO products VALUES
(1, 'product 01'),
(2, 'product 02'),
(3, 'product 03'),
(4, 'product 04'),
(5, 'product 05');
CREATE TABLE date_periods (id serial not null, period int not null, year int not null, starting_date datetime, ending_date datetime);
INSERT INTO date_periods VALUES
(1, 1, 2013, '2013-01-01', '2013-01-29'),
(2, 2, 2013, '2013-02-01', '2013-02-19'),
(3, 3, 2013, '2013-03-01', '2013-03-25'),
(4, 4, 2013, '2013-04-01', '2013-04-22'),
(5, 5, 2013, '2013-05-01', '2013-05-15'),
(6, 6, 2013, '2013-06-01', '2013-06-17'),
(7, 7, 2013, '2013-07-01', '2013-07-28'),
(8, 8, 2013, '2013-08-01', '2013-08-30'),
(9, 9, 2013, '2013-09-01', '2013-09-22'),
(10, 10, 2013, '2013-10-01', '2013-10-23'),
(11, 11, 2013, '2013-11-01', '2013-11-20'),
(12, 12, 2013, '2013-12-01', '2013-12-27'),
(14, 1, 2012, '2012-01-01', '2012-01-29'),
(15, 2, 2012, '2012-02-01', '2012-02-19'),
(16, 3, 2012, '2012-03-01', '2012-03-25'),
(17, 4, 2012, '2012-04-01', '2012-04-22'),
(18, 5, 2012, '2012-05-01', '2012-05-15'),
(19, 6, 2012, '2012-06-01', '2012-06-17'),
(20, 7, 2012, '2012-07-01', '2012-07-28'),
(21, 8, 2012, '2012-08-01', '2012-08-30'),
(22, 9, 2012, '2012-09-01', '2012-09-22'),
(23, 10, 2012, '2012-10-01', '2012-10-23'),
(24, 11, 2012, '2012-11-01', '2012-11-20'),
(25, 12, 2012, '2012-12-01', '2012-12-27'),
(26, 1, 2011, '2011-01-01', '2011-01-29'),
(27, 2, 2011, '2011-02-01', '2011-02-19'),
(28, 3, 2011, '2011-03-01', '2011-03-25'),
(29, 4, 2011, '2011-04-01', '2011-04-22'),
(30, 5, 2011, '2011-05-01', '2011-05-15'),
(31, 6, 2011, '2011-06-01', '2011-06-17'),
(32, 7, 2011, '2011-07-01', '2011-07-28'),
(33, 8, 2011, '2011-08-01', '2011-08-30'),
(34, 9, 2011, '2011-09-01', '2011-09-22'),
(35, 10, 2011, '2011-10-01', '2011-10-23'),
(36, 11, 2011, '2011-11-01', '2011-11-20'),
(37, 12, 2011, '2011-12-01', '2011-12-27');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment