ID TELEFON CULOARE piesa1 piesa2 piesa3 piesa4
1 telefon1 culoare1 0N 0N 0N 0N
1 telefon1 culoare1 14Y 0N 0N 0N
2 telefon2 culoare2 0N 8Y 0N 0N
2 telefon2 culoare2 0N 0N 4Y 0N
3 telefon3 culoare3 0N 0N 0N 0Y
3 telefon3 culoare3 0N 0N 0N 0N
3 telefon3 culoare3 0N 0N 0N 0N
3 telefon3 culoare3 0N 0N 5Y 0N
4 telefon4 Neutru 8N 0N 0N 0N
4 telefon4 Neutru 0N 0N 1N 0N
4 telefon4 Neutru 0N 0N 0N 7Y
我有一个这样的结果集,但是我想以某种方式合并ID列重复的行,并且只留下值结束的单元格' Y'或者使用' N'但是前面的数字大于0。
我设法只通过添加SUM
agg函数和按行的唯一标识符分组来对数字执行此操作,在我的情况下将是TELEFON + CULOARE列。
查询此结果:
SELECT tt.id as `#`
,t.telefon as TELEFON
, IFNULL(c.culoare,'Neutru') as CULOARE
, (IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa1
,(IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa2
,(IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa3
,(IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa4
,tt.total_piese
,tt.is_stoc_min
FROM telefon as t LEFT JOIN
total_piese as tt
ON t.id = tt.id_telefon
LEFT JOIN culoare as c
ON c.id = tt.id_culoare
LEFT JOIN piesa as p
ON p.id = tt.id_piesa
ORDER BY t.id;
我的愿望结果是这样的:
TELEFON CULOARE piesa1 piesa2 piesa3 piesa4
telefon1 culoare1 14Y 0N 0N 0N
telefon2 culoare2 0N 8Y 4Y 0N
telefon3 culoare3 0N 0N 5Y 0Y
telefon4 Neutru 8N 0N 1N 7Y
我有一个返回合并行的查询,但没有' Y'或者' N'在价值的最后:
SELECT tt.id as `#`
,t.telefon as TELEFON
, IFNULL(c.culoare,'Neutru') as CULOARE
,SUM(IF(p.piesa = 'piesa1', tt.total_piese,0)) AS piesa1
,SUM(IF(p.piesa = 'piesa2', tt.total_piese,0)) AS piesa2
,SUM(IF(p.piesa = 'piesa3', tt.total_piese,0)) AS piesa3
,SUM(IF(p.piesa = 'piesa4',tt.total_piese,0)) AS piesa4
,tt.stoc_min
FROM telefon as t LEFT JOIN
total_piese as tt
ON t.id = tt.id_telefon
LEFT JOIN culoare as c
ON c.id = tt.id_culoare
LEFT JOIN piesa as p
ON p.id = tt.id_piesa
GROUP BY t.Telefon,c.Culoare
ORDER BY t.id;
答案 0 :(得分:1)
嗯,给你一个更好(或更优化)的答案有点困难,因为我们没有&#34;基本数据&#34;,但已经是结果。< / p>
无论如何,您可以在piesa字段上使用max
将您的第一次尝试用作子查询。 (因为任何数字都将大于0,并且Y大于N,因此MAX
应该为您提供所需的输出)
select id as `#`, telefon, culoare,
max(piesa1) as piesa1,
max(piesa2) as piesa2,
max(piesa3) as piesa3,
max(piesa4) as piesa4
from
(SELECT
tt.id
,t.telefon as TELEFON
,IFNULL(c.culoare,'Neutru') as CULOARE
,(IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa1
,(IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa2
,(IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa3
,(IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N')) AS piesa4
,tt.total_piese
,tt.is_stoc_min
FROM telefon as t LEFT JOIN
total_piese as tt ON t.id = tt.id_telefon
LEFT JOIN culoare as c ON c.id = tt.id_culoare
LEFT JOIN piesa as p ON p.id = tt.id_piesa) s
GROUP BY id, telefon, culoare
ORDER BY Id
或可能(未经测试)直接在查询中使用max
SELECT tt.id as `#`
,t.telefon as TELEFON
,IFNULL(c.culoare,'Neutru') as CULOARE
,max((IF(p.piesa = 'piesa1', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa1
,max((IF(p.piesa = 'piesa2', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa2
,max((IF(p.piesa = 'piesa3', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa3
,max((IF(p.piesa = 'piesa4', CONCAT(tt.total_piese,tt.is_stoc_min),'0N'))) AS piesa4
FROM telefon as t LEFT JOIN
total_piese as tt
ON t.id = tt.id_telefon
LEFT JOIN culoare as c
ON c.id = tt.id_culoare
LEFT JOIN piesa as p
ON p.id = tt.id_piesa
GROUP BY tt.Id, t.telefon, c.culoare
ORDER BY t.id;