SQL data migration practice: from product level information to AB test table

Create table

First, two tables are defined in the code: dim_prod_hierarchy_info and app_abtest_product_info, both located in the test database.

  • The dim_prod_hierarchy_info table is used to store product hierarchy information and has three columns: hier_node_level_1_name, hier_node_level_2_name, hier_node_level_3_name. These columns store different hierarchical names of products.
  • The app_abtest_product_info table is used to store product information. It also has three columns: prod_level_1, prod_level_2, prod_level_3, which are similar to the column functions of the first table.

sql:

CREATE TABLE test.dim_prod_hierarchy_info (
    hier_node_level_1_name VARCHAR(255),
    hier_node_level_2_name VARCHAR(255),
    hier_node_level_3_name VARCHAR(255)
);

CREATE TABLE test.app_abtest_product_info (
    prod_level_1 VARCHAR(255),
    prod_level_2 VARCHAR(255),
    prod_level_3 VARCHAR(255)
);

Insert data

Next, the code inserts several rows of data into the dim_prod_hierarchy_info table through INSERT INTO statements. These data represent different categories of products and their tiers.

code show as below:

INSERT INTO test.dim_prod_hierarchy_info (hier_node_level_1_name, hier_node_level_2_name, hier_node_level_3_name) VALUES
('SmartPhone', 'Galaxy S21', 'S21 Ultra'),
('Tablet', 'Galaxy Tab', 'Tab S7'),
('SmartWatch', 'Galaxy Watch', 'Watch 3'),
('Earbuds', 'Galaxy Buds', 'Buds Live');

Clear data table

Before inserting data into the app_abtest_product_info table, the code uses a DELETE FROM statement to delete any existing data in the table. This ensures that there is no old data in the table.

DELETE FROM test.app_abtest_product_info WHERE 1=1;

Data migration and filtering

Finally, use the INSERT INTO … SELECT statement to select specific data rows from the dim_prod_hierarchy_info table and insert the data into the app_abtest_product_info table. This query selects only those rows that meet certain criteria: hier_node_level_1_name field must be one of the predefined product categories, and hier_node_level_3_name cannot be empty.

INSERT INTO test.app_abtest_product_info(prod_level_1, prod_level_2, prod_level_3)
SELECT hier_node_level_1_name, hier_node_level_2_name, hier_node_level_3_name
FROM test.dim_prod_hierarchy_info
WHERE hier_node_level_1_name IN ('SmartPhone', 'Tablet', 'SmartWatch', 'Earbuds')
   AND hier_node_level_3_name IS NOT NULL
GROUP BY hier_node_level_1_name, hier_node_level_2_name, hier_node_level_3_name;

Query data

Finally, use a simple SELECT * FROM query to view the data in the dim_prod_hierarchy_info table.

code show as below:

SELECT * FROM test.dim_prod_hierarchy_info;

in conclusion

Through this example, users can learn how to create and operate SQL tables, including how to insert, delete, and migrate data. This is an important skill in database management and data analysis that can be applied to a variety of real-world scenarios.

Guess you like

Origin blog.csdn.net/weixin_46211269/article/details/134533992