Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Rearrange rows in resultant table

Author  Topic 

amalipriya
Starting Member

3 Posts

Posted - 2012-11-29 : 09:47:48
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-29 : 10:09:32
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
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2012-11-29 : 18:35:46
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...[CODE]order by
case
when DynamicOrder = @DynamicOrder then 1
else 2
end,
DynamicOrder,
CustomerID[/CODE]

=================================================
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 - 2012-12-01 : 01:41:58
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.
Go to Top of Page

Elizabeth B. Darcy
Starting Member

39 Posts

Posted - 2012-12-01 : 08:26:46
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"
Go to Top of Page
   

- Advertisement -