Link to home
Start Free TrialLog in
Avatar of Martin Courtney
Martin CourtneyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Problem with MySQL query - graph

Hi
Let me start out by apologising for being a complete novice when it comes to MySQL. I have dabbled with MS Access but I am rusty on MySQL

I am trying to get my head around some logic and I am hoping that someone can set me straight please.

So, I have three tables, one for Staff Accidents, one for Pupil Accidents and one for Hazzards / Near Misses.

Now - with a bit of googling I have come up with this query to return only the rows where the date is within the last three months:

SELECT
	`staff accident`.`fld_key` AS `fld_key`,
	`staff accident`.`Incident No#` AS `Incident No#`,
	`staff accident`.`fld_date` AS `fld_date`,
	`staff accident`.`Inc_Time` AS `Inc_Time`,
	`staff accident`.`Staff_Name` AS `Staff_Name`,
	`staff accident`.`Accident / Incident` AS `Accident / Incident`,
	`staff accident`.`First Aid Received` AS `First Aid Received`,
	`staff accident`.`Dr or Hospital attended?` AS `Dr or Hospital attended?`,
	`staff accident`.`Admitted to Hospital?` AS `Admitted to Hospital?`,
	`staff accident`.`Work Activity?` AS `Work Activity?`
FROM
	`staff accident`
WHERE
	(
		`staff accident`.`fld_date` >= (
			date_format(curdate(), '%Y-%m-01') - INTERVAL 2 MONTH
		)
	)

Open in new window


I have done this for each table, and saved as separate views.
vw_ps_3_months
vw_sa_3_months
vw_hz_3_months

I have then created a view which gives me the totals in one view:

SELECT
	`vw_tot_pa_3_months`.`Pupil Accident` AS `Pupil Accident`,
	`vw_tot_sa_3_months`.`Staff Accidents` AS `Staff Accidents`,
	`vw_tot_hz_3_months`.`Hazzard` AS `Hazzard`
FROM
	(
		(
			`vw_tot_hz_3_months`
			JOIN `vw_tot_pa_3_months`
		)
		JOIN `vw_tot_sa_3_months`
	)

Open in new window


This is great as I now have my three month summary data.

Pupil Accident         Staff Accidents       Hazzard
4                                2                               1

The Problem
Now I need to transpose this data and add a label field, so that I can drive a graph.  This is the point where I have hit a brick wall as I have no idea where to go from here.

This is what I need:

Incident Type     Qty
Pupil Accident        3
Staff Accident         8
Hazzard Report      3

I would welcome any suggestions please. Also if my queries are incorrect I would appreciate some advise please.

NB I am using PHPRunner to produce the GUI.
(At the moment I don't have any relationships established between any of the tables.)

Many thanks

Martin (UK)
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Martin Courtney

ASKER

Thanks very much - that worked a treat
You are welcome.