Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a below scenario in sql server. Please help me in this

SQL
SELECT ListItem as ColumnName from (
SELECT REPLACE(ColumnsList, '$', ListItem) ListItem FROM dbo.MODELS_COLUMNS_STRUCTURE MCS
CROSS JOIN List_Resources WHERE Type = 'country data' ) A


For this query I'm getting result as

VB
Country
Country
Country
Region
Region
Region
Currency
Currency
Currency
Allocation
Allocation
Allocation
Business
Business
Business
CurrencyUSDRate
CurrencyUSDRate
CurrencyUSDRate
CurrencyAUDRate
CurrencyAUDRate
CurrencyAUDRate
CurrencyEURRate
CurrencyEURRate
CurrencyEURRate


I want output as
VB
Country
Region
Currency
Allocation
Business
CurrencyUSDRate
CurrencyAUDRate
CurrencyEURRate


Thanks & Regards,
Mathi.
Posted
Comments
The14thNoah 19-Feb-14 1:19am    
why all the suggested solutions were same? . .are they copying the solution of other's answer?
Andrius Leonavicius 19-Feb-14 14:55pm    
I think they all copied questioner's SELECT statement and added keyword DISTINCT. :)
The14thNoah 19-Feb-14 18:45pm    
hahahaha even they see all the previous answer they still post the same answer? :)
Andrius Leonavicius 19-Feb-14 19:19pm    
No, I don't think so. :) I can't speek for them, but I can tell something from my experience. Once I saw some easy question, which was posted like 5 minutes ago (0 solutions). Then I started writing a solution. After I posted it, my solution was 4th. Why? Because that data wasn't automatically refreshed while I was writing and others posted their solutions. Probably the same thing here...
The14thNoah 19-Feb-14 19:32pm    
ok. . .but you can see the time interval when they post it.Im not arguing for something,Im just find it funny. . .thanks for the reply :)

Try:
SQL
SELECT DISTINCT ListItem as ColumnName from (
SELECT REPLACE(ColumnsList, '$', ListItem) ListItem FROM dbo.MODELS_COLUMNS_STRUCTURE MCS
CROSS JOIN List_Resources WHERE Type = 'country data' ) A
 
Share this answer
 
Comments
Mathi2code 19-Feb-14 0:50am    
If I use distinct, it automatically does order by asc, and the result will be like below.

Allocation
Business
Country
Currency
CurrencyAUDRate
CurrencyEURRate
CurrencyUSDRate
Region

but I want in the same order as mentioned in the question. Is this possible.
OriginalGriff 19-Feb-14 4:39am    
Not necessarily. The problem is that unless you specify an order with an ORDER BY clause, then SQL is at liberty to return rows in whatever order is best for it: which in the case of a DISTINCT query is sorted, because that is the easiest way to work out the DISTINCT requirement: you sort the inputs, then the duplicates are next to each other and easy to discard.
Unless your table includes some information which allows you to order the rows specifically, you can't enforce any particular order on SQL output - even if you don't use DISTINCT than this applies!
Mathi2code 19-Feb-14 8:17am    
Ok :( Thanks.

Regards,
Mathi.
OriginalGriff 19-Feb-14 9:32am    
You're welcome!
Mathi2code 20-Feb-14 6:17am    
Thanks I've added an ID column to get it order by ID as suggested.
why don't you use

SQL
SELECT DISTINCT ListItem as ColumnName from (
SELECT REPLACE(ColumnsList, '$', ListItem) ListItem FROM dbo.MODELS_COLUMNS_STRUCTURE MCS
CROSS JOIN List_Resources WHERE Type = 'country data' ) A
 
Share this answer
 
Comments
Mathi2code 19-Feb-14 0:52am    
If I use distinct, it automatically does order by asc, and the result will be like below.

Allocation
Business
Country
Currency
CurrencyAUDRate
CurrencyEURRate
CurrencyUSDRate
Region

but I want in the same order as mentioned in the question. Is this possible.
Use the below query

SQL
SELECT DISTINCT  ListItem as ColumnName from (
SELECT REPLACE(ColumnsList, '$', ListItem) ListItem FROM dbo.MODELS_COLUMNS_STRUCTURE MCS
CROSS JOIN List_Resources WHERE Type = 'country data' ) A
 
Share this answer
 
Comments
Mathi2code 19-Feb-14 0:52am    
If I use distinct, it automatically does order by asc, and the result will be like below.

Allocation
Business
Country
Currency
CurrencyAUDRate
CurrencyEURRate
CurrencyUSDRate
Region

but I want in the same order as mentioned in the question. Is this possible.
Gauri Chodanker 20-Feb-14 7:46am    
Can you please provide me sample data and table name. I shall write the query for that.

Does your table MODELS_COLUMNS_STRUCTURE use some number field like primarykey. And what is the order in which your data is stored. Is it country first and then allocation or the order in which you want the data. If that is so then use the below query

SELECT DISTINCT somenumberfield, ListItem as ColumnName from (
SELECT REPLACE(ColumnsList, '$', ListItem) ListItem FROM dbo.MODELS_COLUMNS_STRUCTURE MCS
CROSS JOIN List_Resources WHERE Type = 'country data' ) A


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900