【发布时间】:2014-11-24 10:44:38
【问题描述】:
我有以下疑问:
SELECT Number, Concat(Product,' ',Division) as 'Product',
count(*) as 'COUNT', SUM(tta) as 'TTA', ROUND(SUM(tta) / count(*),2) as 'AVG'
FROM cdrdata cdr
LEFT JOIN products p ON p.Number = cdr.calling
LEFT JOIN divisions d ON d.id = p.DivisionID
WHERE CustomerID = 32 AND p.Status = 1
Group by calling
ORDER BY Product;
结果:
从此:
Product1 DIvision1
Product1 Division2
Product2 Division3
Product3 Division4
到这里:
Product1 Division1
Product1 Division2
Product2
Product3
问:如何修改我的查询,以便仅在有重复的Products 的地方连接Product 和Division 并显示重复的内容,如上例所示? p>
【问题讨论】:
-
可能会在
group by之后添加一个having 子句作为having count(*) > 1请注意group by需要在product上 -
我还需要显示重复项,只是为了仅在重复项上连接
-
试试
case when count(*) > 1 then Concat(Product,' ',Division) else Product end as 'Product' -
那行不通...
-
@alex:它确实有效,您可能需要使用a derived table。
标签: mysql mysql-workbench