Ragesh
Ragesh

Reputation: 1

Even and Odd row fetching using sql query

Input Table :

page_no title
1   scrambled egg
2   Fondue
3   Sandwich
4   Tomato Soup
6   Liver

Required Query Result :

leftcolumn rightcolumn
null       scrambled egg
fondue     sandwich
tomato     null
liver      null

Upvotes: 0

Views: 143

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270011

You can use aggregation. Something like this:

select max(case when page_no % 2 = 0 then title end) as leftcolumn,
       max(case when page_no % 2 = 1 then title end) as rightcolumn      
from t
group by floor( page_no / 2);

The % is the modulo function. Some databases use MOD() instead.

Upvotes: 1

Related Questions