Need help to optimise MySQL statement

Soldato
Joined
26 Dec 2008
Posts
3,388
Location
Edinburgh
I know some SQL basics but still a newb, I'm trying to get my head around how I combine a number of SELECT statements.

So far I've put together a number of SELECT statements looking up some information on a database.
This is what I have:

Code:
-- This script looks for the tasks with APPROVAL: Page Idea submitted for approval in the name for selected MONTH
SELECT 
    'Page ideas approved' AS 'Stage',
    taskName AS 'TW Task Name',
    DATE_FORMAT(taskCompletedDate, '%M %y') AS 'Month',
    COUNT(*) AS 'Completed'
FROM
    tasks,
    projectcategories
WHERE
    MONTH(taskCompletedDate) = 2
        AND taskName LIKE '%APPROVAL: Page Idea submitted for approval%'
        AND projectCategoryName = 'SF Pages'
        AND taskStatus = 'completed'
GROUP BY taskStatus;

-- This script looks for the tasks with CREATIVE: Writer (commission and track) in the name for selected MONTH
SELECT 
    'Page commissioned' AS 'Stage',
    taskName AS 'TW Task Name',
    DATE_FORMAT(taskStartDate, '%M %y') AS 'Month',
    COUNT(*) AS 'Commissioned'
FROM
    tasks,
    projectcategories
WHERE
    MONTH(taskStartDate) = 2
        AND taskName LIKE '%CREATIVE: Writer (commission and track)%'
        AND projectCategoryName = 'SF Pages'
        AND (taskStatus = 'new'
        OR taskStatus = 'completed')
GROUP BY taskStatus;

-- This script looks for the tasks with CREATIVE: Writer (commission and track) in the name for selected MONTH
SELECT 
    'Page written' AS 'Stage',
    taskName AS 'TW Task Name',
    DATE_FORMAT(taskCompletedDate, '%M %y') AS 'Month',
    COUNT(*) AS 'Completed'
FROM
    tasks,
    projectcategories
WHERE
    MONTH(taskCompletedDate) = 2
        AND taskName LIKE '%CREATIVE: Writer (commission and track)%'
        AND projectCategoryName = 'SF Pages'
        AND taskStatus = 'completed'
GROUP BY taskStatus;

-- This script looks for the tasks with APPROVAL: Copy Approved in the name for selected MONTH
SELECT 
    'Copy approved' AS 'Stage',
    taskName AS 'TW Task Name',
    DATE_FORMAT(taskCompletedDate, '%M %y') AS 'Month',
    COUNT(*) AS 'Completed'
FROM
    tasks,
    projectcategories
WHERE
    MONTH(taskCompletedDate) = 2
        AND taskName LIKE '%APPROVAL: Copy Approved%'
        AND projectCategoryName = 'SF Pages'
        AND taskStatus = 'completed'
GROUP BY taskStatus;


-- This script looks for the tasks with GET LIVE: Is live? in the name for selected MONTH
SELECT 
    'Pages live' AS 'Stage',
    taskName AS 'TW Task Name',
    DATE_FORMAT(taskCompletedDate, '%M %y') AS 'Month',
    COUNT(*) AS 'Completed'
FROM
    tasks,
    projectcategories
WHERE
    MONTH(taskCompletedDate) = 2
        AND taskName LIKE '%GET LIVE: Is live?%'
        AND projectCategoryName = 'SF Pages'
        AND taskStatus = 'completed'
GROUP BY taskStatus;


I'm using MySQL Workbench and the above statements are outputting the results into separate tabs. Ideally I want to run these into one output tab for easy exporting to Excel etc.

Can someone talk me through how to turn the statements into one coherent code?
 
Associate
Joined
27 Sep 2006
Posts
500
You don't want to combine all those statements, although a union would work it's really ugly.

You should be able to do the whole lot with a single statement as below.

Bear in mind I'm a MSSQL guy, and I can't test this, hopefully this will work. Might need tweaking.

(guessed taskid is id column name)

Code:
SELECT	taskName,
	Stage,
	taskStatus
	COUNT(taskid) as TaskCount
FROM	tasks, projectcategories
WHERE (stage='completed' and taskCompletedDate between '2013-02-01 00:00:00' and '2013-02-28 23:59:59')
or (stage='commissioned' and taskStartDate between '2013-02-01 00:00:00' and '2013-02-28 23:59:59')
GROUP BY taskName, Stage, taskStatus
 
Back
Top Bottom