views:

196

answers:

5

Using MySQL, I have three tables:

projects:

ID  name
1   "birthday party"
2   "soccer match"
3   "wine tasting evening"
4   "dig out garden"
5   "mountainbiking"
6   "making music"

batches:

ID  projectID  templateID  when
1   1          1            7 days before
2   1          1            1 day  before
3   4          2           21 days before
4   4          1            7 days before
5   5          1            7 days before
6   3          5            7 days before
7   3          3           14 days before
8   5          1           14 days before

templates:

ID  name  message
1   inf1  "Hi, I'd like to invite ..."
2   for1  "Dear Sir, Madam, ..."
3   can1  "Can you please ..."
4   inf2  "Would you like to ..."
5   all1  "To all dear friends ..."
6   inf3  "Does any of you guys ..."

I would like to display a table of templates and the number of projects they're used in. So, the result should be (updated!):

templateName  projectCount
inf1          3
for1          1
can1          1
inf2          0
all1          1
inf3          0

I've tried all kinds of SQL queries using various JOINs, but I guess this is too complicated for me. Is it possible to get this result using a single SQL statement?

A: 

have you tried something like this:-

SELECT TemplateId, COUNT(ProjectId) AS ProjectCount FROM Batches GROUP BY TemplateId
ydobonmai
@Ashish - The join in not needed for the wanted result. All the data is present in the `batches` table.
Oded
Yeah..I just realized and updated my answer. Thanks. :-)
ydobonmai
Sorry Ashish, I have updated my example because I need a column from the templates table in the output as well. Thanks for your time.
Fred K
A: 

From the o/p required sample you given above i assume you want numbers of templateId as a projectCount

select templateID, count(templateID) as projectCount from batches group by templateID

EDIT (AFTER question EDIT)

select t.name as templateName, count(b.templateID) as projectCount from batches b, templates t where b.templateID=t.id group by t.id
Salil
Thanks Salil, for the update. I tried your suggestion, but the projectCount for template #1 ("inf1") is 3 and your statement outputs 5. Also, templates #4 and #6 don't show up. Thank you for helping me.
Fred K
A: 

I am not sure about the mySql syntax, but this should do:

SELECT t.name as templateName, COUNT(DISTINCT b.projectID) as projectCount
FROM batches b
INNER JOIN templates t
  ON b.templateId = t.ID
GROUP BY t.name
Oded
It's not correct; OP wants template 1,projectcount 3; your query returns template 1,projectcount 5.
systempuntoout
@systempuntoout - thanks for the correction. Answer updated.
Oded
Sorry Oded, I have updated my example because I need a column from the templates table in the output as well. Thanks for your time.
Fred K
@Fred K - Answer updated, should give you the template name and project count for it.
Oded
Thanks Oded for your update. The output of your query is okay, except that the template that are not used in projects (4 and 6) do not show up in the output.
Fred K
+1  A: 
Select TemplateId,
       Count(distinct projectId) as ProjectCount,
FROM batches 
Group By TemplateId

I hope this should work.

We need distinct as from sample data i can see same template and project has multiple rows ......

Nitin Midha
The "projects" and "templates" tables are linked using "batches", shouldn't that table appear somewhere in the equation?
Fred K
Ohh sorry ... instead of template it should be batches ... Thanks for pointing, updated the answer above ....
Nitin Midha
Sorry Nitin Midha, I have updated my example because I need a column from the templates table in the output as well. Thanks for your time.
Fred K
A: 
SELECT t.name templateName, COUNT(DISTICT b.projectID) projectCount
FROM templates t
LEFT OUTER JOIN batches b ON t.ID = b.templateID
GROUP BY t.ID, t.name
ORDER BY t.ID
Anthony Faull
Yes! That's the one! (Although I had to change "DISTICT batches.projectID" to "DISTINCT b.projectID"). Output is exactly as I wished. Thanks a million, Anthony!
Fred K