我有一个Sales表,其中包含以下列:
现在我想要SUM最后15行,所以我现在正在做:
SELECT TOP 15 SUM(amount) FROM Sales ORDER BY [Date] DESC
Run Code Online (Sandbox Code Playgroud)
但是我明显得到了15行,有没有办法可以总结它而不必在客户端循环并将其SUM化?
我有下表:
memberid
2
2
3
4
3
Run Code Online (Sandbox Code Playgroud)
......我希望得到以下结果:
memberid count
2 2
3 1 ---Edit by gbn: do you mean 2?
4 1
Run Code Online (Sandbox Code Playgroud)
我试图使用:
SELECT MemberID,
COUNT(MemberID)
FROM YourTable
GROUP BY MemberID
Run Code Online (Sandbox Code Playgroud)
...但现在我想找到哪个记录有最大数量.IE:
memberid count
2 2
Run Code Online (Sandbox Code Playgroud) 我有一张桌子,里面有ids和辞职日期.一个单身人士有一个以上的辞职日期.
如何显示其中一个id只有一个最新日期即最大日期的表格.
有人可以帮助我......比一吨
假设有一个包含字段的表
Products
--------
ID
CategoryID
Name
Price
... etc
Run Code Online (Sandbox Code Playgroud)
Ruby on Rails如何提供返回的表
select count(*) from products group by categoryID
Run Code Online (Sandbox Code Playgroud)
这是为了显示每个类别中有多少产品?结果如何,而不是Products.find(:all)Product对象的数组?
作为一个更先进的操作,怎么样
select count(*) from products p inner join category c on p.categoryID = c.ID
group by categoryID
Run Code Online (Sandbox Code Playgroud)
和
select average(price) from products p inner join category c on p.categoryID = c.ID
group by categoryID
Run Code Online (Sandbox Code Playgroud)
?
我需要从3个不同的表,1个父表,2个子表创建一个摘要.
如何根据用户ID(3个表中每个表中的pk)从两个子表中获取记录数.
父表(user)pk是userId子表1和2具有userId和webId的复合pks.
我知道这不是正确的SQL语法,但它说明了我所追求的内容.
select u.userId, count(table1.webId), count(table2.webId)
from `user` u
left join `table1` t1 on u.userId = t1.userId
left join `table2` t2 on u.userId = t2.userId
group by u.userId
Run Code Online (Sandbox Code Playgroud) 我有以下表格,其中包含SQL Server 2008 R2中的客户和购买数据:
顾客
CustId Last First Phone
1 Doe John 555-5555
2 Smith Sally 444-4444
3 Smith Greg 222-1212
Run Code Online (Sandbox Code Playgroud)
Order_Header
OrderId CustId Date
1001 3 07/08/2011
1002 2 07/19/2011
1003 2 03/12/2012
1004 1 03/14/2012
1005 3 03/20/2012
1006 1 04/17/2012
1007 2 06/04/2012
1008 1 08/04/2011
Run Code Online (Sandbox Code Playgroud)
ORDER_LINES
OrderId Sequence Item Type Manufacturer Price
1001 1 WIDGET C WidgCo 12.00
1001 2 SWITCH C SwitchCo 10.00
1002 1 RADIO A RadSupply 30.00
1002 2 CRT A CRT&More 31.00 …Run Code Online (Sandbox Code Playgroud) 我有一个包含三个变量(ACC和类型和ID)的数据框,其中ACC指的是决策的准确性,类型是指30种不同的决策类型,对于参与者的每种决策类型重复15次,ID指的是对参与者.它看起来像这样:
ID ACC Type
1 1 1
1 0 3
1 1 10
etc...
2 1 5
2 0 13
2 0 11
etc...
Run Code Online (Sandbox Code Playgroud)
我的目标是分析参与者中每种决策类型的准确性,并将数据合并到数据框中.如:
ID ACC_Type1 ACC_Type2 […] ACC_Type30
1 70 65 87
2 65 50 90
etc...
Run Code Online (Sandbox Code Playgroud)
到目前为止,我能够通过单独分组决策类型来计算,但是,我正在寻找一种更智能的方法来避免单独键入决策类型值:
library(data.table)
library(plyr)
dt <- data.table(d,key="Type")
dt_Type1<-data.frame (aggregate(ACC~ID,data=subset(dt,Type==1),mean))
dt_Type2<-data.frame (aggregate(ACC~ID,data=subset(dt,Type==2),mean))
[]
dt_Type30<-data.frame (aggregate(ACC~ID,data=subset(dt,Type==30),mean))
total <- merge(dt_Type1,dt_Type2 […] Type30,by="ID")
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!
这是虚拟数据
temp.df <- data.frame(count = rep(1,6), x = c(1,1,NA,NA,3,10), y=c("A","A","A","A","B","B"))
Run Code Online (Sandbox Code Playgroud)
当我按如下所示应用汇总时:
aggregate(count ~ x + y, data=temp.df, FUN=sum, na.rm=FALSE, na.action=na.pass)
Run Code Online (Sandbox Code Playgroud)
我得到:
x y count
1 1 A 2
2 3 B 1
3 10 B 1
Run Code Online (Sandbox Code Playgroud)
但是,我想要以下输出:
x y count
1 NA A 2
2 1 A 2
3 3 B 1
4 10 B 1
Run Code Online (Sandbox Code Playgroud)
希望有道理,谢谢。
Hello Stackoverflow用户,
我对R中的聚合函数的结果有一个问题.我的目的是从数据集中选择某些鸟类,并计算被调查区域内观察到的个体的密度.为此,我获取了主数据文件的一个子集,然后在区域上聚合,计算平均值和个体数量(由向量长度表示).然后我想用计算出的平均面积和个体数来计算密度.那没用.我使用的代码如下:
> head(data)
positionmonth positionyear quadrant Species Code sum_areainkm2
1 5 2014 1 Bar-tailed Godwit 5340 155.6562
2 5 2014 1 Bar-tailed Godwit 5340 155.6562
3 5 2014 1 Bar-tailed Godwit 5340 155.6562
4 5 2014 1 Bar-tailed Godwit 5340 155.6562
5 5 2014 1 Gannet 710 155.6562
6 5 2014 1 Bar-tailed Godwit 5340 155.6562
sub.gannet<-subset(data, species == "Gannet")
sub.gannet<-data.frame(sub.gannet)
x<-sub.gannet
aggr.gannet<-aggregate(sub.gannet$sum_areainkm2, by=list(sub.gannet$positionyear, sub.gannet$positionmonth, sub.gannet$quadrant, sub.gannet$Species, sub.gannet$Code), FUN=function(x) c(observed_area=mean(x), NoInd=length(x)))
names(aggr.gannet)<-c("positionyear", "positionmonth", "quadrant", "species", "code", "x")
aggr.gannet<-data.frame(aggr.gannet) …Run Code Online (Sandbox Code Playgroud) 我的数据看起来像这样,有大约3300行数据:
Year Location Catch
1991 0313 45100
1989 0711 323
1991 0312 1100
1991 0313 45100
1989 0711 323
1991 0312 400
1990 0313 101000
1981 0711 623
1999 0312 410
2000 0313 145100
1987 0711 323
1987 1285 770
....
Run Code Online (Sandbox Code Playgroud)
年份涵盖1977 - 2015年期间,大约有500个不同的地点,并非每年都有数据.
我需要这样的输出,总结每个单元格的捕获量,按位置(行)和年份(列)列表:
Location '1977' '1978' '1979' '1980' '1981' '1982' '1983' ...
0312 456 11100 12560 320 4566 0 12010 ...
0313 121 100 4500 760 112 12050 100100 ...
0711 5500 6500 0 1205 1201 560 90500 …Run Code Online (Sandbox Code Playgroud)