MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 6th, 2012, 05:23 PM
seito seito is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 1 seito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 43 sec
Reputation Power: 0
Unhappy What could be wrong with this query

Hy guys. I'm lost here. I had this multiple query cascade but then I changed my back end from native access to MySQL. And cascade of queries suddenly became way too slow. So I sat down and I'm trying to "translate/rewrite" them into one big pass-through query with multiple sub-queries. Actually I had a great success and everything before this last step runs without problem and EXTREMELY fast ! Hurray for MySQL!

But now I'm stuck in one LAST FINAL STEP !

What I had is:
SQLString = "SELECT T1.DATUM, T1.ID_SeznamURE, T1.ZasedeniTermini AS ZasedeniTermini, " & _
"(SELECT Avg(ZasedeniTermini) " & _
"FROM " & sql_QryURNIK_ZASEDENI_TERMINI_SESTEVEK & " AS T " & _
"WHERE T.DATUM = T1.DATUM AND T.ID_SeznamURE BETWEEN switch(T1.ID_SeznamURE - T2.mini <= 2, T2.mini, T2.maxi - T1.ID_SeznamURE <= 2, T2.maxi - 4, true, T1.ID_SeznamURE - 2) AND switch(T2.maxi - T1.ID_SeznamURE <= 2, T2.maxi, T1.ID_SeznamURE - T2.mini <= 2, T2.mini + 4, true, T1.ID_SeznamURE + 2) " & _
") AS UrnoPovprecje " & _
"FROM " & sql_QryURNIK_ZASEDENI_TERMINI_SESTEVEK & " AS T1 INNER JOIN (SELECT D.DATUM, MIN(D.ID_SeznamURE) AS mini, MAX(D.ID_SeznamURE) AS maxi FROM " & sql_QryURNIK_ZASEDENI_TERMINI_SESTEVEK & " AS D GROUP BY D.DATUM) AS T2 ON T1.DATUM = T2.DATUM " & _
"ORDER BY T1.DATUM, T1.ID_SeznamURE;"

But since switch() is function that MySQL does not recognize I got an error.

So I searched the internet and as I understand I should use CASE clause for same results. So I came up with that:

SQLString = "SELECT T1.DATUM, T1.ID_SeznamURE, T1.ZasedeniTermini AS ZasedeniTermini, " & _
"(SELECT Avg(ZasedeniTermini) " & _
"FROM " & sql_QryURNIK_ZASEDENI_TERMINI_SESTEVEK & " AS T " & _
"WHERE T.DATUM = T1.DATUM AND T.ID_SeznamURE BETWEEN (CASE WHEN T1.ID_SeznamURE - T2.mini <= 2 THEN T2.mini " & _
"WHEN T2.maxi - T1.ID_SeznamURE <= 2 THEN T2.maxi - 4 " & _
"WHEN true THEN T1.ID_SeznamURE - 2 " & _
"ELSE BEGIN END) " & _
"END CASE) " & _
"AND (CASE WHEN T2.maxi - T1.ID_SeznamURE <= 2 THEN T2.maxi " & _
"WHEN T1.ID_SeznamURE - T2.mini <= 2 THEN T2.mini + 4 " & _
"WHEN true THEN T1.ID_SeznamURE + 2 " & _
"ELSE BEGIN END) " & _
"END CASE) " & _
") AS UrnoPovprecje " & _
"FROM " & sql_QryURNIK_ZASEDENI_TERMINI_SESTEVEK & " AS T1 INNER JOIN (SELECT D.DATUM AS DATUM, MIN(D.ID_SeznamURE) AS mini, MAX(D.ID_SeznamURE) AS maxi FROM " & sql_QryURNIK_ZASEDENI_TERMINI_SESTEVEK & " AS D GROUP BY D.DATUM) AS T2 ON T1.DATUM = T2.DATUM " & _
"ORDER BY T1.DATUM, T1.ID_SeznamURE;"

And now I'm really lost since I can't see any mistake and I still get an error. This time it says:
You have an error in your SQL syntax.; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AS UrnoPovprecje FROM (SELECT c.DATUM, c.ID_SeznamURE, Count(c.ID_SeznamURE) A' at line 1

But I think problem is still in syntax or use of CASE clause. But hey, I could be wrong ! I have no idea what could be going on here, so if any of you MySQL jedis could lend me some of your force and show me the path, I would be really grateful!

THANK YOU!

Reply With Quote
  #2  
Old July 6th, 2012, 05:42 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 53 m 41 sec
Reputation Power: 4140
could you do us a favour and post only clean sql, without all those quotes and ampersands

ideally, you should test your clean sql directly in the mysql engine, before embedding it in your perl code or whatever that is
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > What could be wrong with this query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap