I have an existing SQL query that works fine in SQL Server, that I would like to make work in MySQL. I have cleaned it up a bit, and fiddled with the back-ticks for column / table names, etc. but cannot get it to work.
Here is the statement in question:
SELECT `EmployeeID`
FROM `Employee`
WHERE
(
(
SELECT COUNT(*) AS A
FROM `TimeClock`
WHERE (`EmployeeID` = `Employee.EmployeeID`)
AND (`InOut` = 'True')
) > (
SELECT COUNT(*) AS B
FROM `TimeClock` AS `TimeClock_1`
WHERE (`EmployeeID` = `Employee.EmployeeID`)
AND (`InOut` = 'False')
)
)
This should return any EmployeeID‘s that have more InOut = True than InOut = False in the TimeClock table.
Thanks.
This should provide you with the
EmployeeIdvalues where there are more values ofInOutTrue than FalseThe two alternatives I came up with show similar performance on my VERY SMALL unindexed test data. I would recommend you test them against your own data to see if they make any difference:
This one is pretty much the same, but using total record count instead of InOutFalse.
I am not sure if MySQL will require the
CASTI used here, but SQL Server did. (That is what I tested these with) It could not perform aSUMon aBITcolumn.If all three perform similarly, it then comes down to personal preference.