Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below records in my datatable (in single column)
S00118-JR10-B-NO-R-JS-26
S00118-JR10-B-NO-R-JS-22
S00118-JR10-B-NO-R-JS-24
S00201-0110-B-NO-R-ST-16
S00201-0110-B-NO-R-ST-15
S00201-JRSR-B-RE-R-TS-20
S00118-0110-B-NO-R-JK-46
S00201-0104-M-NO-R-SH-11
S00118-0110-B-NO-R-JK-34


I want distinct record with higest number at the end as follows
S00118-JR10-B-NO-R-JS-26
S00201-0110-B-NO-R-ST-16
S00201-JRSR-B-RE-R-TS-20
S00118-0110-B-NO-R-JK-46
S00201-0104-M-NO-R-SH-11
Posted
Updated 3-Mar-16 21:13pm
v3
Comments
dan!sh 3-Mar-16 5:42am    
Is this one column?
Devraj Kapdi 3-Mar-16 5:45am    
yes
dan!sh 3-Mar-16 5:49am    
What is there in other columns? Update the question with entire table structure.
Devraj Kapdi 3-Mar-16 5:52am    
the datatable containing only single column.

If they are all two digit, then try:
SQL
SELECT MAX(MyColumn) FROM MyTable
GROUP BY SUBSTRING(MyColumn, 1, 21)

If they aren't, then it becomes a PITA because the default sort order for strings is based on the difference between the first character pair that don't match - not a numeric value.

[edit]
Thinking about it, not that much of a PITA:
SQL
SELECT  Prefix + CAST(MAX(Val) AS NVARCHAR)  
FROM 
   (SELECT SUBSTRING(MyColumn, 1, 22) AS Prefix, CAST(SUBSTRING(MyColumn, 23, 9999) AS INT) AS Val FROM MyTable) a
GROUP BY Prefix

[/edit]
 
Share this answer
 
v2
Comments
Devraj Kapdi 3-Mar-16 6:04am    
I want it with LINQ in C#
You can do it like this:

C#
List<string> filtered = test.OrderByDescending(x => Convert.ToInt32(x.Substring(x.LastIndexOf("-") + 1))).GroupBy(x => x.Substring(0, x.LastIndexOf("-"))).Select(x => x.First()).ToList();


If this does not work for you, try fixing is yourself.
 
Share this answer
 
Comments
Devraj Kapdi 3-Mar-16 7:00am    
thank u d@nish it worked for me with little modification for datatable as...

List <datarow> filtered = dtOrderHistotyFinal.AsEnumerable().ToList().OrderByDescending(x => Convert.ToInt32(x["col1"].ToString().Substring(x["col1"].ToString().LastIndexOf("-") + 1))).GroupBy(x => x["col1"].ToString().Substring(0, x["col1"].ToString().LastIndexOf("-"))).Select(x => x.First()).ToList();
Here is other way to made it

I create your table with that column.

SELECT Mycolumn FROM MyTable;


Mycolumn
S00118-JR10-B-NO-R-JS-26
S00118-JR10-B-NO-R-JS-22
S00118-JR10-B-NO-R-JS-24
S00201-0110-B-NO-R-ST-16
S00201-0110-B-NO-R-ST-15
S00201-JRSR-B-RE-R-TS-20
S00118-0110-B-NO-R-JK-46
S00201-0104-M-NO-R-SH-11
S00118-0110-B-NO-R-JK-34


And here is the conde I use to get your result requsted with a CTE.

WITH CTE(LEFTSTR, RIGHTSTR, Dupplicate)
AS
(SELECT DISTINCT
		LEFT(Mycolumn,LEN(Mycolumn)-(CHARINDEX('-',REVERSE(Mycolumn))-1))AS LEFTSTR,
		RIGHT(Mycolumn,(CHARINDEX('-',REVERSE(Mycolumn))-1))AS RIGHTSTR,
		ROW_NUMBER() OVER(PARTITION BY LEFT(Mycolumn,LEN(Mycolumn)-(CHARINDEX('-',REVERSE(Mycolumn))-1)) 
						  ORDER BY RIGHT(Mycolumn,(CHARINDEX('-',REVERSE(Mycolumn))-1))DESC)AS Dupplicate
FROM MyTable)

SELECT (LEFTSTR + RIGHTSTR)AS MyColumn  FROM CTE
WHERE Dupplicate = 1;


And Here is the result:

MyColumn
S00118-0110-B-NO-R-JK-46
S00118-JR10-B-NO-R-JS-26
S00201-0104-M-NO-R-SH-11
S00201-0110-B-NO-R-ST-16
S00201-JRSR-B-RE-R-TS-20
 
Share this answer
 
First of all: wrong approach! Why?

You have to work on data, not on string representation of data!



Assuming that each part of string (between [-]) represents some portion of data, you have to define class which can store that piece of data. For training purposes only i decided to write simple HelperClass with 2 properties: Header and Number. Header property is used to store string till the last [-] and Number property to store number.

C#
public class HelperClass
{
	private string header = string.Empty;
	private int nmb = 0;
	
	public string Header
	{
		get {return header;}
		set {header = value;}
	}
	
	public int Number
	{
		get {return nmb;}
		set {nmb = value;}
	}
	
	public override string ToString()
	{
		return header;
	}

}


How to use it?
C#
void Main()
{
	List<string> numbers = new List<string>()
							{
							"S00118-JR10-B-NO-R-JS-26",
							"S00118-JR10-B-NO-R-JS-22",
							"S00118-JR10-B-NO-R-JS-24",
							"S00201-0110-B-NO-R-ST-16",
							"S00201-0110-B-NO-R-ST-15",
							"S00201-JRSR-B-RE-R-TS-20",
							"S00118-0110-B-NO-R-JK-46",
							"S00201-0104-M-NO-R-SH-11",
							"S00118-0110-B-NO-R-JK-34"
							};
							
	var highestNumbers = numbers.Select(x=>new HelperClass
				{
					Header = x.Substring(0,22),
					Number = Convert.ToInt32(x.Substring(22,2))
				})
				.GroupBy(x=>x.Header)
				.Select(grp=> grp.Max(y=>y.Header) + grp.Max(y=>y.Number));
	foreach(var hn in highestNumbers)
	{
		Console.WriteLine("{0}", hn);
	}

}
</string></string>


Result:
S00118-JR10-B-NO-R-JS-26
S00201-0110-B-NO-R-ST-16
S00201-JRSR-B-RE-R-TS-20
S00118-0110-B-NO-R-JK-46
S00201-0104-M-NO-R-SH-11
 
Share this answer
 

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