Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using MySql with Workbench

I have not been able to use the MAX(C.LastContact) agregate in the WHERE clause which is needed to get the correct results. I am thinking another way to do this might be to set up IF statements in the SELECT statement based on the value of a parameter passed into the Stored Procedure. Is this possible. I have not been able to get it to work. I am using Dynamic SQL. Below is a portion of the code:


SET SelectStmt =


"SELECT COUNT(distinct(C.LocationID))AS 'Total Locations',

IF Parameter1 = 'all'
THEN do this


IF((select max(TIMESTAMPDIFF(minute, C1.LastContact, NOW() ))
from computers c1 where c1.locationid = c.locationid) >= @TimeToFail,'Fail','OK')
AS 'Status',


IF Parameter1 = 'fail'
THEN do this


IF Parameter1 = 'ok'
THEN do this


FROM
Computers AS C ";

SET WhereClause = CONCAT(WhereClause, " IFNULL(TIMESTAMPDIFF(minute, C.LastContact, NOW()),0) ",
'>=',@TimeToFail);

SET GroupBy = " GROUP BY C.LocationID ";
Posted

1 solution

you can use CASE WHEN THEN statement to achieve this functionality. an example for this is as follows.

SQL
SET SelectStmt =


"SELECT COUNT(distinct(C.LocationID))AS 'Total Locations',

CASE

WHEN Parameter1 = 'all'
THEN --do this


--IF((select max(TIMESTAMPDIFF(minute, C1.LastContact, NOW() ))
--from computers c1 where c1.locationid = c.locationid) >= @TimeToFail,'Fail','OK')
--AS 'Status',


WHEN Parameter1 = 'fail'
THEN --do this


WHEN Parameter1 = 'ok'
THEN --do this

END AS 'Status'

FROM
Computers AS C ";

SET WhereClause = CONCAT(WhereClause, " IFNULL(TIMESTAMPDIFF(minute, C.LastContact, NOW()),0) ",
'>=',@TimeToFail);

SET GroupBy = " GROUP BY C.LocationID ";


please check and reply if it works.
 
Share this answer
 
Comments
SQL Ed 26-Dec-12 16:05pm    
After trying this I received a Unknown column 'Parameter1' in field list error!!
[no name] 27-Dec-12 7:37am    
here in this case, parameter1 can either be a variable or a column name, please make sure it is either defined or a column of the table

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900