2016-02-08 22 views
-1

我一直试图通过正常的列和mysql函数别名列在哪里mysql函数别名不会工作组由mysql组由我尝试和哪个应该根据mysql文档工作。由正常的列和mysql函数别名列Mysql组

SELECT `project`.`project_name`, SUM(cost_allocated) AS cost_allocated, 
    count(task_id) as countTask, 
    GROUP_CONCAT(task_registration.members SEPARATOR ';') AS members,  
    GROUP_CONCAT(task_registration.indicators SEPARATOR ';') AS indicators, 
    GROUP_CONCAT(task_registration.percent_complete SEPARATOR ';') AS percent_complete, 
    GROUP_CONCAT(task_registration.status SEPARATOR ';') AS status, 
    DATE_FORMAT(`task_registration`.`created_at`, '%M') AS month 
FROM (`project`) 
INNER JOIN `task_registration` ON `task_registration`.`project` = `project`.`project_id` 
GROUP BY `task_registration`.`project`,`month` 
ORDER BY `project`.`project_id` desc 

结果通过除去正常列组和刚刚加入的MySQL函数别名列它工作正常,但我从消除task_registration`.`project后两者都需要

SELECT `project`.`project_name`, SUM(cost_allocated) AS cost_allocated, 
    count(task_id) as countTask, 
    GROUP_CONCAT(task_registration.members SEPARATOR ';') AS members, 
    GROUP_CONCAT(task_registration.indicators SEPARATOR ';') AS indicators, 
    GROUP_CONCAT(task_registration.percent_complete SEPARATOR ';') AS percent_complete, 
    GROUP_CONCAT(task_registration.status SEPARATOR ';') AS status, 
    DATE_FORMAT(`task_registration`.`created_at`, '%M') AS month 
FROM (`project`) 
INNER JOIN `task_registration` ON `task_registration`.`project` = `project`.`project_id` 
GROUP BY `month` 
ORDER BY `project`.`project_id` desc 

后,我得到 enter image description here

现在由 组成enter image description here

GROUP BY `task_registration`.`project`,`month` Doesnt work in this case 

http://sqlfiddle.com/#!2/18389/2我已经试过这到目前为止

+0

请现在看看 –

+0

您实际需要的输出是什么?你的结果集看起来是我期望的结果 – Kickstart

+0

我需要从上一个结果,然后按月在第一个组中按月没有组3-4月二十四月二月在bot有一个一月二月 –

回答

1

非常困难的工作,你想不表的定义,样本输入数据和输出样本数据是什么,但是从你的评论,我只能建议是使用第二查询作为子查询并将其连接到第一查询的结果: -

SELECT project.project_name, 
    SUM(cost_allocated) AS cost_allocated, 
    count(task_id) as countTask, 
    GROUP_CONCAT(task_registration.members SEPARATOR ';') AS members,  
    GROUP_CONCAT(task_registration.indicators SEPARATOR ';') AS indicators, 
    GROUP_CONCAT(task_registration.percent_complete SEPARATOR ';') AS percent_complete, 
    GROUP_CONCAT(task_registration.status SEPARATOR ';') AS status, 
    DATE_FORMAT(task_registration.created_at, '%M') AS month, 
    sub0.cost_allocated AS months_cost_allocated, 
    sub0.countTask AS months_countTask, 
    sub0.members AS months_members, 
    sub0.indicators AS months_indicators, 
    sub0.percent_complete AS months_percent_complete, 
    sub0.status AS months_status 
FROM (`project`) 
INNER JOIN `task_registration` ON `task_registration`.`project` = `project`.`project_id` 
INNER JOIN 
(
    SELECT DATE_FORMAT(task_registration.created_at, '%M') AS month, 
     SUM(cost_allocated) AS cost_allocated, 
     COUNT(task_id) as countTask, 
     GROUP_CONCAT(task_registration.members SEPARATOR ';') AS members, 
     GROUP_CONCAT(task_registration.indicators SEPARATOR ';') AS indicators, 
     GROUP_CONCAT(task_registration.percent_complete SEPARATOR ';') AS percent_complete, 
     GROUP_CONCAT(task_registration.status SEPARATOR ';') AS status 
    FROM (project) 
    INNER JOIN task_registration ON task_registration.project = project.project_id 
    GROUP BY `month` 
) sub0 
ON DATE_FORMAT(task_registration.created_at, '%M') = sub0.`month` 
GROUP BY task_registration.project, 
     `month`, 
     months_cost_allocated, 
     sub0.countTask AS months_countTask, 
     sub0.members AS months_members, 
     sub0.indicators AS months_indicators, 
     sub0.percent_complete AS months_percent_complete, 
     sub0.status AS months_status 
ORDER BY project.project_id desc 
+0

为什么多个按月分组sub0?当我只需要多个列组? –

+0

因为它们是有效的返回列,并且在标准SQL中,任何带有GROUP BY子句的返回列应该是聚合列或在GROUP BY子句中提及。 – Kickstart

+0

http://sqlfiddle.com/#!2/18389/2看看这里 –

0

谢谢你的答案我做这件事是我自己

SELECT COUNT(i.project_name) AS completed_projects,i.month 
FROM (

SELECT `project`.`project_name` , SUM(cost_allocated) AS cost_allocated, COUNT(task_id) AS countTask, GROUP_CONCAT(task_registration.members 
SEPARATOR ';') AS members, GROUP_CONCAT(task_registration.indicators 
SEPARATOR ';') AS indicators, GROUP_CONCAT(task_registration.percent_complete 
SEPARATOR ';') AS percent_complete, GROUP_CONCAT(task_registration.status 
SEPARATOR ';') AS 
STATUS , DATE_FORMAT(task_registration.created_at, '%M') AS 
MONTH FROM (
`project` 
) 
INNER JOIN `task_registration` ON `task_registration`.`project` = `project`.`project_id` 
GROUP BY `task_registration`.`project` 
ORDER BY `project`.`project_id` DESC 
) AS i 
GROUP BY i.month 

http://sqlfiddle.com/#!2/18389/11