Search
 
SCRIPT & CODE EXAMPLE
 

SQL

BigQuery: join 2 tables but only choosing rows based on date column

--  ---------------------------------------------------------------------------------
--  create dummy NEW table 
--  ---------------------------------------------------------------------------------
WITH
  table_new_data AS (
  SELECT
    '2022-01' AS date,
    't1' AS type,
    0 AS DATA
  UNION ALL
  SELECT
    '2022-03',
    't2' ,
    1 
  UNION ALL
  SELECT
    '2021-08' ,
    't1' ,
    1  ),
--  ---------------------------------------------------------------------------------
--  create dummy OLD table 
--  ---------------------------------------------------------------------------------
  table_old_data AS (
  SELECT
    '2021-10' AS date,
    't1' AS type,
    2 AS DATA
  UNION ALL
  SELECT
    '2022-04',
    't2',
    3 
  UNION ALL
  SELECT
    '2021-07',
    't1',
    4
  UNION ALL
  SELECT
    '2021-06',
    't1',
    5),
--  ---------------------------------------------------------------------------------
--  create joined tables based on dates from old table being LOWER (may need <=??)
--  create order = ROW_NUMBER() function to see which date is closest from old table
--  make sure to test on edge cases where dates are the same or equal to
--  ---------------------------------------------------------------------------------
  ordered AS (
  SELECT
    nd.date AS new_date,
    nd.type,
    nd.DATA AS new_data,
    od.date AS old_date,
    od.DATA AS old_data,
    ROW_NUMBER() OVER(PARTITION BY nd.type, nd.date ORDER BY nd.date ) AS rn
  FROM
    table_new_data nd
  LEFT JOIN
    table_old_data od
  ON
    nd.type = od.type
    AND od.date < nd.date )
--  ---------------------------------------------------------------------------------
--  final table to reproduce desired output in question
--  ---------------------------------------------------------------------------------
SELECT
  * EXCEPT(rn)
FROM
  ordered
WHERE
  rn = 1
Comment

PREVIOUS NEXT
Code Example
Sql :: não é possível executar uma operação DML dentro de uma consulta 
Sql :: sql examples from your work 
Sql :: VHDL Example Code of Record Type 
Sql :: oracle_home sqlplus 
Sql :: SQL TABLE : SUBSCRIPTION, PRODUCT, SPECIFICATION 
Sql :: soql queries for not contact related account records in salesforce 
Sql :: how do you execute the fragment or sqlBatch using scriptdom 
Sql :: mysql does collate nopad recognize space 
Sql :: how to add column in oracle 
Sql :: sub blocks in sql 
Sql :: combine islands dates sql 
Sql :: join creating duplicate columns sqllite 
Sql :: Sql runnignsum 
Sql :: SQL FULL OUTER JOIN With AS Alias 
Sql :: ring MySQL get the inserted row id 
Sql :: creating directory /var/lib/postgresql/data ... initdb: error: could not create directory "/var/lib/postgres/data": Permission denied 
Sql :: SQLSTATE[HY000] [1298] Unknown or incorrect time zone 
Sql :: how to delete a database record after a certain time 
Sql :: FILENAME /usr/bin/mysql does not exists. Make sure correct path is set in /etc/dump admin/settings.conf. 
Sql :: convert sqlalchemy.util._collections to list of string 
Sql :: SQL AND, OR and NOT Operators 
Sql :: how many columns can be used for creating index? 
Sql :: PBI TO SQL 
Sql :: cahnge column name apex oracle 
Sql :: SQL IN Operator With Columns 
Sql :: sqlite closes value 
Sql :: sqlite escape single quote 
Sql :: Failed to initialize, mariadb service is unhealthy. 
Sql :: java mysql swing example 
Sql :: mysql primary vs unique 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =