cgcmq
asked on
Convert sp from MS SQL to MySQL (5.0)
Can anyone help me convert a stored procedure that includes a CTE from MS SQL to MySQL 5.0?
WITH CompanyListings AS
(
SELECT
T1.ComanyID, T1.CompanyName, T1.CompanyURL, T1.School, T2.DisplayAlways,
MAX(T3.SessionEndDate) OVER (PARTITION BY T1.ComanyID) AS DisplayDate
FROM TrainingCompanies T1
LEFT JOIN TrainingCourses T2 ON T1.ComanyID = T2.Course_CompanyID
LEFT JOIN TrainingSessions T3 ON T2.CourseID = T3.Session_CourseID
)
SELECT DISTINCT
ComanyID, CompanyName, CompanyURL, ISNULL(School,'') AS School
FROM CompanyListings
WHERE DisplayDate > DATEADD(D,-3, GETDATE()) OR DisplayAlways = 1
ORDER BY CompanyName
ASKER
Are you certain that your solution is MySQL? It looks like an MS SQL query.
yes i am
read this : http://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html
read this : http://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html
ASKER
Sorry - I don't work with MySQL much & do not know the query syntax very well. When I cut & pasted the query & ran it (using Navicat) I received the following error:
1064 - You have an error in your SQL syntax... near '') AS School
FROM (
SELECT
1064 - You have an error in your SQL syntax... near '') AS School
FROM (
SELECT
try this
SELECT DISTINCT
ComanyID, CompanyName, CompanyURL, IfNULL(School,'') AS School
FROM (
SELECT
T1.ComanyID, T1.CompanyName, T1.CompanyURL, T1.School, T2.DisplayAlways,
MAX(T3.SessionEndDate) OVER (PARTITION BY T1.ComanyID) AS DisplayDate
FROM TrainingCompanies T1
LEFT JOIN TrainingCourses T2 ON T1.ComanyID = T2.Course_CompanyID
LEFT JOIN TrainingSessions T3 ON T2.CourseID = T3.Session_CourseID
) CompanyListings
WHERE DisplayDate > DATEADD(D,-3, CURDATE())OR DisplayAlways = 1
ORDER BY CompanyName
ASKER
That solved the initial error - thank you.
It now stumbles at:
(PARTITION BY T1.ComanyID) AS DisplayDate
FROM TrainingCompanies T1
It now stumbles at:
(PARTITION BY T1.ComanyID) AS DisplayDate
FROM TrainingCompanies T1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Like I said, I am not familiar with MySQL so it is going to take me some time to go through this. I will get back to you shortly.
ASKER
I downloaded a syntax converter (SwissSQL Console) and it gave me an output very similar to what you had which was of limited use as it clearly is not functional MySQL.
Instead of following the example at Xaprb, I re-wrote the query in MS SQL so that it did not use a CTE or 'partition over'. From that I was able to more easily convert it to MySQL.
SELECT DISTINCT
ComanyID,
CompanyName,
CompanyURL,
COALESCE(School, '') AS School
FROM TrainingCompanies T1
LEFT JOIN TrainingCourses T2 ON T1.ComanyID = T2.Course_CompanyID
WHERE CourseID IN
(
SELECT Session_CourseID
FROM TrainingSessions
WHERE SessionEndDate > DATE_ADD((CURRENT_TIMESTAM P), INTERVAL - 3 DAY)
) OR DisplayAlways = 1
ORDER BY CompanyName
Instead of following the example at Xaprb, I re-wrote the query in MS SQL so that it did not use a CTE or 'partition over'. From that I was able to more easily convert it to MySQL.
SELECT DISTINCT
ComanyID,
CompanyName,
CompanyURL,
COALESCE(School, '') AS School
FROM TrainingCompanies T1
LEFT JOIN TrainingCourses T2 ON T1.ComanyID = T2.Course_CompanyID
WHERE CourseID IN
(
SELECT Session_CourseID
FROM TrainingSessions
WHERE SessionEndDate > DATE_ADD((CURRENT_TIMESTAM
) OR DisplayAlways = 1
ORDER BY CompanyName
Open in new window