SQL Rollup Parent Child

时间:2017-09-27 18:08:52

标签: sql

我已经在任务上完成了一个汇总,现在我想将这些列汇总到父键(user01)

我正在尝试将a汇总到父键(user01)

SELECT   c1.customer_name AS "project_organization",
              project.user01,
                            c2.customer_name AS "project_owning_organization",
                            project.project_code,
                            project.title,
                            task.task_id,
              task.task_name,
              SUM (ISNULL(task_budget_history.hours_budget,task.hours_budget)) AS "original_budget",    
              SUM (task.hours_budget) as budget,   
                            SUM(person_time_data.quantity) AS actual,    
              SUM((person_time_data.quantity)- task.hours_budget) as Variance 

                        FROM customer c1,
                            customer c2,
                            project_status,
                            project LEFT OUTER JOIN task
                            ON project.project_key=task.project_key
                            LEFT OUTER JOIN person_time_data
                            ON task.task_key = person_time_data.task_key
                            LEFT OUTER JOIN task_budget_history
                            ON task_budget_history.project_key = task.project_key
                            AND task_budget_history.task_key = task.task_key 
                            AND task_budget_history.task_budget_history_key = (SELECT MIN(tbh.task_budget_history_key) FROM task_budget_history tbh WHERE tbh.task_key = task_budget_history.task_key AND ISNULL(tbh.hours_budget,1)<>1)                        

                WHERE c1.customer_key = project.customer_key and :PM_Rollup = 'Yes' 
                            AND c2.customer_key = project.owning_customer_key and :PM_Rollup = 'Yes'
                            AND project.project_status_key = project_status.project_status_key and :PM_Rollup = 'Yes'
                            AND (task.hours_budget IS NOT NULL OR task.project_key IN (SELECT project_key
                                                         FROM person_time_data ptd2)) and :PM_Rollup = 'Yes'
                            AND c1.customer_name = ISNULL(:PM_project_organization,c1.customer_name) and :PM_Rollup = 'Yes'
                            AND c2.customer_name = ISNULL(:PM_project_owning_organization, c2.customer_name) and :PM_Rollup = 'Yes'  
              AND project.user01 = ISNULL(LEFT(:PM_project_code,7),project.user01)
                            AND CASE project_status.open_status
                                WHEN 'Y' THEN 'Active'
                                ELSE 'Inactive'
                                END = ISNULL(:PM_project_status, CASE project_status.open_status
                                                     WHEN 'Y' THEN 'Active'
                                                     ELSE 'Inactive'
                                                     END) and :PM_Rollup = 'Yes'
                            AND project.project_code not like 'TSR%' and :PM_Rollup = 'Yes'


                        GROUP BY rollup (task.task_id,
            c1.customer_name,
              project.user01,  
                            c2.customer_name,
                        project.project_code ,
                            project.title,
                            task.task_name ,
              person_time_data.quantity,
                            task_budget_history.hours_budget, 
              (person_time_data.quantity)- task.hours_budget, 
                            task.hours_budget  ) 


              having task_name is null and project.user01 is not null    and project.title is not null   and person_time_data.quantity is null   

               order by 4, 6    

1 个答案:

答案 0 :(得分:0)

如果您想按user01分组,则需要

 GROUP BY task.task_id,
 c1.customer_name,  
 c2.customer_name,
 project.project_code ,
 project.title,
 task.task_name ,
 person_time_data.quantity,
 task_budget_history.hours_budget,) 
 (person_time_data.quantity)- task.hours_budget, 
 task.hours_budget  
 project.user01 WITH ROLLUP

这是正确的ROLLUP语法