Hi all,
I'm trying to create new columns based on the elements within a column. I have two tables, the first is like this:
Tasks:
Key | Tree | Duty | Done_by |
-----------------------------
2 | 2 | Top | NULL |
3 | 2 | Plan | Bill |
4 | 2 | Done | Jane |
5 | 3 | Top | NULL |
etc
and Area:
Base | Site |
----------------
2 | Bristol |
5 | Cardiff |
8 | Bristol |
etc
and the table I want to produce looks like this:
Plans Done_by | COUNT(Bristol) | COUNT(Cardiff) |
-------------------------------------------------
Bill | 2 | 1 |
etc
As you can see the key is shown in base but refers to a specific assignment listed in Tree. Each assignment can be undertaken by different individuals. This is what I have so far:
SELECT TRIM(UPPER(tk.done_by)) AS "Plans done by", COUNT(ar.site) AS "Bristol",
COUNT(ar2.site) AS "Cardiff"
FROM Task tk
INNER JOIN Task tk2 ON tk.tree = tk2.tree
LEFT OUTER JOIN Area ar ON tk2.key = ar.base
INNER JOIN Task tk3 ON tk.tree = tk3.tree
LEFT OUTER JOIN Area ar2 ON tk3.key = ar2.base
WHERE (tk.duty = 'plan' AND ar.site = 'Bristol')
OR (ar2.site = 'Cardiff' AND tk.duty = 'plan')
GROUP BY TRIM(UPPER(tk.done_by))
ORDER BY TRIM(UPPER(tk.done_by)), COUNT(ar.site) DESC, COUNT(ar2.site) DESC
As you may be able to see from this query, I am a complete SQL novice! Please can you help!!!!
I'm trying to create new columns based on the elements within a column. I have two tables, the first is like this:
Tasks:
Key | Tree | Duty | Done_by |
-----------------------------
2 | 2 | Top | NULL |
3 | 2 | Plan | Bill |
4 | 2 | Done | Jane |
5 | 3 | Top | NULL |
etc
and Area:
Base | Site |
----------------
2 | Bristol |
5 | Cardiff |
8 | Bristol |
etc
and the table I want to produce looks like this:
Plans Done_by | COUNT(Bristol) | COUNT(Cardiff) |
-------------------------------------------------
Bill | 2 | 1 |
etc
As you can see the key is shown in base but refers to a specific assignment listed in Tree. Each assignment can be undertaken by different individuals. This is what I have so far:
SELECT TRIM(UPPER(tk.done_by)) AS "Plans done by", COUNT(ar.site) AS "Bristol",
COUNT(ar2.site) AS "Cardiff"
FROM Task tk
INNER JOIN Task tk2 ON tk.tree = tk2.tree
LEFT OUTER JOIN Area ar ON tk2.key = ar.base
INNER JOIN Task tk3 ON tk.tree = tk3.tree
LEFT OUTER JOIN Area ar2 ON tk3.key = ar2.base
WHERE (tk.duty = 'plan' AND ar.site = 'Bristol')
OR (ar2.site = 'Cardiff' AND tk.duty = 'plan')
GROUP BY TRIM(UPPER(tk.done_by))
ORDER BY TRIM(UPPER(tk.done_by)), COUNT(ar.site) DESC, COUNT(ar2.site) DESC
As you may be able to see from this query, I am a complete SQL novice! Please can you help!!!!