473,473 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

'case column' in HAVING clause

5 New Member
Hello,

I'm having problem migrating to postgreSQL from MySQL, some of my queries does not work in postgres.

Query(simplified):
Expand|Select|Wrap|Line Numbers
  1. SELECT t1.bus_stop, t1.time,
  2. (case when t1.time >2200 then t1.time + 10000 else t1.time END) AS sort_column
  3. FROM routes AS t1
  4. GROUP BY  t1.bus_stop, t1.time
  5. HAVING sort_column > 2200 ORDER BY sort_column  LIMIT 1
Error:
Expand|Select|Wrap|Line Numbers
  1. ERROR:  column "sort_column" does not exist
How to use a 'case column' in HAVING clause?
Apr 3 '08 #1
3 7917
rski
700 Recognized Expert Contributor
Maybe like this

Expand|Select|Wrap|Line Numbers
  1. SELECT i,j, sort_kolumn from (
  2. SELECT t1.bus_stop as i, t1.time as j,
  3. (case when t1.time >2200 then t1.time + 10000 else t1.time END) AS sort_column FROM routes AS t1) foo
  4. GROUP BY  i,j
  5. HAVING sort_column > 2200 ORDER BY sort_column  LIMIT 1
Does it work?
Apr 3 '08 #2
Philia
5 New Member
Maybe like this

Expand|Select|Wrap|Line Numbers
  1. SELECT i,j, sort_kolumn from (
  2. SELECT t1.bus_stop as i, t1.time as j,
  3. (case when t1.time >2200 then t1.time + 10000 else t1.time END) AS sort_column FROM routes AS t1) foo
  4. GROUP BY  i,j
  5. HAVING sort_column > 2200 ORDER BY sort_column  LIMIT 1
Does it work?
Yes, it works. Thank you rski. :)
But for me it looks like way around the problem, is there a better way to do this?
I'm migrating to postgres to speed up my aplication so I'm concerned about the speed of this query.
Apr 3 '08 #3
rski
700 Recognized Expert Contributor
If i'm right HAVING clause can't have CASE clause.
Say what do you want to achieve and what is table structure, maybe there is another solution than yours.
Apr 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
2
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate =...
3
by: jason | last post by:
I'll try to explain in detail what it is I'm struggling with. I created a SQL database with one table called CallSheet which contains the following columns: Caller ID: <--Primary Key, auto...
7
by: mandible | last post by:
Hello I'm trying to have some control on how my data is ordered depending on an input parameter my question is in a stored procedure how can I do something like this at the end of my...
6
by: Steve | last post by:
I realize that this probably isn't a best practice, but I'm working with legacy code that has a query stored in one column of a table. Because the queries vary, the JSP page that selects a query...
6
by: axelsino | last post by:
I know this has been asked before and yes, I have read the section in the documentation about it. But, my question is: If I have setup mysql with strict_trans_tables, will MySQL allow "null"...
5
by: Sascha.Moellering | last post by:
Hi, I receive the error code SQL0338N if I try to compile this statement (part of the statement): .... left outer join lateral (SELECT * FROM LIZSYSABA.VWZL0359TBS WHERE tbs_name = CASE WHEN...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.