提取点击月份没有

时间:2013-11-05 10:35:39

标签: mysql sql database

我正在使用MySQL。这是我的架构:

  

bannerstatclick(idBannerStats:integer,Time:Timestamp,idCampaignBanner:char(36))

我正在尝试编写一个查询,通过使用idCampaignBanner上的count来选择月份点击总数。 这将无法正常工作,它将无效使用组功能。 我还会尝试使用having子句,但它也不起作用......

SELECT count(idCampaignBanner) AS TotalClicks ,max(`Time`) AS maxdate,(min(`Time`) + INTERVAL 30 DAY)as monthly
FROM newradium.BannerStatsClick 
WHERE Time BETWEEN   max(`Time`)  AND (  max(`Time`)- INTERVAL 30 DAY)

2 个答案:

答案 0 :(得分:0)

这样的事情应该有用(如果你做聚合,你需要group by子句)

select count(idCampaignBanner), MONTH(`Time`) as m
from newradium.BannerStatsClick
group by m

答案 1 :(得分:0)

SELECT 
  count(idCampaignBanner) AS TotalClicks
, max(`Time`) AS maxdate
, (min(`Time`) + INTERVAL 30 DAY)as monthly
FROM newradium.BannerStatsClick 
WHERE Time <= (Select max(`Time`) FROM newradium.BannerStatsClick)
  And Time >= (Select max(`Time`) - INTERVAL 30 DAY FROM newradium.BannerStatsClick)

技术上可以摆脱“时间&lt; =(选择max(Time)FROM newradium.BannerStatsClick)”,并不会真正影响选择。但如果您将来需要不同的范围,请留下来