tags:

views:

141

answers:

2

I currently have a table(/grid) of data that I can page, filter and sort. On the table I also have a built in checkbox column. Paging, filtering and sorting right now happen within the SQL query. What I want to be able to do is sort by the clicked items in my checkbox column. This would bring all items that are checked to the front of the table. Since the checkboxes themselves are all client side I can't just tell the SQL query to sort by a column that doesn't exist (maybe I need to dynamically create one?)

In essence what I think would happen is that the checked boxes ID value would be sent into the query the the SQL query itself would somehow know to sort by that first and then by the others specified.

Something like, where 1, 2, and 3 are clicked:

SELECT * FROM Blah ORDER BY (SELECT ID FROM Blah WHERE ID IN (1,2,3)), AnotherColumnToSort

That's the plan anyway, anyone have any ideas on how to actually accomplish that?

Update: Smack I'm on an Oracle DB and not SQL Server like I had thought.

+5  A: 

Set a BIT field to 1 when the row is checked, 0 otherwise, then order by that field first:

SELECT *
FROM Blah
ORDER BY Checked DESC, AnotherColumnToSort

If you don't want to (or can't) store whether the row is checked in the table, derive it using a CASE clause:

SELECT *
FROM Blah
ORDER BY
  CASE
    WHEN ID IN (1, 2, 3) THEN 1
    ELSE 0
  END DESC,
  AnotherColumnToSort
Welbog
I think you're on to something. I'll give this a try. My own thoughts were involving 2 duplicate queries with a UNION and REMOVE.
rball
Crap, I just remembered that I'm working with a Oracle DB and not SQL Server. I think the idea is still right though, just not sure about the syntax.
rball
Ok, looks like Oracle's CASE seems really similar. Phew!
rball
Worked like a charm, thanks!
rball
+1  A: 

I would avoid re-querying the data you already have -- it's not going to scale well and you will only have problems with it. This type of functionality is available in a lot of 3rd-party grids (I would venture that damn near all the pay-for grids have this).

Austin Salonen
What I'm doing is only getting records 1-10 out of 1 million. To go to the next page, or to filter that data I have to re-query the data. Seems like another question, but how would I get around that?
rball
Just as a note, my current way is working quite well. True more database hits (was thinking about caching eventually), but the page load time has gone down from 20 minutes to a second or so.
rball
I think you may be surprised by the functionality of some 3rd-party controls. They may be expensive but your time is valuable and they could save you a lot. If you're a .Net developer, check out http://www.telerik.com/products/aspnet-ajax/grid.aspx.
Austin Salonen
BTW, I don't work for them or use their products but I do see/hear a lot of ads for their products and they look nice.
Austin Salonen
I've used Telerik stuff. Very feature rich and nice as long as you don't need to manipulate them closely. They have a lot of terribly annoying edge-case bugs that no one has been able to explain. So if as long as you're not trying to get fancy, they're good stuff.
Welbog
Actually, before I went this whole route I told the powers that be to just buy something. I evaluated Telerik, DevExpress, ComponentArt etc, gave recommendations, and then I was order to build my own. Go figure. Honestly though, how would they handle it differently?
rball