您好我正在加入表格以返回数据,但我希望按特定位置对数据进行分组,并计算/求和与该位置相关的值 目前我只能检索数据,但它不会对该位置的值求和/计数这是我的查询
select brand_locations.locationName,campaign_stats.clicks,
campaign_stats.impressions,
campaign_stats.day,brand_locations.city,
click_actions.walkTo,click_actions.showMap,
click_actions.call,click_actions.coupon,driveTo,campaigns_details.state
from campaign_stats
inner join campaigns_details on campaign_stats.campaignId = campaigns_details.campaignId
inner join click_actions on click_actions.campaignId=campaign_stats.campaignId
inner join brand_locations on brand_locations.brandId = campaigns_details.brandId
where brand_locations.city = 'cape town'
输出
bellville 58
water front 12
bellville 4
bellville 1
century city 2
century city 4
我的目标是
bellville 63
water front 12
century city 6
谢谢
答案 0 :(得分:1)
MySQL SUM()函数检索已经过GROUP BY子句分组操作的表达式的和值。你试过这个选项吗?
select brand_locations.locationName,SUM(campaign_stats.clicks),
campaign_stats.impressions,
campaign_stats.day,brand_locations.city,
click_actions.walkTo,click_actions.showMap,
click_actions.call,click_actions.coupon,driveTo,campaigns_details.state
from campaign_stats
inner join campaigns_details on campaign_stats.campaignId = campaigns_details.campaignId
inner join click_actions on click_actions.campaignId=campaign_stats.campaignId
inner join brand_locations on brand_locations.brandId = campaigns_details.brandId
where brand_locations.city = 'cape town' group by brand_locations.locationName
答案 1 :(得分:0)
尝试此查询。我已按位置名称向查询添加了一个组,并使用函数sum来计算总点击次数。
select brand_locations.locationName,SUM(campaign_stats.clicks) as campaign_stats_cnt,
sum(campaign_stats.impressions) as impressions_cnt,
campaign_stats.day,brand_locations.city,
click_actions.walkTo,click_actions.showMap,
click_actions.call,click_actions.coupon,driveTo,campaigns_details.state
from campaign_stats
inner join campaigns_details on campaign_stats.campaignId = campaigns_details.campaignId
inner join click_actions on click_actions.campaignId=campaign_stats.campaignId
inner join brand_locations on brand_locations.brandId = campaigns_details.brandId
where brand_locations.city = 'cape town' group by brand_locations.locationName
答案 2 :(得分:0)
您可以使用sum()
从您获得的结果中获得所需的结果
SELECT title, SUM(count)
FROM (resultQuery)
GROUP BY title
注意:将标题替换为第一列的name
,将count
替换为第二列的名称,将resultQuery
替换为他们查询您的名称
是的,这是执行它的hacky子查询方式....