开发者

GROUP_CONCAT with additional grouping?

I have started on a script to generate statistics of how 开发者_如何学Pythonoften and when a particular article is called using

SELECT `title`, `page_id`, COUNT(*) AS `total`, 
GROUP_CONCAT(DISTINCT `date_created` 
             ORDER BY `date_created` SEPARATOR ',') dates 
FROM `statistics` 
WHERE `supplier_id` = '27' 
GROUP BY `title` 
ORDER BY `title`;

which produces

[0] => Array
        (
            [0] => Array
                (
                    [total] => 3
                    [dates] => 2011-04-26,2011-04-27
                )

            [statistics] => Array
                (
                    [title] => Title 2
                    [page_id] => 2
                )  
        )

[1] => Array
        (
            [0] => Array
                (
                    [total] => 6
                    [dates] => 2011-04-26,2011-04-27,2011-04-28
                )

            [statistics] => Array
                (
                    [title] => Title 7
                    [page_id] => 7
                )
        )

Good stuff. But how can I add a field similar to dates which adds a breakdown of ids grouped by date_created thus creating something like:

[2] => Array
    (
        [0] => Array
            (
                [total] => 6
                [dates] => 2011-04-26,2011-04-27,2011-04-28
                [total_by_date] => 2,1,3
            )

        [statistics] => Array
            (
                [title] => Title 7
                [page_id] => 7
            )
)

I would like to add an extra GROUP BY along these lines:

GROUP_CONCAT(DISTINCT COUNT(*) AS `total_by_date` 
             GROUP BY `date_created` ORDER BY `date_created` SEPARATOR ',')

but it's not working, how to I fix this?

DUMP:

CREATE TABLE `statistics` (
  `id` int(11) NOT NULL auto_increment,
  `pagetype` varchar(50) character set utf8 collate utf8_unicode_ci NOT NULL,
  `supplier_id` int(11) NOT NULL,
  `page_id` int(11) default NULL,
  `title` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  `date_created` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=920 ;

-- 
-- Dumping data for table `statistics`
-- 

INSERT INTO `statistics` (`id`, `pagetype`, `supplier_id`, `page_id`, `title`, `date_created`) VALUES 
(1, 'newsarticle', 27, 2751, 'Title 1', '2011-04-26'),
(2, 'newsarticle', 27, 2751, 'Title 1', '2011-04-26'),
(3, 'newsarticle', 27, 2751, 'Title 1', '2011-04-27'),
(4, 'newsarticle', 27, 462009, 'Title 2', '2011-04-26'),
(5, 'newsarticle', 27, 462009, 'Title 2', '2011-04-26'),
(6, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(7, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(8, 'newsarticle', 27, 462009, 'Title 2', '2011-04-27'),
(9, 'newsarticle', 27, 462009, 'Title 2', '2011-04-28'),
(10, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(11, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(12, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(13, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26'),
(14, 'newsarticle', 27, 46200, 'Title 3', '2011-04-26');


Not 100% sure this is gonna work, but it's a start:

Do the 2nd GROUP_CONCAT in a sub-query.

SELECT title
  , s1.page_id
  , s2.total_by_date
  , GROUP_CONCAT(DISTINCT s1.date_created
                 ORDER BY s1.date_created SEPARATOR ',') as dates  
FROM statistics s1
INNER JOIN  
  (SELECT page_id
     ,GROUP_CONCAT(/*DISTINCT*/ gc.cnt
     ORDER BY gc.date_created SEPARATOR ',') AS total_by_date
   FROM 
   (
      SELECT count(*) as cnt
        ,date_created
      FROM statistics
      WHERE supplier_id = '27'
      GROUP BY date_created
   ) gc
   GROUP BY gc.date_created
  ) s2 ON (s1.page_id = s2.page_id)      
WHERE supplier_id = '27'  
GROUP BY title  
ORDER BY title; 

Using distinct count(*) would hide items with the same count, which is not I think you want, so I've put it in in commented form.
BTW the ORDER BY title is not needed here, because GROUP BY already sorts on title.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜