具有时间间隔的DAYNAME和名称的SUM表

时间:2015-08-14 14:49:38

标签: mysql

我把这个名为“downloads”的表格组成了这样的

id  date                name                                      quantity
 1  2015-05-13 23:30:09 CRCP03 - Cartoon Race Car Pack 03                1
 2  2015-05-14 00:30:09 CRCPV2-01 - Cartoon Race Cars Pack V2 - 01       1
 3  2015-05-14 17:10:06 CRCP03 - Cartoon Race Car Pack 03                1
 4  2015-05-15 23:47:07 CRCPV2-01 - Cartoon Race Cars Pack V2 - 01       1
 5  2015-05-16 03:31:06 CRCP03 - Cartoon Race Car Pack 03                1
 6  2015-05-17 13:13:06 CRCP03 - Cartoon Race Car Pack 03                1
 7  2015-05-18 00:32:06 CRCPV2-01 - Cartoon Race Cars Pack V2 - 01       1

我希望SUM "name" DAYNAME DATE_SUB(curdate(), INTERVAL 1 MONTH) SELECT name, SUM(quantity), DAYNAME(d.date) AS weekday FROM downloads d GROUP BY WEEKDAY(d.date) /*// This does not work I get a fetch_assoc error also does not have a time interval SELECT WEEKDAY(d.date) AS weekday, SUM(CASE WHEN d.name = 'CRCP03 - Cartoon Race Car Pack 03' THEN 1 ELSE 0 END) AS a, SUM(CASE WHEN d.name = 'CRCPV2-01 - Cartoon Race Cars Pack V2 - 01' THEN 1 ELSE 0 END) AS b, FROM downloads d GROUP BY WEEKDAY(d.date) */ Monday - CRCP03 - Cartoon Race Car Pack 03 - 70 Tuesday - CRCPV2-01 - Cartoon Race Cars Pack V2 - 01 - 72 Wednesday - CRCP03 - Cartoon Race Car Pack 03 - 89 Thursday - CRCP03 - Cartoon Race Car Pack 03 - 77 Friday - CRCP03 - Cartoon Race Car Pack 03 - 70 Saturday - CRCP03 - Cartoon Race Car Pack 03 - 68 Sunday - CRCPV2-01 - Cartoon Race Cars Pack V2 - 01 - 49 。 也是知名度的总和

当前我拥有的是这个

playerMap.heatmapCallback

我得到了这种没有间隔的混合结果

<ui-gmap-google-map center='map.center' zoom='map.zoom'>
    <ui-gmap-layer namespace="visualization" type="HeatmapLayer" show="map.showHeat" onCreated="map.heatLayerCallback"></ui-gmap-layer>
</ui-gmap-google-map>

感谢任何携带帮助的人

1 个答案:

答案 0 :(得分:1)

要获取每个工作日显示的每个名称的次数,您可以使用已有的聚合,但按名称和工作日分组:

SELECT DAYNAME(dateColumn) AS weekDay, name, SUM(quantity) AS totalCount
FROM downloads
GROUP BY DAYNAME(dateColumn), name;

要仅选择间隔,您只需添加where子句即可。例如,如果我只想在5月份这样做:

SELECT DAYNAME(dateColumn) AS weekDay, name, SUM(quantity) AS totalCount
FROM downloads
WHERE dateColumn BETWEEN '2015-05-01' AND '2015-05-31'
GROUP BY DAYNAME(dateColumn), name;

如果你想并排查看名称,工作日的每一行,你需要查看一个数据透视表,这很难做到。但是,如果您知道行名称,则可以使用条件聚合:

SELECT DAYNAME(dateColumn) AS weekday,
  SUM(name = 'CRCPV2-01 - Cartoon Race Cars Pack V2 - 01') AS 'CRCPV2-01 - Cartoon Race Cars Pack V2 - 01',
  SUM(name = 'CRCP03 - Cartoon Race Car Pack 03') AS 'CRCP03 - Cartoon Race Car Pack 03'
FROM downloads
WHERE dateColumn BETWEEN '2015-05-01' AND '2015-05-31'
GROUP BY DAYNAME(dateColumn);

这两个查询都可以在此SQL Fiddle中查看。