groupBy

Posted sunshineicy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了groupBy相关的知识,希望对你有一定的参考价值。

public List groupBy(List list,String flag,String... sortName) throws Exception{
Map<String,List<Object>> tMap = new HashMap<String,List<Object>>();
for(Object t : list){
String filedKey ="";
for(String filedName : sortName){
Field field = t.getClass().getDeclaredField(filedName);
filedKey = field.get(t)+","+filedKey;
System.out.println("filedName======"+filedKey);
}

if(tMap.containsKey(filedKey)){
tMap.get(filedKey).add(t);
}else{
List tList1 = new ArrayList();
tList1.add(t);
tMap.put(filedKey,tList1);
}
}

Field fields[]=list.get(0).getClass().getDeclaredFields();//获得对象所有属性
Field field=null;
for (int i = 0; i < fields.length; i++) {
field=fields[i];
field.setAccessible(true);
}
List<CoastClaimResult> claimList = new ArrayList<CoastClaimResult>();
for(Map.Entry<String,List<Object>> entry : tMap.entrySet()){
BigDecimal billedAmt=new BigDecimal(0);
BigDecimal paidAmt = new BigDecimal(0);
List<String> distinctCountList = new ArrayList<String>();
CoastClaimResult result = new CoastClaimResult();
System.out.println("Key = " + entry.getKey());
for(Object t : entry.getValue()){
System.out.println(t.toString());

String bill = (String) t.getClass().getDeclaredField("amount"+flag).get(t);
billedAmt.add(new BigDecimal(bill));

String claimNo = (String) t.getClass().getDeclaredField("claimNo").get(t);
if(!distinctCountList.contains(claimNo)){
distinctCountList.add(claimNo);
}

/*String paid = (String) t.getClass().getDeclaredField("amountpaid").get(t);
paidAmt.add(new BigDecimal(paid));*/
}
for(String filedName : sortName){
Field f = CoastClaimResult.class.getClass().getDeclaredField("filedName");
f.setAccessible(true);
f.set(CoastClaimResult.class, (String) list.get(0).getClass().getDeclaredField("filedName").get(list.get(0)));
}
result.setExt1(new BigDecimal(distinctCountList.size()));
if(flag.equals("billed")){
result.setBilledAmt(billedAmt);
result.setTitleCategory("BILLED");
}else{
result.setPaidAmt(billedAmt);
result.setTitleCategory("PAID");
}

// result.setPaidAmt(paidAmt);
claimList.add(result);


// System.out.println(polNo.toString());
}

return claimList;
}

 

 

DECLARE CATEGORY_CURSOR CURSOR FOR
SELECT DISTINCT
DEPCODE,
DEPNAME
FROM ( SELECT DEPNAME, MAX(DEPCODE) DEPCODE FROM #CLAIMDATA (nolock) GROUP BY DEPNAME) M
ORDER BY DEPCODE,
DEPNAME

OPEN CATEGORY_CURSOR
FETCH NEXT FROM CATEGORY_CURSOR INTO @LS_CATEGORY,@LS_CATEGORY

WHILE @@FETCH_STATUS=0
BEGIN
SELECT @[email protected][email protected]_CATEGORY+‘,‘

FETCH NEXT FROM CATEGORY_CURSOR INTO @LS_CATEGORY,@LS_CATEGORY
END

以上是关于groupBy的主要内容,如果未能解决你的问题,请参考以下文章

六分组数据

sql 单表/多表查询去除重复记录

sql语句去重

GROUP BY语句与HAVING语句的使用

SQL中GROUP BY语句与HAVING语句的使用

sql 单表distinct/多表group by查询去除重复记录