通过 VLOOKUP 查询清理 SQL 中的数据
Clean data in SQL Query through VLOOKUP
我有一个查询可以提取以下列中的销售数据:
- 购买日期
- 产品编号
- 产品名称
- 价格
但是,在业务发展初期,一些产品 ID 的输入格式有误。
目前我有一个 Excel 文件,其中 A 列中的 old/wrong ID 和 B 列中的正确 ID。
我想知道是否有一种方法可以编写一个查询来查看产品 ID 是否在列表的 A 列中。如果为真,它将 return 来自 B 列的正确 ID,如果为假,它将 return 来自数据库的产品 ID。
在 Excel 中,我会按以下方式进行:=IFERROR(VLOOKUP(Product ID,ID Fix SheetA:B,2,FALSE),Product ID)
示例数据
# Created On Item IDs Product Name Price
1 26/02/2018 10:51 ABC1 Product Name 1 99
2 26/02/2018 10:22 G7781 Product Name 2 1299
3 26/02/2018 10:23 L5303 Product Name 3 165
4 26/02/2018 10:24 G9576 Product Name 4 1999
5 26/02/2018 10:26 ABC2 Product Name 5 99
6 26/02/2018 10:30 GGG1 Product Name 6 469
7 26/02/2018 10:37 T1283 Product Name 7 299
8 26/02/2018 10:42 L4505 Product Name 8 329
9 26/02/2018 10:48 L3007 Product Name 9 99
如您所见,我们使用 L、G 或 T 后跟产品编号。
我们的三个垂直领域各有一个字母。
但是,有些早期已经添加了ABC或GGG前缀。
Column A Column B
ABC1 L886
ABC2 L5632
GGG1 G7268
这就是为什么我有这个额外的文件,在 A 列中有 old/incorrect 个 ID,在 B 列中有正确的 ID。
我想要的结果是按垂直(ID 以 L、T 或 G 开头)对销售数据进行分组。但在此之前,我需要进行某种查找以修复不正确的项目 ID。
如果数据库中的值正确
drop table if exists t,t1;
create table t( id int, itemid varchar(20));
insert into t values
(1 , 'ABC1' ),
(2 , 'G7781'),
(3 , 'L5303'),
(4 , 'G9576'),
(5 , 'ABC2' ),
(6 , 'GGG1' ),
(7 , 'T1283'),
(8 , 'L4505'),
(9 , 'L3007');
create table t1(Columna varchar(20),Columnb varchar(20));
insert into t1 values
( 'ABC1' , 'L886'),
( 'ABC2' , 'L5632'),
( 'GGG1' , 'G7268');
select left(
case when t1.columnb is not null then t1.columnb
else t.itemid
end ,1) vertical,
count(*) obs
from t
left join t1 on t1.columna = t.itemid
group by left(case when t1.columnb is not null then t1.columnb
else t.itemid
end ,1);
+----------+-----+
| vertical | obs |
+----------+-----+
| G | 3 |
| L | 5 |
| T | 1 |
+----------+-----+
3 rows in set (0.00 sec)
我有一个查询可以提取以下列中的销售数据:
- 购买日期
- 产品编号
- 产品名称
- 价格
但是,在业务发展初期,一些产品 ID 的输入格式有误。 目前我有一个 Excel 文件,其中 A 列中的 old/wrong ID 和 B 列中的正确 ID。
我想知道是否有一种方法可以编写一个查询来查看产品 ID 是否在列表的 A 列中。如果为真,它将 return 来自 B 列的正确 ID,如果为假,它将 return 来自数据库的产品 ID。
在 Excel 中,我会按以下方式进行:=IFERROR(VLOOKUP(Product ID,ID Fix SheetA:B,2,FALSE),Product ID)
示例数据
# Created On Item IDs Product Name Price
1 26/02/2018 10:51 ABC1 Product Name 1 99
2 26/02/2018 10:22 G7781 Product Name 2 1299
3 26/02/2018 10:23 L5303 Product Name 3 165
4 26/02/2018 10:24 G9576 Product Name 4 1999
5 26/02/2018 10:26 ABC2 Product Name 5 99
6 26/02/2018 10:30 GGG1 Product Name 6 469
7 26/02/2018 10:37 T1283 Product Name 7 299
8 26/02/2018 10:42 L4505 Product Name 8 329
9 26/02/2018 10:48 L3007 Product Name 9 99
如您所见,我们使用 L、G 或 T 后跟产品编号。 我们的三个垂直领域各有一个字母。 但是,有些早期已经添加了ABC或GGG前缀。
Column A Column B
ABC1 L886
ABC2 L5632
GGG1 G7268
这就是为什么我有这个额外的文件,在 A 列中有 old/incorrect 个 ID,在 B 列中有正确的 ID。
我想要的结果是按垂直(ID 以 L、T 或 G 开头)对销售数据进行分组。但在此之前,我需要进行某种查找以修复不正确的项目 ID。
如果数据库中的值正确
drop table if exists t,t1;
create table t( id int, itemid varchar(20));
insert into t values
(1 , 'ABC1' ),
(2 , 'G7781'),
(3 , 'L5303'),
(4 , 'G9576'),
(5 , 'ABC2' ),
(6 , 'GGG1' ),
(7 , 'T1283'),
(8 , 'L4505'),
(9 , 'L3007');
create table t1(Columna varchar(20),Columnb varchar(20));
insert into t1 values
( 'ABC1' , 'L886'),
( 'ABC2' , 'L5632'),
( 'GGG1' , 'G7268');
select left(
case when t1.columnb is not null then t1.columnb
else t.itemid
end ,1) vertical,
count(*) obs
from t
left join t1 on t1.columna = t.itemid
group by left(case when t1.columnb is not null then t1.columnb
else t.itemid
end ,1);
+----------+-----+
| vertical | obs |
+----------+-----+
| G | 3 |
| L | 5 |
| T | 1 |
+----------+-----+
3 rows in set (0.00 sec)