简体   繁体   中英

How to create rows in SQL based on a criteria?

Is there a way in SQL to create rows based from the previous month's values?

We're tracking a metric quarterly so the actuals for January would be the same for Feb and March.

What's the best way to do this in SQL ?

Use "insert from select":

insert into mytable (month, column1, column2)
select 'feb', column1, column2 from mytable where month='jan'

insert into mytable (month, column1, column2)
select 'mar', column1, column2 from mytable where month='jan'

List all columns that you would like to copy.

This question is a little difficult to answer for the following reasons:

  1. Please tag with the engine you are using. No one engine completely conforms to the SQL Standard, particularly as it applies to date/time operations.
  2. Are your data timestamps, daily, weekly, or monthly data?
  3. Can you provide some DDL and expected output?

With that housekeeping taken care of, lets assume you have daily data. I pulled some stock price data from Yahoo Finance for AAPL. I have provided an example of a query which provides average close price data for the preceeding quarter. Using PostgreSQL syntax. Note: this syntax would vary under another engine such as Oracle (See: ADD_MONTHS ) or SQL Server .

SELECT 
  p.trading_date,
  EXTRACT(QUARTER FROM p.trading_date) current_quarter,
  EXTRACT(MONTH FROM p.trading_date) current_month,
  EXTRACT(QUARTER FROM p.trading_date - INTERVAL '3 MONTHS') last_quarter,
  EXTRACT(YEAR FROM p.trading_date - INTERVAL '3 MONTHS') last_quarter_year,
  (
     SELECT AVG(p2.close_price)
     FROM price_data p2
     WHERE 1 = 1
      AND EXTRACT(QUARTER FROM p2.trading_date) = EXTRACT(QUARTER FROM p.trading_date - INTERVAL '3 MONTHS')
      AND EXTRACT(YEAR FROM p2.trading_date) = EXTRACT(YEAR FROM p.trading_date - INTERVAL '3 MONTHS')
  ) average_close_price_last_quarter,
  p.close_price
FROM price_data p;

CREATE TABLE price_data ( trading_date DATE NOT NULL, tag VARCHAR(50), close_price numeric(10, 2) );

Sample DDL for Daily Data:

INSERT INTO price_data (trading_date, close_price) VALUES 
('2017-07-10',142.84),
('2017-07-12',143.509186),
('2017-07-13',145.508102),
('2017-07-14',146.758636),
('2017-07-17',147.270706),
('2017-07-18',147.782761),
('2017-07-19',148.708359),
('2017-07-20',148.038757),
('2017-07-21',147.969849),
('2017-07-24',149.761978),
('2017-07-25',150.402039),
('2017-07-26',151.111023),
('2017-07-27',148.255402),
('2017-07-28',147.211624),
('2017-07-31',146.4534),
('2017-08-01',147.753204),
('2017-08-02',154.734665),
('2017-08-03',153.188705),
('2017-08-04',153.99614),
('2017-08-07',156.37912),
('2017-08-08',157.629684),
('2017-08-09',158.594666),
('2017-08-10',153.543152),
('2017-08-11',155.678436),
('2017-08-14',158.021317),
('2017-08-15',159.751297),
('2017-08-16',159.108719),
('2017-08-17',156.054092),
('2017-08-18',155.698212),
('2017-08-21',155.41153),
('2017-08-22',157.952118),
('2017-08-23',158.149826),
('2017-08-24',157.447952),
('2017-08-25',158.031189),
('2017-08-28',159.622787),
('2017-08-29',161.046326),
('2017-08-30',161.481277),
('2017-08-31',162.12384),
('2017-09-01',162.173279),
('2017-09-05',160.225815),
('2017-09-06',160.057755),
('2017-09-07',159.415192),
('2017-09-08',156.815277),
('2017-09-11',159.652451),
('2017-09-12',159.01976),
('2017-09-13',157.823593),
('2017-09-14',156.469269),
('2017-09-15',158.050964),
('2017-09-18',156.854813),
('2017-09-19',156.914124),
('2017-09-20',154.284561),
('2017-09-21',151.635208),
('2017-09-22',150.15239),
('2017-09-25',148.827698),
('2017-09-26',151.388062),
('2017-09-27',152.465607),
('2017-09-28',151.526474),
('2017-09-29',152.356857),
('2017-10-02',152.050415),
('2017-10-03',152.712738),
('2017-10-04',151.724197),
('2017-10-05',153.612335),
('2017-10-06',153.523361),
('2017-10-09',154.057175),
('2017-10-10',154.116486),
('2017-10-11',154.759079),
('2017-10-12',154.215363),
('2017-10-13',155.194031),
('2017-10-16',158.050964),
('2017-10-17',158.634216),
('2017-10-18',157.932343),
('2017-10-19',154.195572),
('2017-10-20',154.462494),
('2017-10-23',154.383408),
('2017-10-24',155.302765),
('2017-10-25',154.620682),
('2017-10-26',155.609222),
('2017-10-27',161.184708),
('2017-10-30',164.812729),
('2017-10-31',167.106186),
('2017-11-01',164.980774),
('2017-11-02',166.186829),
('2017-11-03',170.526596),
('2017-11-06',172.256577),
('2017-11-07',172.810165),
('2017-11-08',174.223831),
('2017-11-09',173.86795),
('2017-11-10',173.292511),
('2017-11-13',172.598022),
('2017-11-14',169.988754),
('2017-11-15',167.746582),
('2017-11-16',169.750671),
('2017-11-17',168.808151),
('2017-11-20',168.639496),
('2017-11-21',171.774567),
('2017-11-22',173.580231),
('2017-11-24',173.590149),
('2017-11-27',172.717072),
('2017-11-28',171.705139),
('2017-11-29',168.143433),
('2017-11-30',170.494751),
('2017-12-01',169.70105),
('2017-12-04',168.460922),
('2017-12-05',168.30217),
('2017-12-06',167.677139),
('2017-12-07',167.984711),
('2017-12-08',168.034302),
('2017-12-11',171.308289),
('2017-12-12',170.345932),
('2017-12-13',170.911438),
('2017-12-14',170.861832),
('2017-12-15',172.598022),
('2017-12-18',175.028717),
('2017-12-19',173.163528),
('2017-12-20',172.975037),
('2017-12-21',173.629822),
('2017-12-22',173.629822),
('2017-12-26',169.224838),
('2017-12-27',169.254608),
('2017-12-28',169.73082),
('2017-12-29',167.895416),
('2018-01-02',170.901505),
('2018-01-03',170.871735),
('2018-01-04',171.665436),
('2018-01-05',173.619904),
('2018-01-08',172.975037),
('2018-01-09',172.9552),
('2018-01-10',172.915497),
('2018-01-11',173.897705),
('2018-01-12',175.69342),
('2018-01-16',174.800537),
('2018-01-17',177.687576),
('2018-01-18',177.846313),
('2018-01-19',177.052628),
('2018-01-22',175.604141),
('2018-01-23',175.643814),
('2018-01-24',172.846054),
('2018-01-25',169.760574),
('2018-01-26',170.157425),
('2018-01-29',166.635422),
('2018-01-30',165.653244),
('2018-01-31',166.109604),
('2018-02-01',166.456848),
('2018-02-02',159.234253),
('2018-02-05',155.25589),
('2018-02-06',161.744293),
('2018-02-07',158.281815),
('2018-02-08',153.926437),
('2018-02-09',155.809189),
('2018-02-12',162.084991),
('2018-02-13',163.708725),
('2018-02-14',166.727081),
('2018-02-15',172.3255),
('2018-02-16',171.767639),
('2018-02-20',171.18988),
('2018-02-21',170.412872),
('2018-02-22',171.837372),
('2018-02-23',174.825851),
('2018-02-26',178.282532),
('2018-02-27',177.704758),
('2018-02-28',177.435791),
('2018-03-01',174.327774),
('2018-03-02',175.533142),
('2018-03-05',176.140793),
('2018-03-06',175.991364),
('2018-03-07',174.357666),
('2018-03-08',176.26033),
('2018-03-09',179.288635),
('2018-03-12',181.021957),
('2018-03-13',179.278687),
('2018-03-14',177.754562),
('2018-03-15',177.963745),
('2018-03-16',177.336182),
('2018-03-19',174.626633),
('2018-03-20',174.566864),
('2018-03-21',170.612106),
('2018-03-22',168.201401),
('2018-03-23',164.306427),
('2018-03-26',172.106354),
('2018-03-27',167.693359),
('2018-03-28',165.8405),
('2018-03-29',167.135513),
('2018-04-02',166.039734),
('2018-04-03',167.743164),
('2018-04-04',170.950806),
('2018-04-05',172.13623),
('2018-04-06',167.733215),
('2018-04-09',169.39679),
('2018-04-10',172.584503),
('2018-04-11',171.777618),
('2018-04-12',173.471085),
('2018-04-13',174.058807),
('2018-04-16',175.144638),
('2018-04-17',177.555328),
('2018-04-18',177.15686),
('2018-04-19',172.13623),
('2018-04-20',165.08342),
('2018-04-23',164.60527),
('2018-04-24',162.314102),
('2018-04-25',163.021362),
('2018-04-26',163.589188),
('2018-04-27',161.696487),
('2018-04-30',164.625183),
('2018-05-01',168.450439),
('2018-05-02',175.891754),
('2018-05-03',176.21051),
('2018-05-04',183.123856),
('2018-05-07',184.448746),
('2018-05-08',185.335327),
('2018-05-09',186.640305),
('2018-05-10',189.309998),
('2018-05-11',188.589996),
('2018-05-14',188.149994),
('2018-05-15',186.440002),
('2018-05-16',188.179993),
('2018-05-17',186.990005),
('2018-05-18',186.309998),
('2018-05-21',187.630005),
('2018-05-22',187.160004),
('2018-05-23',188.360001),
('2018-05-24',188.149994),
('2018-05-25',188.580002),
('2018-05-29',187.899994),
('2018-05-30',187.5),
('2018-05-31',186.869995),
('2018-06-01',190.240005),
('2018-06-04',191.830002),
('2018-06-05',193.309998),
('2018-06-06',193.979996),
('2018-06-07',193.460007),
('2018-06-08',191.699997),
('2018-06-11',191.229996),
('2018-06-12',192.279999),
('2018-06-13',190.699997),
('2018-06-14',190.800003),
('2018-06-15',188.839996),
('2018-06-18',188.740005),
('2018-06-19',185.690002),
('2018-06-20',186.5),
('2018-06-21',185.460007),
('2018-06-22',184.919998),
('2018-06-25',182.169998),
('2018-06-26',184.429993),
('2018-06-27',184.160004),
('2018-06-28',185.5),
('2018-06-29',185.110001),
('2018-07-02',187.179993),
('2018-07-03',183.919998),
('2018-07-05',185.399994),
('2018-07-06',187.970001),
('2018-07-09',190.580002),
('2018-07-10',190.350006);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM