Reference not supported on group function - mysql

I have some problem using this query, it appears that i cannot use alias as reference on group by query
SELECT v.V_VEHICLEID, v.V_LICENSENO,
ROUND((MAX(IF(DAY(WP_DATETIME) = 1, WP_ODOMETER, NULL)) - MIN(IF(DAY(WP_DATETIME) = 1, WP_ODOMETER, NULL))) / 1000) AS TRIP1,
...,
ROUND((MAX(IF(DAY(WP_DATETIME) = 31, WP_ODOMETER, NULL)) - MIN(IF(DAY(WP_DATETIME) = 31, WP_ODOMETER, NULL))) / 1000) AS TRIP31,
ROUND(MAX(IF(DAY(WP_DATETIME) = 1, WP_FUELREAD, NULL)) - MIN(IF(DAY(WP_DATETIME) = 1, WP_FUELREAD, NULL)),2) AS FUEL1,
...,
ROUND(MAX(IF(DAY(WP_DATETIME) = 31, WP_FUELREAD, NULL)) - MIN(IF(DAY(WP_DATETIME) = 31, WP_FUELREAD, NULL)),2) AS FUEL31,
(SELECT TRIP1 / FUEL1) AS ECON1,
...,
(SELECT TRIP31 / FUEL31) AS ECON31
FROM VEHICLES v
JOIN WAYPOINTS wp on wp.WP_VEHICLEID = v.V_VEHICLEID
WHERE MONTH(wp.WP_DATETIME) = '6' AND v.V_USER = 'tc'
GROUP BY wp.WP_VEHICLEID
the error
Reference 'TRIP1' not supported (reference to group function)
any solution?

You can't use the column aliases you defined in a query in the same query. Try something like the following:
SELECT *,
TRIP1 / FUEL1 AS ECON1,
TRIP31 / FUEL31 AS ECON31,
OTHER COLUMNS....
FROM
(
SELECT v.V_VEHICLEID,
v.V_LICENSENO,
ROUND((MAX(IF(DAY(WP_DATETIME) = 1, WP_ODOMETER, NULL)) - MIN(IF(DAY(WP_DATETIME) = 1, WP_ODOMETER, NULL))) / 1000) AS TRIP1,
ROUND(MAX(IF(DAY(WP_DATETIME) = 1, WP_FUELREAD, NULL)) - MIN(IF(DAY(WP_DATETIME) = 1, WP_FUELREAD, NULL)),2) AS FUEL1,
ROUND((MAX(IF(DAY(WP_DATETIME) = 31, WP_ODOMETER, NULL)) - MIN(IF(DAY(WP_DATETIME) = 31, WP_ODOMETER, NULL))) / 1000) AS TRIP31,
ROUND(MAX(IF(DAY(WP_DATETIME) = 31, WP_FUELREAD, NULL)) - MIN(IF(DAY(WP_DATETIME) = 31, WP_FUELREAD, NULL)),2) AS FUEL31,
OTHER COLUMNS...
FROM VEHICLES v
JOIN WAYPOINTS wp
ON wp.WP_VEHICLEID = v.V_VEHICLEID
WHERE MONTH(wp.WP_DATETIME) = '6'
AND v.V_USER = 'tc'
GROUP BY wp.WP_VEHICLEID
) t

you did not specify the alias WP. take a look, it must be like this:
...
JOIN WAYPOINTS wp on wp.WP_VEHICLEID = v.V_VEHICLEID

Related

How to improve/simplify a query with a lot of subquerys?

I have a sql query that sum and counts some registers from my db, the problem is that I'm using a lot of subquerys and all of them have the same condition, I need to find a better solution because this is causing slowness in my system.
I tried to use a simple left join but mysql returned a single row and I want that counts and sum every register on the table dbgeneralesImportacion.
I have something like this:
SELECT dbgeneralesImportacion.id,
sapito.dbgeneralesImportacion.documentosCompletos AS 'documentosCompletos',
(SELECT COUNT(c.id)
FROM sapito.dbcontenedores c
WHERE c.operacion = dbgeneralesImportacion.id ) AS 'ContadorContenedores',
(SELECT SUM(IF(c.estatus = 1, 1, 0))
FROM sapito.dbcontenedores c
WHERE c.operacion = dbgeneralesImportacion.id ) AS 'ContadorDespachos',
(SELECT SUM(IF(c.estatus = 2, 1, 0))
FROM sapito.dbcontenedores c
WHERE c.operacion = dbgeneralesImportacion.id ) AS 'ContadorCompletado',
(SELECT SUM(IF(c.desconsolidacion = 1, 1, 0))
FROM sapito.dbcontenedores c
WHERE c.operacion = dbgeneralesImportacion.id ) AS 'DesconsolidacionPuerto',
(SELECT SUM(c.bultos) FROM sapito.dbcontenedores c WHERE c.operacion = dbgeneralesImportacion.id) AS 'bultos'
FROM dbgeneralesImportacion
And my idea was to do this but counts all register and return a single row:
SELECT dbgeneralesImportacion.id AS 'id',
COUNT(c.id) AS 'ContadorContenedores',
SUM(IF(c.estatus = 1, 1, 0)) AS 'ContadorDespachos',
SUM(IF(c.estatus = 2, 1, 0)) AS 'ContadorCompletado',
SUM(IF(c.desconsolidacion = 1, 1, 0)) AS 'DesconsolidacionPuerto',
SUM(c.bultos) AS 'bultos'
FROM dbgeneralesImportacion
LEFT JOIN dbcontenedores c ON c.operacion = dbgeneralesImportacion.id
Thank you everyone
You could use a join a and group by
SELECT a.id
, a.documentosCompletos
, COUNT(c.id) ContadorContenedores
, SUM(IF(c.estatus = 1, 1, 0)) ContadorDespachos
, SUM(IF(c.estatus = 2, 1, 0)) ContadorCompletado
, SUM(IF(c.desconsolidacion = 1, 1, 0)) DesconsolidacionPuerto
, SUM(c.bultos) bultos
FROM dbgeneralesImportacion a
INNER JOIN sapito.dbcontenedores c ON c.operacion = a.id
GRUP BY a.id, a.documentosCompletos
Not positive, but couldn't you do something like this
SELECT dbgeneralesImportacion.id,
sapito.dbgeneralesImportacion.documentosCompletos AS 'documentosCompletos',
COUNT(c.id) AS 'ContadorContenedores',
SUM(IF(c.estatus = 1, 1, 0)) AS 'ContadorDespachos',
SUM(IF(c.estatus = 2, 1, 0)) AS 'ContadorCompletado',
SUM(IF(c.desconsolidacion = 1, 1, 0)) AS 'DesconsolidacionPuerto',
SUM(c.bultos) AS 'bultos'
FROM dbgeneralesImportacion, sapito.dbcontenedores c
WHERE c.operacion = dbgeneralesImportacion.id
GROUP BY dbgeneralesImportacion.id
You need a GROUP BY, but you can also simplify the query:
SELECT gi.id AS id,
COUNT(c.id) AS ContadorContenedores,
SUM( c.estatus = 1 ) AS ContadorDespachos,
SUM( c.estatus = 2 ) AS ContadorCompletado,
SUM( c.desconsolidacion = 1 ) AS DesconsolidacionPuerto,
SUM(c.bultos) AS bultos
FROM dbgeneralesImportacion LEFT JOIN
dbcontenedores c
ON c.operacion = gi.id
GROUP BY gi.id;
MySQL treats boolean values as numbers in a numeric context, with true being 1 and false being 0. This makes it easy to count up matching values.
Also, only use single quotes for string and date constants. Using them for column aliases can lead to hard-to-debug problems.

MySQL getting and Invalid use of group function

I'm trying to count the result of tc.personnel_id if there's more than 1 result, then the output is 1. And then finally, it will get the sum. But I am getting an "Invalid use of group function" error when i execute this code. Any help will be appreciated.
SELECT tv.id,
tv.vesselName,
SUM(IF(tpi.returningCrew = 0, 1,
IF(
COUNT(tc.personnel_id) > 1, 1, 0)
)
) AS ex_crew,
SUM(IF(tpi.returningCrew = 1, 1, 0)) AS new_hire
FROM tbl_vessels AS tv
LEFT JOIN tbl_personnel_info AS tpi
ON tv.id = tpi.lastJoinedVsl
LEFT JOIN tbl_contracts AS tc
ON tpi.id = tc.personnel_id
LEFT JOIN tbl_management AS tm
ON tm.id = tv.management
WHERE tpi.emp_status = 'ON-BOARD'
AND tc.status = 'ACTIVE'
AND tv.trade_route = 'WORLD WIDE'
AND tm.management = 'TOKYO'
GROUP BY vesselName;
Aggregate functions can not be nested. So something like SUM(.. COUNT(..) ..) is not possible. You might try something like this:
SELECT tv.id,
tv.vesselName,
CASE WHEN COUNT(tc.personnel_id) > 1
THEN SUM(IF(tpi.returningCrew = 0, 1, 1)
ELSE SUM(IF(tpi.returningCrew = 0, 1, 0)
END AS ex_crew,
SUM(IF(tpi.returningCrew = 1, 1, 0)) AS new_hire
FROM [..]
SUM(IF(tpi.returningCrew = 0, 1, 1) can be rewriten to SUM(1) or to COUNT(*).
SUM(IF(tpi.returningCrew = 0, 1, 0) cann also be rewritten to SUM(tpi.returningCrew = 0)
And SUM(IF(tpi.returningCrew = 1, 1, 0)) to SUM(tpi.returningCrew = 1)
So the final select could be:
SELECT tv.id,
tv.vesselName,
CASE WHEN COUNT(tc.personnel_id) > 1
THEN COUNT(*)
ELSE SUM(tpi.returningCrew = 0)
END AS ex_crew,
SUM(tpi.returningCrew = 1) AS new_hire
FROM [..]

need help adding column to one table using function that does arithmetic operations between columns from two separate tables

I am trying to add a column "wOBA" to a table "starting_pitcher_stats" in MySQL using sequel-pro. Below is the code for a function that performs arithmetic operations on nine variables in the "starting_pitcher_stats" table. In particular, the function gathers the values for a number of variables, applies different weights (coefficients) to some of them (numerator below) and divides that sum by the addition and subtraction of a few more variables. All of these variables reside in the "starting_pitcher_stats" table. The arithmetic operations are expressed in the below formula (the coefficients are the values that are multiplied by each of the variables in numerator below):
wOBA=(.69*walks_a + .72*HBP + .89*singles_a + 1.27*doubles_a + 1.62*triples_a+ 2.10*HR_a)/(at_bats+walks_a+SF+HBP)
Each weight varies by year. The different weights for each year come from the table "GUTS".
The first dilemma I'm having is getting the correct code for the function. The second is the correct code syntax to use to actually call up this function and populate the new column with correct weighted wOBA values for each game of each year(season) for each "Starting_Pitcher".
The function is created with the code below and is listed as a function "wOBA" in my list of functions and procedures. However, the little wheel/knob next to the function name in sequel pro is greyed-out for some reason. It's not until I find out the correct code to invoke it, that I'll know if there are any errors.
Please ask for any more information that I can provide to clarify anything.
Thank you in advance.
DELIMITER $$
CREATE FUNCTION wOBA(wOBA DECIMAL(10,3))
RETURNS DECIMAL(10,3)
BEGIN
DECLARE wOBA decimal(10,3);
SET wOBA = 0;
SELECT cast((SELECT SUM(weighted_BB) as wBB_sum
FROM (
SELECT g.wBB*SUM(if(e.event_CD=14,1,0)) as weighted_BB
FROM events e
INNER JOIN GUTS g
ON substring(e.game_ID,4,4)=g.season
WHERE PIT_ID=Starting_Pitcher
GROUP BY g.season)
as walks_a)
+ (SELECT SUM(weighted_HBP) as wHBP_sum
FROM (
SELECT g.wHBP*SUM(if(e.event_CD=16,1,0)) as weighted_HBP
FROM events e
INNER JOIN GUTS g
ON substring(e.game_ID,4,4)=g.season
WHERE PIT_ID=Starting_Pitcher
GROUP BY g.season)
as HBP)
+ (SELECT SUM(weighted_1B) as w1B_sum
FROM (
SELECT g.w1B*SUM(if(e.event_CD=20,1,0)) as weighted_1B
FROM events e
INNER JOIN GUTS g
ON substring(e.game_ID,4,4)=g.season
WHERE PIT_ID=Starting_Pitcher
GROUP BY g.season)
as singles_a)
+ (SELECT SUM(weighted_2B) as w2B_sum
FROM (
SELECT g.w2B*SUM(if(e.event_CD=21,1,0)) as weighted_2B
FROM events e
INNER JOIN GUTS g
ON substring(e.game_ID,4,4)=g.season
WHERE PIT_ID=Starting_Pitcher
GROUP BY g.season)
as doubles_a)
+ (SELECT SUM(weighted_3B) as w3B_sum
FROM (
SELECT g.w3B*SUM(if(e.event_CD=22,1,0)) as weighted_3B
FROM events e
INNER JOIN GUTS g
ON substring(e.game_ID,4,4)=g.season
WHERE PIT_ID=Starting_Pitcher
GROUP BY g.season)
as triples_a)
+ (SELECT SUM(weighted_HR) as wHR_sum
FROM (
SELECT g.wHR*SUM(if(e.event_CD=23,1,0)) as weighted_HR
FROM events e
INNER JOIN GUTS g
ON substring(e.game_ID,4,4)=g.season
WHERE PIT_ID=Starting_Pitcher
GROUP BY g.season)
as HR_a) as decimal(10,3))
/
cast(SUM(if(e.ab_fl="T",1,0))
+ SUM(if(e.event_cd=14,1,0))
+ SUM(if(e.SF_fl="T",1,0))
+ SUM(if(e.event_cd=16,1,0)) as unsigned) INTO wOBA
FROM events e
WHERE e.PIT_ID=Starting_Pitcher AND PIT_START_FL = "T"
LIMIT 1;
RETURN wOBA;
END
$$
DELIMITER ;
Darwin, Here are two screen shots of the events table. The first is of the structure and the second is some of the content (as not all of the content fits into the shot):
[
Here are screenshots of the structure and contents of the GUTS table.
Here is a screenshot of the events table structure showing the fields (and their definitions)in the function:
Update:
UPDATE retrosheet.starting_pitcher_stats
SET starting_pitcher_stats.wOBA =(SELECT
(
(g.wBB * SUM(IF(e.event_cd = 14, 1, 0)))
+ (g.wHBP * SUM(IF(e.event_cd = 16, 1, 0)))
+ (g.w1B * SUM(IF(e.event_cd = 20, 1, 0)))
+ (g.w2B * SUM(IF(e.event_cd = 21, 1, 0)))
+ (g.w3B * SUM(IF(e.event_cd = 22, 1, 0)))
+ (g.wHR * SUM(IF(e.event_cd = 23, 1, 0)))
)
/
(
SUM(IF(e.ab_fl = 'T', 1, 0))
+ SUM(IF(e.event_cd = 14, 1, 0))
+ SUM(IF(e.sf_fl = 'T', 1, 0))
+ SUM(IF(e.event_cd = 16, 1, 0))
) AS wOBA
FROM events AS e, GUTS AS g
WHERE e.YEAR_ID = g.SEASON_ID
AND e.PIT_START_FL= 'T'
AND e.PIT_ID = Starting_Pitcher)
The query just keeps running. I'll keep tweaking it.
update:
screenshots of starting_pitcher_stats table
Update:
Ok, I'm trying to just create a wOBA column as part of a new table with columns for the other components of wOBA.
But, the query just goes on forever. How can I make the run-time shorter?
DROP TABLE IF EXISTS starting_pitcher_wOBA;
CREATE TABLE starting_pitcher_wOBA
SELECT
a.YEAR_ID
,
a.GAME_ID
,
a.PIT_ID
,
a.wBB
,
a.wHBP
,
a.w1B
,
a.w2B
,
a.w3B
,
a.wHR
,
a.u_walks_a
,
a.HBP
,
a.singles_a
,
a.doubles_a
,
a.triples_a
,
a.HR_a
,
a.at_bats
,
a.sacrifice_flies_a
,
a.wOBA
FROM
(
SELECT
g.YEAR_ID
,
h.GAME_ID
,
e.PIT_ID
,
g.wBB
,
g.wHBP
,
g.w1B
,
g.w2B
,
g.w3B
,
g.wHR
,
SUM(IF(e.event_cd = 14, 1, 0)) AS u_walks_a
,
SUM(IF(e.event_cd = 16, 1, 0)) AS HBP
,
SUM(IF(e.event_cd = 20, 1, 0)) AS singles_a
,
SUM(IF(e.event_cd = 21, 1, 0)) AS doubles_a
,
SUM(IF(e.event_cd = 22, 1, 0)) AS triples_a
,
SUM(IF(e.event_cd = 23, 1, 0)) AS HR_a
,
SUM(IF(e.ab_fl = 'T', 1, 0)) AS at_bats
,
SUM(IF(e.sf_fl = 'T', 1, 0)) AS sacrifice_flies_a
,
(
(
(g.wBB * SUM(IF(e.event_cd = 14, 1, 0)))
+ (g.wHBP * SUM(IF(e.event_cd = 16, 1, 0)))
+ (g.w1B * SUM(IF(e.event_cd = 20, 1, 0)))
+ (g.w2B * SUM(IF(e.event_cd = 21, 1, 0)))
+ (g.w3B * SUM(IF(e.event_cd = 22, 1, 0)))
+ (g.wHR * SUM(IF(e.event_cd = 23, 1, 0)))
)
/
(
SUM(IF(e.ab_fl = 'T', 1, 0))
+ SUM(IF(e.event_cd = 14, 1, 0))
+ SUM(IF(e.sf_fl = 'T', 1, 0))
+ SUM(IF(e.event_cd = 16, 1, 0))
)
) AS wOBA
FROM events AS e, GUTS AS g, game AS h
WHERE e.PIT_START_FL= 'T'
GROUP BY g.YEAR_ID, h.GAME_ID,e.PIT_ID) AS a
INNER JOIN GUTS AS g
ON
a.YEAR_ID=g.YEAR_ID
INNER JOIN game AS h
ON
a.GAME_ID = h.GAME_ID
INNER JOIN events AS e
ON
a.PIT_ID = e.PIT_ID
We'll start by cleaning up the query. You should always try to perform your calculations across each row when possible rather than performing multiple vertical subqueries, as this avoids the DBMS making multiple passes over the same table.
SELECT
(
( (g.wbb * SUM(IF(e.event_cd = 14, 1, 0)))
+ (g.whbp * SUM(IF(e.event_cd = 16, 1, 0)))
+ (g.w1b * SUM(IF(e.event_cd = 20, 1, 0)))
+ (g.w2b * SUM(IF(e.event_cd = 21, 1, 0)))
+ (g.w3b * SUM(IF(e.event_cd = 22, 1, 0)))
+ (g.whr * SUM(IF(e.event_cd = 23, 1, 0)))
)
/
(
SUM(IF(e.ab_fl = 'T', 1, 0))
+ SUM(IF(e.event_cd = 14, 1, 0))
+ SUM(IF(e.sf_fl = 'T', 1, 0))
+ SUM(IF(e.event_cd = 16, 1, 0))
)
) AS woba
FROM events e, guts g
WHERE e.year_id = g.season_id
AND e.pit_start_fl = 'T'
AND e.pit_id = starting_pitcher
GROUP BY g.season;
Assuming I haven't dropped a comma somewhere, this will return a column woba for each year for the specified starting pitcher.
Note that I've joined the tables on e.year_id instead of SUBSTRING(e.game_ID,4,4); this avoids the overhead of calling SUBSTRING() on each record. This sort of thing seems minor, but it can add up quickly over a large table.
That should be enough to get you started.

Using MySQL Pivot Table Queries for a Monthly Attendance Report

I have used pivot query for monthly report..
My table is like this:
CREATE TABLE IF NOT EXISTS `attendance` (
`empID` int(11) NOT NULL,
`date` date NOT NULL,
`deptID` int(11) NOT NULL,
`attStatus` varchar(3) NOT NULL,
PRIMARY KEY (`empID`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
For monthly attendance report, I have used this query:
SELECT empID,
GROUP_CONCAT(if(DAY(`date`) = 1, `attStatus`, NULL)) AS 'day1',
GROUP_CONCAT(if(DAY(`date`) = 2, `attStatus`, NULL)) AS 'day2',
GROUP_CONCAT(if(DAY(`date`) = 3, `attStatus`, NULL)) AS 'day3',
GROUP_CONCAT(if(DAY(`date`) = 4, `attStatus`, NULL)) AS 'day4',
GROUP_CONCAT(if(DAY(`date`) = 5, `attStatus`, NULL)) AS 'day5',
GROUP_CONCAT(if(DAY(`date`) = 6, `attStatus`, NULL)) AS 'day6',
GROUP_CONCAT(if(DAY(`date`) = 7, `attStatus`, NULL)) AS 'day7',
GROUP_CONCAT(if(DAY(`date`) = 8, `attStatus`, NULL)) AS 'day8',
GROUP_CONCAT(if(DAY(`date`) = 9, `attStatus`, NULL)) AS 'day9',
GROUP_CONCAT(if(DAY(`date`) = 10, `attStatus`, NULL)) AS 'day10',
GROUP_CONCAT(if(DAY(`date`) = 11, `attStatus`, NULL)) AS 'day11',
GROUP_CONCAT(if(DAY(`date`) = 12, `attStatus`, NULL)) AS 'day12',
GROUP_CONCAT(if(DAY(`date`) = 13, `attStatus`, NULL)) AS 'day13',
GROUP_CONCAT(if(DAY(`date`) = 14, `attStatus`, NULL)) AS 'day14',
GROUP_CONCAT(if(DAY(`date`) = 15, `attStatus`, NULL)) AS 'day15',
GROUP_CONCAT(if(DAY(`date`) = 16, `attStatus`, NULL)) AS 'day16',
GROUP_CONCAT(if(DAY(`date`) = 17, `attStatus`, NULL)) AS 'day17',
GROUP_CONCAT(if(DAY(`date`) = 18, `attStatus`, NULL)) AS 'day18',
GROUP_CONCAT(if(DAY(`date`) = 19, `attStatus`, NULL)) AS 'day19',
GROUP_CONCAT(if(DAY(`date`) = 20, `attStatus`, NULL)) AS 'day20',
GROUP_CONCAT(if(DAY(`date`) = 21, `attStatus`, NULL)) AS 'day21',
GROUP_CONCAT(if(DAY(`date`) = 22, `attStatus`, NULL)) AS 'day22',
GROUP_CONCAT(if(DAY(`date`) = 23, `attStatus`, NULL)) AS 'day23',
GROUP_CONCAT(if(DAY(`date`) = 24, `attStatus`, NULL)) AS 'day24',
GROUP_CONCAT(if(DAY(`date`) = 25, `attStatus`, NULL)) AS 'day25',
GROUP_CONCAT(if(DAY(`date`) = 26, `attStatus`, NULL)) AS 'day26',
GROUP_CONCAT(if(DAY(`date`) = 27, `attStatus`, NULL)) AS 'day27',
GROUP_CONCAT(if(DAY(`date`) = 28, `attStatus`, NULL)) AS 'day28',
GROUP_CONCAT(if(DAY(`date`) = 29, `attStatus`, NULL)) AS 'day29',
GROUP_CONCAT(if(DAY(`date`) = 30, `attStatus`, NULL)) AS 'day30',
GROUP_CONCAT(if(DAY(`date`) = 31, `attStatus`, NULL)) AS 'day31',
COUNT(if(`attStatus`='P', `attStatus`, NULL)) AS 'totalP'
FROM `attendance`
WHERE deptID=1 AND `date` BETWEEN '2011-07-01' AND '2011-07-31'
GROUP BY empID
So my question is, isn't this query too long?
Is there any alternative to this query to display monthly attendance report ?
If this query is okay then for those months which have 30 days in it, result for 31 days is displayed. Is there any solution to this?
UPDATE:
Here is a screenshot which explain what type of report I want:
No idea if your original query would work but something like this would be simpler:
select DAY(date),count(*) from attendance where MONTH(date) = 2 group by DAY(date);
That should get the attendance for February. The output would be:
+-----------+----------+
| DAY(date) | count(*) |
+-----------+----------+
| 18 | 1 |
| 19 | 2 |
+-----------+----------+
First, create a help table:
CREATE TABLE days
( d TINYINT PRIMARY KEY
) ;
INSERT INTO days
VALUES
(0), (1), (2), ..., (31) ;
Then you can try this. It will return less columns than your query though:
SELECT
a.empID
, GROUP_CONCAT( CASE WHEN a.attStatus IS NULL
THEN 'N'
ELSE a.attStatus
END
ORDER BY dates.dateCheck)
AS days
, COUNT( CASE WHEN a.attStatus='P' THEN 1 END )
AS totalP
FROM
( SELECT monthStart + INTERVAL d DAY
AS dateCheck
FROM days
CROSS JOIN
( SELECT '2011-07-01' AS monthStart
) AS m
WHERE MONTH(monthStart + INTERVAL d DAY) = MONTH(monthStart)
) AS dates
LEFT JOIN attendance AS a
ON a.`date` = dates.dateCheck
WHERE a.deptID = 1
GROUP BY a.empID

Rewriting MySQL query with subqueries to joins

I have written a fairly complex SQL query to get some statistics about animals from an animal sampling database. This query includes a number of subqueries and I would now like to see if it is possible to rewrite this query in any way to use joins instead of subqueries. I have a dim idea that this might reduce query time. (it's now about 23s on a mac mini).
Here's the query:
SELECT COUNT(DISTINCT a.AnimalID), TO_DAYS(a.VisitDate) AS day,
DATE_FORMAT(a.VisitDate, '%b %d %Y'), a.origin,
(
SELECT COUNT(DISTINCT a.AnimalID)
FROM samples AS a
JOIN
custom_animals AS b
ON a.AnimalID = b.animal_id
WHERE
b.organism = 2
AND
TO_DAYS(a.VisitDate) = day
) AS Goats,
(
SELECT COUNT(DISTINCT a.AnimalID)
FROM samples AS a
JOIN custom_animals AS b
ON a.AnimalID = b.animal_id
WHERE
b.organism = 2
AND
b.sex = 'Female'
AND
TO_DAYS(a.VisitDate) = day
) AS GF,
(
SELECT COUNT(DISTINCT a.AnimalID)
FROM samples AS a
JOIN custom_animals AS b
ON a.AnimalID = b.animal_id
WHERE
b.organism = 3
AND
b.sex = 'Female'
AND
TO_DAYS(a.VisitDate) = day
) AS SF
FROM
samples AS a
JOIN custom_animals AS b
ON a.AnimalID = b.animal_id
WHERE
project = 5
AND
AnimalID LIKE 'AVD%'
GROUP BY
TO_DAYS(a.VisitDate);
Thanks to ksogor my query is now way faster at;
SELECT DATE_FORMAT(s.VisitDate, '%b %d %Y') AS date,
s.origin,
SUM(IF(project = 5 AND s.AnimalID LIKE 'AVD%', 1, 0)) AS sampled_animals,
SUM(IF(ca.organism = 2, 1, 0)) AS sampled_goats,
SUM(IF(ca.organism = 2 AND ca.sex = 'Female', 1, 0)) AS female_goats,
SUM(IF(ca.organism = 3 AND ca.sex = 'Female', 1, 0)) AS female_sheep
FROM samples s JOIN custom_animals ca ON s.AnimalID = ca.animal_id
GROUP BY date;
I would still need to make this query select distinct s.AnimalID though as right now it counts the samples we have taken from these animals instead of the animals themselves. Anyone got any idea?
After some more help from ksogor I now have a great query:
SELECT DATE_FORMAT(s.VisitDate, '%b %d %Y') AS date,
s.origin,
SUM(IF(project = 5 AND s.AnimalID LIKE 'AVD%', 1, 0)) AS sampled_animals,
SUM(IF(ca.organism = 2, 1, 0)) AS sampled_goats,
SUM(IF(ca.organism = 2 AND ca.sex = 'Female', 1, 0)) AS female_goats,
SUM(IF(ca.organism = 3 AND ca.sex = 'Female', 1, 0)) AS female_sheep
FROM (
SELECT DISTINCT AnimalID AS AnimalID,
VisitDate,
origin,
project
FROM samples
) s
JOIN custom_animals ca ON s.AnimalID = ca.animal_id
GROUP BY date;
You can just use if or case statements, like this:
SELECT SUM(if(project = 5 AND AnimealID LIKE 'AVD%', 1, 0)) AS countbyproj,
TO_DAYS(s.VisitDate) AS day,
DATE_FORMAT(s.VisitDate, '%b %d %Y') AS date,
s.origin,
SUM(if(ca.organism = 2, 1, 0)) AS countGoats,
SUM(if(ca.organism = 2 AND ca.sex = 'Female', 1, 0)) AS countGF,
SUM(if(ca.organism = 3 AND ca.sex = 'Female', 1, 0)) AS countSF
FROM samples s JOIN custom_animals ca ON s.AnimalID = ca.animal_id
GROUP BY TO_DAYS(a.VisitDate);
I can't check query, I don't know what result you're expected and which tables/relations you have, so this is only example with idea.
If you need count unque AnimealID's for each day:
SELECT SUM(byproj) AS countbyproj,
day,
date,
origin,
SUM(Goats) AS countGoats,
SUM(GF) AS countGF,
SUM(SF) AS countSF
FROM (
SELECT s.AnimealID,
if(project = 5 AND AnimealID LIKE 'AVD%', 1, 0) AS byproj,
TO_DAYS(s.VisitDate) AS day,
DATE_FORMAT(s.VisitDate, '%b %d %Y') AS date,
s.origin,
if(ca.organism = 2, 1, 0)) AS Goats,
if(ca.organism = 2 AND ca.sex = 'Female', 1, 0) AS GF,
if(ca.organism = 3 AND ca.sex = 'Female', 1, 0) AS SF
FROM samples s JOIN custom_animals ca ON s.AnimalID = ca.animal_id
) dataset
GROUP BY dataset.day, dataset.AnimealID;