1

我正在尝试按子字符串分组。我只得到一个结果:

Region = Coastal, Value (R) = 1144900.

我应该得到2个结果。

这是我的代码:

SELECT
  DISTINCT SUBSTRING_INDEX(`team_name`, '-', 1) AS 'Region', 
  SUM(`opportunities`.`value`) AS 'Value (R)'
FROM 
  `opportunities` 
  LEFT JOIN cf_type_link_data ON cf_type_link_data.sourceid = opportunities.id
  LEFT JOIN cf_lu_type_fields ON cf_type_link_data.fieldid = cf_lu_type_fields.id
  LEFT JOIN cf_lu_types_fields_dropdown ON cf_type_link_data.`value` = cf_lu_types_fields_dropdown.id AND cf_lu_type_fields.id = cf_lu_types_fields_dropdown.fieldid
  LEFT JOIN `lu_teams` ON `lu_teams`.`contactid` = `opportunities`.`user_allocation`
  LEFT JOIN `teams` ON `teams`.`id` = `lu_teams`.`teamid`
  LEFT JOIN `lu_opportunity_status` ON `lu_opportunity_status`.`id` = `opportunities`.`status`
WHERE 1 
  AND `cf_lu_types_fields_dropdown`.`values` = 'Building Project'
  AND cf_lu_type_fields.fieldname = 'Scaffolding Segment'
  AND (`opportunities`.`expecteddate` >= '2012-01-01' AND `opportunities`.`expecteddate` <= '2012-07-24')
GROUP BY 'Region' 
ORDER BY cf_lu_types_fields_dropdown.`values`;

任何帮助表示赞赏。

4

2 回答 2

0

您不能在 select inGROUP BY子句中使用相同的别名

尝试

SELECT DISTINCT SUBSTRING_INDEX(team_name, '-', 1) AS 'Region', 
       SUM(opportunities.value) AS 'Value (R)'....

GROUP BY SUBSTRING_INDEX(team_name, '-', 1)
ORDER BY cf_lu_types_fields_dropdown.values;
于 2012-07-24T12:51:24.867 回答
0

尽量不要使用引号作为字段标识符 -

“地区”-> 地区

在您的情况下,您在结果数据集中只看到一条记录,因为您按字符串“区域”分组,但您应该按值分组。

于 2012-07-24T12:52:04.543 回答