473,408 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

SQL Pagination With Dynamic Order By

I have a need to perform pagination while using dynamic sorting. As
an exmaple -

SELECT TOP(10) * FROM (
SELECT
TextColumn,
DecimalColumn,
ROW_NUMER() OVER (
ORDER BY
CASE @x
WHEN 1 THEN TextColumn
WHEN 2 THEN DecimalColumn
END
DESC
) AS SortOrder
FROM Table1
) AS Results WHERE SortOrder ( 10 ) ORDER BY SortOrder

This is obviously just some sample but an error is given because the
data type of the 2 columns used in the order by are different. It
works if I cast DecimalColumn to match the textcolumn but then the
sorting is wrong. Is there a way to do this in a single query with 2
different data types?

Thanks for your help.

Apr 4 '07 #1
2 4590
rm********@gmail.com wrote:
I have a need to perform pagination while using dynamic sorting. As
an exmaple -

SELECT TOP(10) * FROM (
SELECT
TextColumn,
DecimalColumn,
ROW_NUMER() OVER (
ORDER BY
CASE @x
WHEN 1 THEN TextColumn
WHEN 2 THEN DecimalColumn
END
DESC
) AS SortOrder
FROM Table1
) AS Results WHERE SortOrder ( 10 ) ORDER BY SortOrder

This is obviously just some sample but an error is given because the
data type of the 2 columns used in the order by are different. It
works if I cast DecimalColumn to match the textcolumn but then the
sorting is wrong. Is there a way to do this in a single query with 2
different data types?
Try this:

WHEN 2 THEN
right(' '+cast(DecimalColumn as varchar(20)), 20)
Apr 4 '07 #2
> Is there a way to do this in a single query with 2 different data types? <<

The CASE expression has to have one and only one data type, just like
any other expression in SQL and strongly typed languages.

The trick is to keep both columns and NULL one of the out or make it a
constant, somethign like this:

ROW_NUMBER() OVER (ORDER BY
CASE WHEN @x = 1 THEN decimal_col ELSE CAST(NULL AS DECIMAL(s,p)) END,
CASE WHEN @x = 2 THEN text_col ELSE CAST (NULL AS CHAR(n)) END) AS
sort_order

The casting is a bit redundant, but a nice reminder.

Apr 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kurzman | last post by:
Let me know what you think about the following code: DECLARE @MaxIdValue int DECLARE @MaxSortFieldValue nvarchar(50) SELECT TOP 1 @MaxIdValue = , @MaxSortFieldValue = FROM ( SELECT TOP...
2
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't...
1
by: Ed Jay | last post by:
I generate a DHTML page (a medical report) with dynamically generated text based on user input (answers to questions). The page length changes dynamically. I desire that when the page is printed...
4
by: Ed Jay | last post by:
I generate a DHTML page (a medical report) with dynamically generated text based on user input (answers to questions). The page length changes dynamically. I desire that when the page is printed...
1
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that...
5
by: sandipon | last post by:
I have used the following Pagination script to display database records in a series of pages, but only the first page shows up properly, and on clicking the links of subsequent pages or the NEXT ,...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
4
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
2
by: kkshansid | last post by:
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.