答案 0 :(得分:0)
select count(distinct case when right(transactionid, 2) like '-[0-9]'
then left(transactionid, len(transactionid) - 2) --cut out the version (-1,-2,-3,etc.)
else transactionid end
) [NumberOfUniqueTransactionId]
from MY_TABLE
答案 1 :(得分:0)
你必须使用不同的
select distinct left(TransactionId,LEN(TransactionId)-CHARINDEX('-',TransactionId)) from yourTable
答案 2 :(得分:0)
假设您使用Linq To SQL或实体框架来查询您的SQL(假设您提到了使用C#lambda表达式)
假设您的poco实体看起来像:
public class Entity
{
public string TransactionId { get; set; }
public string ServiceId { get; set; }
public string SourceApplicationName { get; set; }
}
您可以使用以下查询:
List<Entity> entities = null; /* you might want to use it from DB context */
var result = entities.DistinctBy(e => new { e.TransactionId, e.ServiceId, e.SourceApplicationName });
由于DistinctBy不可用作扩展方法。您可以使用以下一种通用扩展方法:
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> knownKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (knownKeys.Add(keySelector(element)))
{
yield return element;
}
}
}