2

我有一个包含名称、color1、color2、color3 和其他列(SQLite)的表:

CREATE TABLE data (name TEXT, lvlavailable INTEGER, lvlExtra TEXT,
color1 TEXT, color2 TEXT, color3 TEXT);

当前数据

INSERT INTO "someTable" VALUES ("name","Amarillo","NoColor","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","NoColor","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Azul","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Azul","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Rojo","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Rojo","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Rojo","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Rojo","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Rojo","Verde");
INSERT INTO "someTable" VALUES ("name","Amarillo","Verde","NoColor");
INSERT INTO "someTable" VALUES ("name","Amarillo","Violeta","NoColor");
INSERT INTO "someTable" VALUES ("name","Azul","NoColor","NoColor");
INSERT INTO "someTable" VALUES ("name","Azul","NoColor","NoColor");
INSERT INTO "someTable" VALUES ("name","Azul","Amarillo","NoColor");
INSERT INTO "someTable" VALUES ("name","Azul","Amarillo","NoColor");
INSERT INTO "someTable" VALUES ("name","Azul","Amarillo","NoColor");
INSERT INTO "someTable" VALUES ("name","Azul","Amarillo","Rojo");

在这种情况下,列名是无关紧要的 *我知道插入不正确,因为列数,但我想你明白了*

当前选择语句:

SELECT name, 
       color1, 
       CASE WHEN color2 = 'NoColor' THEN '' ELSE color2 END as color2, 
       CASE WHEN color3 = 'NoColor' THEN '' ELSE color3 END as color3 
 FROM someTable 
 GROUP BY color1,color2,color3,name

案例用于其他目的

目前的结果:

name    Amarillo        
name    Amarillo        
name    Amarillo    Azul    
name    Amarillo    Azul    
name    Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo            Verde
name    Amarillo    Verde   
name    Amarillo    Violeta 
name    Azul        
name    Azul        
name    Azul            Amarillo    
name    Azul            Amarillo    
name    Azul            Amarillo    
name    Azul            Amarillo    Rojo

颜色的名称是西班牙语,对此感到抱歉

期望的结果:

name    Amarillo        
name    Amarillo        
name    Amarillo    Azul    
name    Amarillo    Azul
name    Azul            Amarillo    
name    Azul            Amarillo    
name    Azul            Amarillo
name    Azul            Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo    
name    Amarillo    Rojo            Verde
name    Amarillo    Verde   
name    Amarillo    Violeta 
name    Azul        
name    Azul        

请注意,当它获得组 Amarillo,Azul,[empty] 时,它将继续使用 Amarillo,Azul 并在每个颜色的列中独立,但我想保持原始颜色列位置,如图所示,我不想要创建另一个列,刚刚订购。

所以,我想要的是把那些颜色相同但顺序不同的组放在一起。

那可能吗?

4

2 回答 2

1

如果我做对了:

select * from table1 
where 
 'red' in (color1,color2,color3) 
  and
 'blue' in (color1,color2,color3) 

好的,这是对您的数据的组查询:

SQLFiddle 演示

select *

from someTable
group by 
(select 
  group_concat(c1)
  from
  (
  select c1 from
  (  
  select color1 as c1
  union all
  select color2 as c1
  union all  
  select color3 as c1 
  ) t2
  order by c1  
  ) t1
)
于 2013-04-22T13:17:22.490 回答
1

尝试:

select name, 
       color1, 
       case color2 when 'NoColor' then '' else color2 end as colour2, 
       case color3 when 'NoColor' then '' else color3 end as colour3
from data
order by
       case 
           when color2 = 'NoColor' then color1 
           when color3 = 'NoColor' then min(color1, color2)
           else min(color1,color2,color3)
       end,
       case
           when color2 = 'NoColor' then ''
           when color3 = 'NoColor' then max(color1, color2)
           else max(min(color1,color2),min(color1,color3),min(color2,color3))
       end,
       case color3
           when 'NoColor' then ''
           else max(color1,color2,color3)
       end

SQLFiddle在这里

于 2013-04-22T14:30:55.780 回答