你表结构就有问题要把列传成行才行自己重新映射对象关系吧select num1+','+field1 as name1,num2+','+field2 as name2,num3+','+field3 as name3 from 表class A { public string name{get;set;} void CutNum() { this.name=name.SubString(2,name.Length);//截取字符串 } }List<A> list=new List<A>();var finallist= from li in list order by li.name select li; list=finallist.ToList(); foreach(A single in list) { single.CutNum(); Console.WriteLine(single.name); }list里边的对象就是排序好了的,按照升序,然后再去掉前面的数字和逗号,当然你也可以不加逗号!随便你怎么搞吧,就这么个意思这个方法比较好理解!明天睡醒了我在鼓捣个先进的在数据库区出来的时候把每一个字段都给类A里的NAME属性赋值,然后仍进LIST里边就可以了
select case when num1 >= num2 and num1 >= num3 then field1 when num2 >= num1 and num2 >= num3 then field2 when num3 >= num1 and num3 >= num2 then field3 end c1 , case when num1 >= num2 and num1 <= num3 then field1 when num2 >= num1 and num2 <= num3 then field2 when num3 >= num1 and num3 <= num2 then field3 end c2 , case when num1 <= num2 and num1 <= num3 then field1 when num2 <= num1 and num2 <= num3 then field2 when num3 <= num1 and num3 <= num2 then field3 end c3 from tb
CREATE TABLE #temp ( field1 VARCHAR(10), field2 VARCHAR(10), field3 VARCHAR(10), num1 INT, num2 INT, num3 INT ) INSERT #temp SELECT 'a', 'b', 'c', 1, 2, 3 UNION ALL SELECT 'd', 'e', 'f', 6, 4, 5 GO --SQL: ;WITH cte AS (SELECT rowno = ROW_NUMBER() OVER(ORDER BY GETDATE()), * FROM #temp), TB AS (SELECT rowno, field1, num1 FROM cte UNION ALL SELECT rowno, field2, num2 FROM cte UNION ALL SELECT rowno, field3, num3 FROM cte) SELECT * FROM (SELECT rowno FROM cte) a CROSS APPLY (SELECT aa = STUFF((SELECT ','+field1 FROM TB WHERE rowno = a.rowno ORDER BY num1 FOR XML PATH('')), 1, 1, '')) b /* 1 a,b,c 2 e,f,d */
CREATE TABLE #temp ( field1 VARCHAR(10), field2 VARCHAR(10), field3 VARCHAR(10), num1 INT, num2 INT, num3 INT ) INSERT #temp SELECT 'a', 'b', 'c', 1, 2, 3 UNION ALL SELECT 'd', 'e', 'f', 6, 4, 5 GO --SQL: SELECT * FROM #temp select case when num1 >= num2 and num1 >= num3 then field1 --找最大的,没问题 when num2 >= num1 and num2 >= num3 then field2 when num3 >= num1 and num3 >= num2 then field3 end c1 , case when num1 >= num2 and num1 <= num3 then field1 --找中间的,缺少条件 when num1 >= num3 and num1 <= num2 then field1 when num2 >= num1 and num2 <= num3 then field2 when num2 >= num3 and num2 <= num1 then field2 when num3 >= num1 and num3 <= num2 then field3 when num3 >= num2 and num3 <= num1 then field3 end c2 , case when num1 <= num2 and num1 <= num3 then field1 --找最小的,没问题 when num2 <= num1 and num2 <= num3 then field2 when num3 <= num1 and num3 <= num2 then field3 end c3 from #temp /* c b a d f e */
{
public string name{get;set;} void CutNum()
{
this.name=name.SubString(2,name.Length);//截取字符串
}
}List<A> list=new List<A>();var finallist= from li in list order by li.name
select li;
list=finallist.ToList();
foreach(A single in list)
{
single.CutNum();
Console.WriteLine(single.name);
}list里边的对象就是排序好了的,按照升序,然后再去掉前面的数字和逗号,当然你也可以不加逗号!随便你怎么搞吧,就这么个意思这个方法比较好理解!明天睡醒了我在鼓捣个先进的在数据库区出来的时候把每一个字段都给类A里的NAME属性赋值,然后仍进LIST里边就可以了
when num2 >= num1 and num2 >= num3 then field2
when num3 >= num1 and num3 >= num2 then field3
end c1 ,
case when num1 >= num2 and num1 <= num3 then field1
when num2 >= num1 and num2 <= num3 then field2
when num3 >= num1 and num3 <= num2 then field3
end c2 ,
case when num1 <= num2 and num1 <= num3 then field1
when num2 <= num1 and num2 <= num3 then field2
when num3 <= num1 and num3 <= num2 then field3
end c3
from tb
(
field1 VARCHAR(10),
field2 VARCHAR(10),
field3 VARCHAR(10),
num1 INT,
num2 INT,
num3 INT
)
INSERT #temp
SELECT 'a', 'b', 'c', 1, 2, 3 UNION ALL
SELECT 'd', 'e', 'f', 6, 4, 5
GO
--SQL:
;WITH cte AS
(SELECT rowno = ROW_NUMBER() OVER(ORDER BY GETDATE()), * FROM #temp),
TB AS
(SELECT rowno, field1, num1 FROM cte UNION ALL
SELECT rowno, field2, num2 FROM cte UNION ALL
SELECT rowno, field3, num3 FROM cte)
SELECT * FROM
(SELECT rowno FROM cte) a
CROSS APPLY
(SELECT aa = STUFF((SELECT ','+field1 FROM TB WHERE rowno = a.rowno ORDER BY num1 FOR XML PATH('')), 1, 1, '')) b
/*
1 a,b,c
2 e,f,d
*/
(
field1 VARCHAR(10),
field2 VARCHAR(10),
field3 VARCHAR(10),
num1 INT,
num2 INT,
num3 INT
)
INSERT #temp
SELECT 'a', 'b', 'c', 1, 2, 3 UNION ALL
SELECT 'd', 'e', 'f', 6, 4, 5
GO
--SQL:
SELECT * FROM #temp
select case when num1 >= num2 and num1 >= num3 then field1 --找最大的,没问题
when num2 >= num1 and num2 >= num3 then field2
when num3 >= num1 and num3 >= num2 then field3
end c1 ,
case when num1 >= num2 and num1 <= num3 then field1 --找中间的,缺少条件
when num1 >= num3 and num1 <= num2 then field1
when num2 >= num1 and num2 <= num3 then field2
when num2 >= num3 and num2 <= num1 then field2
when num3 >= num1 and num3 <= num2 then field3
when num3 >= num2 and num3 <= num1 then field3
end c2 ,
case when num1 <= num2 and num1 <= num3 then field1 --找最小的,没问题
when num2 <= num1 and num2 <= num3 then field2
when num3 <= num1 and num3 <= num2 then field3
end c3
from #temp
/*
c b a
d f e
*/
这SQL给力。楼主如果这张表数据不多,字段不多,查询逻辑不复杂,不需要表连接神马的没拓展需求的话就用楼上这几位老大的SQL吧实现比较简单!数据多,业务复杂的话就用C#代码实现