dbTalk Databases Forums  

Help with order by case... syntax with mimer db

comp.databases comp.databases


Discuss Help with order by case... syntax with mimer db in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
dos.fishing@gmail.com
 
Posts: n/a

Default Help with order by case... syntax with mimer db - 07-10-2007 , 05:37 AM






I'm working with mimer mobile db and need som help with the following
statement in a procedure.

I have the following today:
SELECT column1 FROM table1
ORDER BY case when some_expression then column2 else column3
end;

I would like to do the following but the syntax seems to be wrong:

SELECT column1 FROM table1
ORDER BY case when some_expression then coloumn1, column2 else
coloumn3, column4 end;

i.e. add some more columns to the order expression.

Alternatively, I would like to do something like:

SELECT column1 FROM table1{INDEX case when some_expression then index1
else index2 end};

where index1 and index2 are indexes created for table1.

Any idea's of how to accomplish this?
Cheers
//Kid


Reply With Quote
  #2  
Old   
Lennart
 
Posts: n/a

Default Re: Help with order by case... syntax with mimer db - 07-10-2007 , 05:42 AM






dos.fishing (AT) gmail (DOT) com wrote:
Quote:
I'm working with mimer mobile db and need som help with the following
statement in a procedure.

I have the following today:
SELECT column1 FROM table1
ORDER BY case when some_expression then column2 else column3
end;

I would like to do the following but the syntax seems to be wrong:

SELECT column1 FROM table1
ORDER BY case when some_expression then coloumn1, column2 else
coloumn3, column4 end;

i.e. add some more columns to the order expression.

Alternatively, I would like to do something like:

SELECT column1 FROM table1{INDEX case when some_expression then index1
else index2 end};

where index1 and index2 are indexes created for table1.

Any idea's of how to accomplish this?
Cheers
//Kid

Try:

SELECT column1 FROM table1
ORDER BY
case when some_expression then coloumn1 else column3 end,
case when some_expression then coloumn2 else column4 end

/Lennart


Reply With Quote
  #3  
Old   
dos.fishing@gmail.com
 
Posts: n/a

Default Re: Help with order by case... syntax with mimer db - 07-10-2007 , 06:17 AM



Thanks Lennart, that works! =)

But unfortunatly, I still have problems.

The reason I wanted to order by either (column1,column2) or
(column3,column4) was because I have indexes on these coloumns:
CREATE INDEX index1 ON table1(column1,column2);
CREATE INDEX index2 ON table1(column3,column4);

But it seems like the compiler/optimizer doesn't understand that I
want to use the index when using you solution (and I'm not blaming
it!)
Any other ideas?

Cheers
//Kid

Quote:
Try:

SELECT column1 FROM table1
ORDER BY
case when some_expression then coloumn1 else column3 end,
case when some_expression then coloumn2 else column4 end

/Lennart



Reply With Quote
  #4  
Old   
Lennart
 
Posts: n/a

Default Re: Help with order by case... syntax with mimer db - 07-10-2007 , 07:19 AM



dos.fishing (AT) gmail (DOT) com wrote:
Quote:
Thanks Lennart, that works! =)

But unfortunatly, I still have problems.

The reason I wanted to order by either (column1,column2) or
(column3,column4) was because I have indexes on these coloumns:
CREATE INDEX index1 ON table1(column1,column2);
CREATE INDEX index2 ON table1(column3,column4);

But it seems like the compiler/optimizer doesn't understand that I
want to use the index when using you solution (and I'm not blaming
it!)
Any other ideas?
I assume the optimizer uses the index if you do either:

order by column1, column2

or:

order by column3, column4

correct?


What is some_expression? Can you determine it in advance and either:

order by colomn1, column2

or

order by colomn3, column4

What type is column1, 2, 3 and 4? If they are some kind of chartype
perhaps it helps with:

order by
case when some_expression then coloumn1 || column2 else column3 ||
column4 end



Quote:
Cheers
//Kid


Try:

SELECT column1 FROM table1
ORDER BY
case when some_expression then coloumn1 else column3 end,
case when some_expression then coloumn2 else column4 end

/Lennart



Reply With Quote
  #5  
Old   
Lennart
 
Posts: n/a

Default Re: Help with order by case... syntax with mimer db - 07-10-2007 , 07:23 AM



dos.fishing (AT) gmail (DOT) com wrote:
Quote:
Thanks Lennart, that works! =)

But unfortunatly, I still have problems.

The reason I wanted to order by either (column1,column2) or
(column3,column4) was because I have indexes on these coloumns:
CREATE INDEX index1 ON table1(column1,column2);
CREATE INDEX index2 ON table1(column3,column4);

But it seems like the compiler/optimizer doesn't understand that I
want to use the index when using you solution (and I'm not blaming
it!)
Any other ideas?
On second thought, why would it use the index to order the result set?
Does it really use the index if you

order by column1, column2?


/Lennart



Quote:
Cheers
//Kid


Try:

SELECT column1 FROM table1
ORDER BY
case when some_expression then coloumn1 else column3 end,
case when some_expression then coloumn2 else column4 end

/Lennart



Reply With Quote
  #6  
Old   
dos.fishing@gmail.com
 
Posts: n/a

Default Re: Help with order by case... syntax with mimer db - 07-10-2007 , 08:36 AM



Yes, it uses the index to scan through the table and not having to
sort/order afterwards.

I'm solving it by duplicating code:

if some_expression then
..
..
SELECT column1 FROM table1
ORDER BY coloumn1, column2;
..
..
else
..
..
SELECT column1 FROM table1
ORDER BY coloumn3, column4;
..
..
endif

Thanks for your help!
//Kid

Quote:
On second thought, why would it use the index to order the result set?
Does it really use the index if you

order by column1, column2?

/Lennart


Reply With Quote
  #7  
Old   
Lennart
 
Posts: n/a

Default Re: Help with order by case... syntax with mimer db - 07-10-2007 , 09:58 AM



dos.fishing (AT) gmail (DOT) com wrote:
Quote:
Yes, it uses the index to scan through the table and not having to
sort/order afterwards.
Yes, but I'm not sure that is the most cost efficient way to do it at
all times. I dont know Mimer so I can only guess that by doing an index
scan you will get a lot of random touch on the data pages. A sequential
scan on the data pages might be more efficient, since you are reading
the whole table.

Anyhow, glad you solved it

/Lennart


Quote:
I'm solving it by duplicating code:

if some_expression then
..
..
SELECT column1 FROM table1
ORDER BY coloumn1, column2;
..
..
else
..
..
SELECT column1 FROM table1
ORDER BY coloumn3, column4;
..
..
endif

Thanks for your help!
//Kid

On second thought, why would it use the index to order the result set?
Does it really use the index if you

order by column1, column2?

/Lennart


Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.