我有两个模型,作者和文章:
class Author(models.Model):
name = models.CharField('name', max_length=100)
class Article(models.Model)
title = models.CharField('title', max_length=100)
pubdate = models.DateTimeField('publication date')
authors = models.ManyToManyField(Author)
Run Code Online (Sandbox Code Playgroud)
现在我想选择所有作者并用他们各自的文章计数注释它们.这与Django的聚合物是一块蛋糕.问题是,它应该只计算已发布的文章.根据Django票务跟踪器中的票11305,这还不可能.我试图使用该CountIf票证中提到的注释,但它没有引用日期时间字符串,也没有进行所需的所有连接.
那么,除了编写自定义SQL之外,最好的解决方案是什么?
我有一个Maven POM聚合了几个模块.
<project [stuff]>
<modelVersion>4.0.0</modelVersion>
<groupId>com.fuhu.osg</groupId>
<artifactId>UserManagement</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>UserManagement</name>
<modules>
<module>core</module>
<module>war</module>
<module>ejbs</module>
<module>ear</module>
</modules>
</project>
Run Code Online (Sandbox Code Playgroud)
我想执行一个不适用于顶级POM模块的目标.像mvn db-migrate:create之类的东西.就像这样,似乎这会尝试针对子项目运行所述命令,这对于每个其他目标都是正确的,但对于这个目标则不然.
有没有办法让Maven POM既是某些目标的聚合体又是其他目标的普通项目?
我们正在开发一个使用DDD的项目,但是他们仍然不知道如何对待查找实体.例如,我们有一个名为"Customer"的聚合,而实体"Customer"也是聚合根.实体"Customer"具有属性"CustomerTypeID".
但我们还有一个实体"CustomerType",代表所有现有客户类型(ID和描述).将有一个管理功能,允许用户维护客户类型(即添加新的客户类型等).
请注意,我不是在谈论为特定客户更改客户类型,而是在维护客户类型列表.
我为这个冗长的故事道歉,但这是我的问题:
我是否认为"CustomerType"是一个实体而不是一个价值对象?
应该"CustomerType"在聚合"客户"内部,还是应该在其自己的聚合内部,因为我们将访问它并将其维护在特定客户的上下文之外?
如果这个实体和任何其他基本查找实体(如客户状态,产品类型等)的实体应该自己聚合(并且是这些聚合的聚合根),那么我最终不会得到数百个存储库?(因为每个聚合根将拥有自己的存储库)
正如你所看到的,我在这里陷入困境,只需指向正确的方向.
===================================我试着写一些代码回复eulerfx的回复,但我无法'让它工作,所以我会把它放在这里.
关于第2点,在屏幕上我们显然只会显示类型的描述(例如"个人").这是否意味着我最终得到这样的东西?:
Public Class Customer
Inherits EntityBase(Of Integer)
Implements IAggregateRoot
Public Property CustomerID As Integer
...
Public Property CustomerType As CustomerType
...
Run Code Online (Sandbox Code Playgroud)
和
公共类CustomerType Inherits EntityBase(Of Integer)实现IAggregateRoot
Public Property CustomerTypeID As Integer
Public Property CustomerTypeDescription As String
Run Code Online (Sandbox Code Playgroud)
或者"Customer"类中的属性应该是CustomerTypeID吗?
关于第3点,聚合和有界背景之间有什么区别?"客户"聚合("客户"是否为聚合根),它还包含仅存在于客户上下文中的任何实体和价值对象,而不是有界上下文?
下面的列表是使用以下代码段在时间序列中与每个月对应的数据总和:
aggregate(data, by=list(Year=format(DateTime, "%Y"), Month=format(DateTime, "%m")), sum, na.rm=TRUE)
Year Month x
1 1981 01 62426.43
2 1982 01 70328.87
3 1983 01 67516.34
4 1984 01 64454.00
5 1985 01 78801.46
6 1986 01 73865.18
7 1987 01 64224.96
8 1988 01 72362.39
9 1981 02 74835.16
10 1982 02 75275.58
11 1983 02 67457.39
12 1984 02 64981.99
13 1985 02 56490.10
14 1986 02 62759.89
15 1987 02 65144.44
16 1988 02 67704.67
Run Code Online (Sandbox Code Playgroud)
这部分很容易......但是我试图获得每个月的所有月度总和的平均值(即每个月的平均值)如果我执行以下操作:
aggregate(data, by=list(Month=format(DateTime, "%m")), sum, …Run Code Online (Sandbox Code Playgroud) 我有一个数据框由行绑定许多数据帧,每个数据帧用唯一键标识.我希望计算大数据帧的每个子集(使用唯一密钥)中的列的相关系数.例如,使用mtcars数据我可能要计算列之间的相关性hp,并wt在列中的每个独特的价值cyl.我可以循环完成
data("mtcars")
for(i in c(4,6,8)){
temp = subset(mtcars,mtcars$cyl==i)
cor(temp$hp,temp$wt)
}
Run Code Online (Sandbox Code Playgroud)
我认为聚合会更好,但这段代码不起作用:
data("mtcars")
aggregate(mtcars,by=mycars$cyl,cor)
Run Code Online (Sandbox Code Playgroud) 我有一张桌子,我需要填充平均值.我目前正在使用低效的代码,这将花费很长时间在大型数据集上.例:
样本数据:
x = read.table(text="a b value mean
1 1 10 0
1 1 12 0
2 2 14 0
2 1 16 0", header=TRUE)
Run Code Online (Sandbox Code Playgroud)
码:
y <- aggregate(x$value, list(a = x$a,b = x$b), mean)
print(y)
# a b x
# 1 1 1 11
# 2 2 1 16
# 3 2 2 14
for (i in 1:4) {
for (j in 1:3) {
if (x$a[i]==y$a[j] && x$b[i]==y$b[j]) {
x$mean[i]=y$x[j] }
}
}
print(x) # This is the final …Run Code Online (Sandbox Code Playgroud) 我的数据集是这样的
Section Time x
s3 9:35 2
s4 9:35 2
s1 9:36 1
s2 10:01 1
s8 11:00 2
Run Code Online (Sandbox Code Playgroud)
因此,我希望按小时间隔对数据部分进行分组,并总结该间隔中的x值
我的预期产量是
sec Time x
s1 9:00-10:00 1
s2 9:00-10:00 0
s3 9:00-10:00 2
s4 9:00-10:00 2
s8 9:00-10:00 0
s1 10.00-11.00 0
s2 10.00-11.00 1
s3 10.00-11.00 0
s4 10.00-11.00 0
s8 10.00-11.00 1
Run Code Online (Sandbox Code Playgroud)
我试图从堆栈溢出的这篇文章中获得一些帮助,但是我的查询出现以下错误.这里x是我的框架
data.frame(value = tapply(cbind(x$x),
list(sec= x$section,cut(x$Time, breaks="1 hour")),
sum))
Error in cut.default(x$Time, breaks = "1 hour") : 'x' must be numeric
Run Code Online (Sandbox Code Playgroud)
我甚至不确定这是对还是错.我从未在R中使用过时间数据.所以我对如何实现这一点的任何帮助都将是一个很大的帮助.
我想编写自定义分组和聚合函数来获取用户指定的列名和用户指定的聚合映射.我不知道前面的列名和聚合映射.我想写一个类似下面的函数.但我是Scala的新手,我无法解决它.
def groupAndAggregate(df: DataFrame, aggregateFun: Map[String, String], cols: List[String] ): DataFrame ={
val grouped = df.groupBy(cols)
val aggregated = grouped.agg(aggregateFun)
aggregated.show()
}
Run Code Online (Sandbox Code Playgroud)
并希望称之为
val listOfStrings = List("A", "B", "C")
val result = groupAndAggregate(df, Map("D"-> "SUM", "E"-> "COUNT"), listOfStrings)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?任何人都可以帮助我.
我希望按名称聚合数据框的两列,具体方法如下:
parts的特殊聚集两列的结果列fruits和partsparts苹果,香蕉和草莓的价值无关紧要,一切都得到了总结,但parts葡萄和新西兰的价值应该成为新的fruits名字这听起来似乎很简单,但经过数小时的试验和错误,我没有找到任何有用的解决方案.这是一个例子:
theDF <- data.frame(dates = as.Date(c(today()+20)),
fruits = c("Apple","Apple","Apple","Apple","Banana","Banana","Banana","Banana",
"Strawberry","Strawberry","Strawberry","Strawberry","Grape", "Grape",
"Grape","Grape", "Kiwi","Kiwi","Kiwi","Kiwi"),
parts = c("Big Green Apple","Apple2","Blue Apple","XYZ Apple4",
"Yellow Banana1","Small Banana","Banana3","Banana4",
"Red Small Strawberry","Red StrawberryY","Big Strawberry",
"StrawberryZ","Green Grape", "Blue Grape", "Blue Grape",
"Blue Grape","Big Kiwi","Small Kiwi","Big Kiwi","Middle Kiwi"),
stock = as.vector(sample(1:20)) )
Run Code Online (Sandbox Code Playgroud)
当前数据框:
所需的输出:
我有一个包含大量数据的表,它有一个类别ID和postId,我需要阅读每个类别3个具有相同CatID的新帖子.
这与其他人提出的问题不重复.请检查在我的问题中postid catid可以是任何东西,当重复问题时,它是在运行查询之前计算的.
我写的是
SELECT
MAX(` postid `) AS p1,
` catid ` AS c1
FROM
` postcategory `
GROUP BY
` catid
Run Code Online (Sandbox Code Playgroud)
我可以将其他2个查询放入union union中,但它会使查询变得很大.在MySQL中有没有什么好方法可以做到这一点.我正在寻找的阅读3 postId(最大)属于同一类别.
postId catId
------ --------
9 3
15 3
16 3
17 3
18 3
19 5
20 8
21 6
22 8
23 6
46 6
46 8
26 3
25 3
27 5
28 3
37 6
39 10
40 6
41 6
42 6
43 6
44 5
45 11
63 6 …Run Code Online (Sandbox Code Playgroud) aggregate ×10
r ×5
group-by ×2
apache-spark ×1
correlation ×1
data.table ×1
dataframe ×1
django ×1
java ×1
maven ×1
mean ×1
mysql ×1
plugins ×1
pom.xml ×1
python ×1
scala ×1
statistics ×1
time-series ×1