Kotlin暴露条件下的总和(Caseweelse)
我有一个数据结构如下的表:Kotlin暴露条件下的总和(Caseweelse),kotlin,kotlin-exposed,Kotlin,Kotlin Exposed,我有一个数据结构如下的表: id type cityName regDate 1249 0 City1 2019-10-01 我想得到独特城市的结果输出,以及每个月的注册数量,作为数据类对象的列表 data class NewClientsNumberCityMonth( val cityName: String = "", val januaryNewClientsNumber :Int = 0, val februaryNewCl
id type cityName regDate
1249 0 City1 2019-10-01
我想得到独特城市的结果输出,以及每个月的注册数量,作为数据类对象的列表
data class NewClientsNumberCityMonth(
val cityName: String = "",
val januaryNewClientsNumber :Int = 0,
val februaryNewClientsNumber :Int = 0,
val marchNewClientsNumber :Int = 0,
val aprilNewClientsNumber :Int = 0,
val mayNewClientsNumber :Int = 0,
val juneNewClientsNumber :Int = 0,
val julyNewClientsNumber :Int = 0,
val augustNewClientsNumber :Int = 0,
val septemberNewClientsNumber :Int = 0,
val octoberNewClientsNumber :Int = 0,
val novemberNewClientsNumber :Int = 0,
val decemberNewClientsNumber :Int = 0
val total :Int = 0
)
并使用这些对象作为字符串填充表格(我们不知道唯一城市的数量),
结果应该是:
City1 5 8 3 1 2 1 4 1 2 1 0 0
City2 69 23 7 5 3 10 24 14 12 23 25 10
...
我在试这个
val tempMutList = mutableListOf<NewClientsNumberCityMonthModel>()
transaction(Connection.TRANSACTION_SERIALIZABLE, 2) {
addLogger(StdOutSqlLogger)
ClientsDataExposed
.slice(ClientsDataExposed.cityName)
.selectAll()
.groupBy(ClientsDataExposed.cityName)
.map { it[ClientsDataExposed.cityName] }
.toList().forEach {
val regsInCity = ClientsDataExposed
.slice(
ClientsDataExposed.id,
ClientsDataExposed.cityName,
ClientsDataExposed.type,
ClientsDataExposed.regDate,
ClientsDataExposed.regDate.month()
)
.selectAll()
.andWhere { ClientsDataExposed.cityName.eq(it) }
.andWhere {
ClientsDataExposed.regDate.between(
Date.valueOf("2019-01-01".toString()),
Date.valueOf("2020-01-01".toString())
)
}
.andWhere {
(ClientsDataExposed.type.inList(contracterTypeSelectedCheckboxes.filterValues { it }.keys.toList())) and (ClientsDataExposed.cityName.inList(
citiesSelectedChekboxes.filterValues { it }.keys.toList()
))
}
.map { it[ClientsDataExposed.regDate.month()] }
.toList()
val cityClientsPerMonth = NewClientsNumberCityMonthModel(
it,
regsInCity.count { it == 1 },
regsInCity.count { it == 2 },
regsInCity.count { it == 3 },
regsInCity.count { it == 4 },
regsInCity.count { it == 5 },
regsInCity.count { it == 6 },
regsInCity.count { it == 7 },
regsInCity.count { it == 8 },
regsInCity.count { it == 9 },
regsInCity.count { it == 10 },
regsInCity.count { it == 11 },
regsInCity.count { it == 12 },
regsInCity.count()
)
tempMutList.add(cityClientsPerMonth)
//obj of dataclass
}
viewTableTabOfnewClientsNumberCityMonth.value = tempMutList.map { it }
}
val tempMutList=mutableListOf()
事务(Connection.transaction_SERIALIZABLE,2){
addLogger(StdOutSqlLogger)
客户数据暴露
.slice(ClientsDataExposed.cityName)
.selectAll()
.groupBy(ClientsDataExposed.cityName)
.map{it[ClientsDataExposed.cityName]}
.toList().forEach{
val regsInCity=ClientsDataExposed
.切片(
ClientsDataExposed.id,
ClientsDataExposed.cityName,
ClientsDataExposed.type,
ClientsDataExposed.regDate,
ClientsDataExposed.regDate.month()
)
.selectAll()
.andWhere{ClientsDataExposed.cityName.eq(it)}
.在哪里{
ClientsDataExposed.regDate.between(
日期。价值(“2019-01-01”。toString()),
日期.valueOf(“2020-01-01”.toString())
)
}
.在哪里{
(ClientsDataExposed.type.inList(ContracterTypeSelectedCheckBox.filterValues{it}.keys.toList())和(ClientsDataExposed.cityName.inList(
citiesSelectedChekboxes.filterValues{it}.keys.toList()
))
}
.map{it[ClientsDataExposed.regDate.month()]}
托利斯先生()
val cityClientsPerMonth=NewClientsNumberCityMonthModel(
信息技术
regsInCity.count{it==1},
regsInCity.count{it==2},
regsInCity.count{it==3},
regsInCity.count{it==4},
regsInCity.count{it==5},
regsInCity.count{it==6},
regsInCity.count{it==7},
regsInCity.count{it==8},
regsInCity.count{it==9},
regsInCity.count{it==10},
regsInCity.count{it==11},
regsInCity.count{it==12},
regsInCity.count()
)
tempMutList.add(cityClientsPerMonth)
//数据类对象
}
viewTableTabOfnewClientsNumberCityMonth.value=tempMutList.map{it}
}
我知道,我应该使用Sum()和caseweelse,比如,带有check-on
ClientsDataExposed.regDate.month()
它给出了一个月数(1-12),用于将sum()结果分配给我的数据类属性,但我找不到任何CaseWhenElse语法的示例,我自己也无法理解,
或者有没有其他方法不使用caseweelse?请检查代码:
val monthExpr = ClientsDataExposed.regDate.month()
fun sumByMonth(monthNum: Int) = Expression.build {
val caseExpr = case().
When(monthExpr eq intLiteral(monthNum), intLiteral(1)).
Else(intLiteral(0))
Sum(caseExpr, IntegerColumnType())
}
val janSumExp = sumByMonth(1)
val febSumExp = sumByMonth(2)
ClientsDataExposed.
slice(ClientsDataExposed.cityName, janSumExp, febSumExp).
selectAll().
groupBy(ClientsDataExposed.id).map {
val janSum = it[janSumExp]
val febSum = it[febSumExp]
...
}
是的,它有效,谢谢,唯一需要注意的是它应该是
。。。groupBy(ClientsDataExposed.cityName).
我还使用相同的模板来计算每月的付款(在将基本示例表与付款表合并之后),只需对我的付款属性字段名称进行一点小小的更改intLiteral
-thing