简体   繁体   中英

Combine 2 mysql queries into one table with unique rows

I am trying to complete a query that displays the total number of hours billed to each project, in the format below:

Project Name |  Hours
Project A    |  2
Project B    |  6
Project C    |  8

I have two SQL queries that count the number of hours billed to each project. This is necessary because each query has a different JOIN:

For example, if time_records.parent_type = 'Task', then the Join is:

INNER JOIN time_records
ON time_records.parent_id = project_objects.id

If time_records.parent_type = 'Project', then the Join is:

INNER JOIN time_records
ON projects.id = time_records.parent_id

I am trying to combine these two queries into one table, however the issue I have is that the table repeats the projects, instead of combining them into one unique row per project (similar to how a Pivot Table would function).

Here is my current query:

(SELECT projects.name AS expr1
     , sum(time_records.value) AS expr2
FROM
  project_objects
INNER JOIN projects
ON projects.id = project_objects.project_id
INNER JOIN time_records
ON time_records.parent_id = project_objects.id
WHERE
  time_records.parent_type = 'Task' 
GROUP BY 
  projects.name)

UNION ALL

(SELECT projects.name AS expr1
      , sum(time_records.value) as expr2
FROM
   projects
INNER JOIN time_records
ON projects.id = time_records.parent_id
WHERE
  time_records.parent_type = 'Project'
GROUP BY 
  projects.name)

Here's how it currently outputs:

Project Name | Hours
Project A | 2
Project B | 6
Project C | 8
Project A | 4

I need the Project A to be on one line, showing a value of 6 hours, instead of two lines. I tried the UNION operation however that doesn't help.

Can you try this and see if this works ?

SELECT X.expr1, SUM(X.expr2)
FROM 
  (SELECT 
    projects.name AS expr1
    , sum(time_records.value) AS expr2
  FROM project_objects
  INNER JOIN projects
    ON projects.id = project_objects.project_id
  INNER JOIN time_records
    ON time_records.parent_id = project_objects.id
  WHERE time_records.parent_type = 'Task' 
  GROUP BY projects.name

  UNION

  SELECT 
    projects.name AS expr1
    , sum(time_records.value) as expr2
  FROM projects
  INNER JOIN time_records
    ON projects.id = time_records.parent_id
  WHERE time_records.parent_type = 'Project'
  GROUP BY projects.name) X
GROUP BY X.expr1

use OR condition to connect

EDIT:

Check this out: most probable this would work for u. Sum results from two select statements

SELECT projects.name AS nameval
     , sum(time_records.value) AS total
FROM ((SELECT projects.name AS expr1
     , sum(time_records.value) AS expr2
FROM
  project_objects
INNER JOIN projects
ON projects.id = project_objects.project_id
INNER JOIN time_records
ON time_records.parent_id = project_objects.id
WHERE
  time_records.parent_type = 'Task' 
GROUP BY 
  projects.name)

UNION ALL

(SELECT projects.name AS expr1
      , sum(time_records.value) as expr2
FROM
   projects
INNER JOIN time_records
ON projects.id = time_records.parent_id
WHERE
  time_records.parent_type = 'Project'
GROUP BY 
  projects.name)) t group by expr1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM