我有下表
以下是我想要实现的结果
解释
我想按位置对数据进行分组,并获取列中可用的第一个非空值。例如,对于 locationcd 1300,列 A 将具有值John,列 B 将具有值John,列 C 将具有值Taylor和列 D 将具有值Brian。
我的解决方案
我做了一些不保证正确结果的事情。这是我的查询
select locationCd,
min([$1-$500]),
min([$501-$2500]) as '$501-$2500',
Min([$2501-$5000]) as '$2501-$5000',
min([$$5000 and above]) as '$$5000 and above'
from #tab
group by LocationCd
请帮忙。
答案 0 :(得分:0)
Select LocationId, MAX(Ty1.A) A
From #tabl T01
Outer Apply (Select Top 1 A From #tabl T1 Where T1.LocationId=T01.LocationId And T1.A Is Not Null) Ty1
Group By LocationId
这对你有帮助???
答案 1 :(得分:0)
// Singelton class
public class MyComparator implements Comparator<String> {
private static MyComparator myComparator = null;
private MyComparator() {}
@Override
public int compare(String a, String b) {
return compareString(a,b);
}
public static int compareString(String a, String b) {
if (a.length() > b.length())
return -1;
if (a.length() == b.length()) {
if (a.compareTo(b) > 0)
return -1;
else if (a.compareTo(b) == 0)
return 0;
}
return 1;
}
public static synchronized MyComparator getInstance() {
if(myComparator==null) {
myComparator = new MyComparator();
}
return myComparator;
}
}
答案 2 :(得分:0)
试试这个。希望这有助于你...
select LocationCD,min(A),min(B),min(C),min(D)
from #tab
group by LocationCD
答案 3 :(得分:0)
以下查询将提供所需的结果: -
select ROW_NUMBER() OVER ( partition by locationCd order by locationCd) RID,locationCd,A,B,C,D into #t1 from table_name
select locationcd,min(A) A,
(select B from #t1 b where b.locationcd=a.locationcd AND b.RID=3) B,
(select C from #t1 c where c.locationcd=a.locationcd AND c.RID=2) C,
(select D from #t1 d where d.locationcd=a.locationcd AND d.RID=1) D
from #t1 a
group by locationcd
drop table #t1
输出: -
locationcd A B C D
200 David David Detlef Brian
201 David David Detlef Brian
400 Walter Brad Detlef Brian
1200 David David Detlef Brian
1300 John John Taylor Brian
1400 Walter Brad Detlef Brian
将table_name替换为必需的表