是否可以使用SQLlite将表转换为其他格式?

时间:2019-05-28 14:30:53

标签: sqlite

我正在尝试将这张表中的表格转换为其他格式。

图片:

Image

以下是数据。我的数据库中有一张表,其列标题当前定义为col_0 ... col_7。

+----------+------------------+-------------+--------------+-------------+--------------+-------------+--------------+
|  col_0   |      col_1       |    col_2    |    col_3     |    col_4    |    col_5     |    col_6    |    col_7     |
+----------+------------------+-------------+--------------+-------------+--------------+-------------+--------------+
|          |                  | Spoon       |              | Fork        |              | Knife       |              |
| Location | Market Postcode  | Trade Price | Retail Price | Trade Price | Retail Price | Trade Price | Retail Price |
| Market A | AAA111           | 0.5         | 1            | 0.6         | 1.1          | 0.55        | NA           |
| Market B | BBB111           | 0.7         | 2            | 0.8         | 1.5          | NA          | NA           |
+----------+------------------+-------------+--------------+-------------+--------------+-------------+--------------+ 

我希望以一种能够看到市场A和B的每种产品的贸易和零售价格的方式进行转换。

+----------+------------------+---------+-------------+--------------+
|  col_0   |      col_1       |  col_2  |    col_3    |    col_4     |
+----------+------------------+---------+-------------+--------------+
| Location | Market Post Code | Product | Trade Price | Retail Price |
| Market A | AAA111           | Spoon   | 0.5         | 1            |
| Market A | AAA111           | Fork    | 0.6         | 1.1          |
| Market A | AAA111           | Knife   | 0.55        | NA           |
| Market B | BBB111           | Spoon   | 0.7         | 2            |
| Market B | BBB111           | Fork    | 0.8         | 1.5          |
| Market B | BBB111           | Knife   | NA          | NA           |
+----------+------------------+---------+-------------+--------------+

我对SQL和SQL lite还是很陌生,如果有人可以帮助我,那会很好吗?

谢谢

1 个答案:

答案 0 :(得分:0)

是的,但是很尴尬,下面是一个示例:-

-- Create the original data
DROP TABLE IF EXISTS original;
CREATE TABLE IF NOT EXISTS original (COL_0 TEXT, COL_1 TEXT, COL_2 TEXT, COL_3 TEXT, COL_4 TEXT, COL_5 TEXT, COL_6 TEXT, COL_7 TEXT);
INSERT INTO original VALUES
    (null,null,'Spoon','','Fork','','Knife',''),
    ('Location','Market Postocde','Trade Price','Retail Price','Trade Price','Retail Price','Trade Price','Reatail Price'),
    ('Market A','AAA111',0.5,1,0.6,1.1,0.55,'NA'),
    ('Market B','BBB111',0.7,2,0.8,1.5,'NA','NA')
;
SELECT * FROM original;

-- Prepare to create the transformed
DROP TABLE IF EXISTS transformed;
CREATE TABLE IF NOT EXISTS transformed (COL_0 TEXT, COL_1 TEXT, COL_2 TEXT, COL_3 TEXT, COL_4 TEXT);


WITH 
    item1 AS (SELECT COL_2 FROM original WHERE (length(COL_0) < 1 AND LENGTH(COL_1) < 1) OR (COL_0 IS NULL AND COL_1 IS NULL) ),
    item2 AS (SELECT COL_4 FROM original WHERE (length(COL_0) < 1 AND LENGTH(COL_1) < 1) OR (COL_0 IS NULL AND COL_1 IS NULL)),
    item3 AS (SELECT COL_6 FROM original WHERE (length(COL_0) < 1 AND LENGTH(COL_1) < 1) OR (COL_0 IS NULL AND COL_1 IS NULL)),

    full1 AS (SELECT COL_0, COL_1,(SELECT * FROM item1),COL_2,COl_3 FROM original WHERE length(COL_1) = 6),
    full2 AS (SELECT COL_0, COL_1,(SELECT * FROM item2),COL_4,COL_5 FROM original WHERE length(COL_1) = 6),
    full3 AS (SELECT COL_0, COL_1,(SELECT * FROM item3),COL_6,COL_7 FROM original WHERE length(COL_1) = 6),

    reformed AS (SELECT * FROM full1 UNION SELECT * FROM full2 UNION SELECT * FROM full3)

    INSERT INTO transformed SELECT * FROM reformed
;

SELECT * FROM transformed;

说明

阶段1-创建原始数据

根据注释创建原始数据,选择结果为:-

enter image description here

以上内容也已使用:-

运行

enter image description here

  • 即当该行包含两项时,col_0和COL_1可以为空白或为空。

阶段2-准备

下一阶段将创建一个称为transformed的表,该表中将接收转换后的数据(即具有5列的表)。

阶段3-转型

这将创建2组3个通用表表达式(即,有3个项目)和一个最终CTE(已更改),该CTE将第二组CTE合并,然后将其传递到INSERT到准备好的结果表中,命名为已转换

通过处理原始表并仅选择将要找到该项目的列(即COl_0和COL_1为空字符串或为空的行)来生成第一组,即仅是特定项目。

第二组获取包含与该项目相关的数据的列,以及从相应项目获得该项目的列? CTE。包含条件是市场代码的长度为6个字符。

改革后的CTE将第二组中的三个CTE合并为一个CTE。

最后,经过重组的CTE用于加载转换后的表。

已经假定不需要标题,因为通常不会将其存储为实际数据。

结果是:-

enter image description here