以下查询真的很慢,有人可以帮助我加快速度吗?
SELECT DISTINCT m.Item, switch(
(SELECT COUNT(B1.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],30) FROM [Lista de objetos] as b) AS B1 WHERE B1.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],30),
(SELECT COUNT(B2.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],27) FROM [Lista de objetos] as b) AS B2 WHERE B2.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],27),
(SELECT COUNT(B3.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],24) FROM [Lista de objetos] as b) AS B3 WHERE B3.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],24),
(SELECT COUNT(B4.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],21) FROM [Lista de objetos] as b) AS B4 WHERE B4.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],21),
(SELECT COUNT(B5.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],18) FROM [Lista de objetos] as b) AS B5 WHERE B5.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],18),
(SELECT COUNT(B6.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],15) FROM [Lista de objetos] as b) AS B6 WHERE B6.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],15),
(SELECT COUNT(B7.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],11) FROM [Lista de objetos] as b) AS B7 WHERE B7.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],11),
(SELECT COUNT(B8.Item) FROM(SELECT DISTINCT b.Item, Left(b.[Local Instalacao],6) FROM [Lista de objetos] as b) AS B8 WHERE B8.ITEM=M.ITEM) =1,Left(m.[Local Instalacao],6),
true,"") AS localSugerido
FROM [Lista de objetos] AS m
ORDER BY m.ITEM;
是否可以创建一个中间表并在查询执行结束后删除它?
修改
数据样本:
+---------+--------------------------+
| Item | Local Instalacao |
+---------+--------------------------+
| 1549821 | 301095.FEDN.PNN.01.04.01 |
| 1549821 | 301095.FEDN.PNN.01.10.01 |
| 1549821 | 301095.FEDN.PNN.01.46.01 |
| 1549821 | 301095.FEDN.MCB.01.49.01 |
| 1549822 | 301095.FEDN.PNN.01.01 |
| 1549822 | 301095.FEDN.PNN.01.43.01 |
| 1549823 | 301095.FEDN.PNN.03.04.01 |
| 1549823 | 301095.XXXX.PNN.03.37.01 |
+---------+--------------------------+
预期结果
每个项目的共同因素
+---------+--------------------+
| Item | Local Instalacao |
+---------+--------------------+
| 1549821 | 301095.FEDN |
| 1549822 | 301095.FEDN.PNN.01 |
| 1549823 | 301095 |
+---------+--------------------+