2014-09-24 150 views
0

我被計數列上的mysql ROLLUP卡住了。你能指導我嗎?與計數的mysql彙總

我寫下面的MySQL查詢

查詢:

SELECT (SELECT store.StoreName from store 
      WHERE store.storeOwnerId=redeem.StoreNo) 
      AS CompanyName, 
     (SELECT mscheme.SchemeName from mscheme 
      WHERE mscheme.SchemeNo = redeem.SchemeNo) 
      AS SchemeName, 
count(*) AS Count 
FROM kirana_app.redeem GROUP BY redeem.SchemeNo 
ORDER BY CompanyName; 

** **輸出

CompanyName,SchemeName, Count 
'Dwarkesh', 'September-saffola', '25' 
'Dwarkesh', 'September-Tea', '20' 
'Nitin stores', 'Testing Scheme', '1' 
'Nitin stores', 'September-Parle', '63' 

我需要與計數列捲起像

輸出如:

CompanyName,SchemeName, Count 
'Dwarkesh', 'September-saffola', '25' 
'Dwarkesh', 'September-Tea', '20' 
**Dwarkesh', NULL, '45'** 
'Nitin stores', 'Testing Scheme', '1' 
'Nitin stores', 'September-Parle', '63' 
**'Nitin stores', NULL, '64**' 
+0

您是否嘗試過「GROUP BY CompanyName」? – JazyK 2014-09-24 05:58:38

回答

0

學會做適當的連接而不是子查詢。

SELECT s.StoreName AS CompanyName, 
m.SchemeName AS SchemeName, 
COUNT(*) AS Count 
FROM redeem r 
INNER JOIN store s ON r.StoreNo = s.storeOwnerId 
INNER JOIN mscheme m ON m.SchemeNo = r.SchemeNo 
GROUP BY s.CompanyName, m.SchemeNo 
WITH ROLLUP; 

ORDER BY是隱含GROUP BY完成。