1
SELECT 
  Day, 
  month, 
  year,
  GROUP_CONCAT(total), 
  GROUP_CONCAT(SP_ID)
FROM
(
    SELECT 
      DATE_FORMAT(l.act_date, '%d') AS DAY, 
      DATE_FORMAT(l.act_date, '%M') AS MONTH,
      EXTRACT(YEAR FROM l.act_date) AS YEAR, 
      COUNT(*) as total,l.sp_id 
    FROM lead_activity2 as l
    right outer join salesperson as s on l.sp_id=s.sp_id
    WHERE l.act_name='scb' 
      AND ((l.act_date>='2012-09-07 13:03:27' ) 
      AND (l.act_date<= '2012-11-07 13:03:27')) 
    GROUP BY MONTH, YEAR, DAY, l.sp_id 
    ORDER BY YEAR DESC, MONTH DESC, DAY DESC, l.sp_id DESC
) t GROUP BY day, month, year

http://sqlfiddle.com/#!2/1514d/3 - スキームとクエリを表示できます。

私が手に入れたいのは

18 | october | 2012 | 0,0,1,1 | 6,5,4,3

spid 6 と spid 5 には 10 月 18 日のデータはありませんが、右結合と右外部結合の両方を試してみると、どちらもうまくいかないようです...

4

1 に答える 1

1

GROUP_CONCAT次のように使用します。

SELECT 
  Day, 
  month, 
  year,
  GROUP_CONCAT(total), 
  GROUP_CONCAT(SP_ID)
FROM
(
    SELECT 
      DATE_FORMAT(l.act_date, '%d') AS DAY, 
      DATE_FORMAT(l.act_date, '%M') AS MONTH,
      EXTRACT(YEAR FROM l.act_date) AS YEAR, 
      COUNT(*) as total,l.sp_id 
    FROM lead_activity2 as l 
    WHERE l.act_name='scb' 
      AND ((l.act_date>='2012-09-07 13:03:27' ) 
      AND (l.act_date<= '2012-11-07 13:03:27')) 
    GROUP BY MONTH, YEAR, DAY, l.sp_id 
    ORDER BY YEAR DESC, MONTH DESC, DAY DESC, l.sp_id DESC
) t GROUP BY day, month, year

更新された SQL フィドル

更新:はい、これを行うことができますが、LEFT JOIN一致しないを含めるために使用しますsp_id。これらの一致しない ID には、次のようにゼロで表示するためのNULL使用値があります。IFNULL

SELECT 
  Day, 
  month, 
  year,
  GROUP_CONCAT(total) Total, 
  GROUP_CONCAT(SP_ID) 'List of sp_ids'
FROM
(
  SELECT
    DATE_FORMAT(l.act_date, '%d') AS DAY, 
    DATE_FORMAT(l.act_date, '%M') AS MONTH, 
    EXTRACT(YEAR FROM l.act_date) AS YEAR, 
    COUNT(*) as total,
    IFNULL(s.sp_id , 0) sp_id
  FROM lead_activity2 as l 
  LEFT JOIN salesperson s ON l.sp_id = s.sp_id
  WHERE l.act_name='scb' 
    AND ((l.act_date>='2012-09-07 13:03:27' )
    AND (l.act_date<= '2012-11-07 13:03:27')) 
  GROUP BY MONTH, YEAR,DAY,s.sp_id 
) t
ORDER BY YEAR DESC, 
         MONTH DESC, 
         DAY DESC, 
         sp_id DESC

SQL Fiddle デモの更新

于 2012-11-07T09:20:27.133 に答える