我有一个包含name,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,[空]时,它将继续使用Amarillo,Azul与wich列中的每个颜色相互独立,但我想保持原始颜色列位置,如图所示,我不要我想创建另一个专栏,只是订购。
所以,我想要的是让那些具有相同颜色但不同顺序的组合在一起。
这可能吗?
答案 0 :(得分:1)
如果我做得对:
select * from table1
where
'red' in (color1,color2,color3)
and
'blue' in (color1,color2,color3)
好的,这里是您的数据的群组查询:
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
)
答案 1 :(得分: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 here。