SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Rearrange rows in resultant table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

amalipriya
Starting Member

3 Posts

Posted - 11/29/2012 :  09:47:48  Show Profile  Reply with Quote
Dear All

Below is my resultant table:
I want to order the resultant rows dynamically.


CustomerID   DynamicOrder
75	     1
76	     1
77	     1
76	     2
77	     2
78	     2
80           3
81           3

If I pass DynamicOrder =2

I need the result as below


CustomerID   DynamicOrder
75	     2
76	     2
77	     3
76	     1
77	     1
78	     1
80           3
81           3


And also distinct of customerID

Please do help me.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/29/2012 :  10:09:32  Show Profile  Reply with Quote
After having read through your post couple of times, I am unable to understand the logic you are using to get the results. What are the rules to be used to change the DynamicOrder associated with CustomerID=75, for example?
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1721 Posts

Posted - 11/29/2012 :  18:35:46  Show Profile  Reply with Quote
I also am unclear on your requirements but perhaps this is a small step in the right direction. If not, feel free to disregard entirely...
order by
   case
      when DynamicOrder = @DynamicOrder then 1
      else 2
      end,
   DynamicOrder,
   CustomerID


=================================================
Hear the sledges with the bells - silver bells!
What a world of merriment their melody foretells!
Go to Top of Page

amalipriya
Starting Member

3 Posts

Posted - 12/01/2012 :  01:41:58  Show Profile  Reply with Quote
Sorry For not making you to understand my requirements.

Let me explain. This is all about customers.
I have 4 searches as below:

Customer who did not made sales - between date
Customer never called
Called Customer - between dates
Customer Added - Between dates

So I have 4 queries for above search.Because I have to search in different tables for above conditions.
I have to combined all the results in one table and show.

My requirement is, I need to order the search query itself. For example, I need the customers who never made order on top and it will be dynamic. Next time I need the called customer should appear on top. How to I do this?

Hope it's clear now.

Edited by - amalipriya on 12/01/2012 01:43:20
Go to Top of Page

Elizabeth B. Darcy
Starting Member

United Kingdom
39 Posts

Posted - 12/01/2012 :  08:26:46  Show Profile  Reply with Quote
Is there a column in your table that indicates the customer action?

Or, perhpas is the value in the DynamicOrder column is indicative of the customer action? If that indeed is so, the example you posted does not seem to be consistent with that conjecture (because, as Sunita pointed out in her response, CustomerID = 75 has DynamicOrder = 1 in the first result set and DynamicOrder = 2 in the second result set).

______________________________________________
-- "If a book is well written, I always find it too short"

Edited by - Elizabeth B. Darcy on 12/01/2012 08:27:31
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000