如何在另一列的分组中对一列进行求和?

时间:2011-07-17 08:54:19

标签: sql ms-access

这是我的问题:

NAME    DATE           PRESENTS
bob     march 3 2011      1
bob     june 6 2008       2
bob     jan 3 2012        3
mary    feb 14 1986       4
mary    april 10 2001     5
mary    jan 3 2012        6
kate    march 3 2011      7
kate    jan 3 2012        8
kate    oct 9 2013        9
celia   march 3 2011     10
celia   feb 14 1986      11
celia   july 4 2011      12
celia   jan 3 2012       13
celia   feb 14 1991      14

因此,我们的目标是,如果他们在那些日子里收到任何礼物,我们会在同一天添加凯特和西莉亚的礼物数量。

我需要做的就是这样,除了更大的数据集。数据集的大小可能约为100,000个条目。

我需要SQL中的答案,或者作为Access 2003查询。

5 个答案:

答案 0 :(得分:1)

如果presents是礼物凯特或西莉亚或其他人在特定日期的礼物数量:

select name, date, sum(presents) total_on_date
from table
where name in ('kate','celia')
group by name, date

我绝不是一个sql天才

答案 1 :(得分:0)

select name, date, count(presents) total_on_date
from table
where name in ('kate','celia')
group by name, date

假设您要计算礼物ID

答案 2 :(得分:0)

你必须加入回归,就像这样:

select DATE, SUM(PRESENTS) as TOTAL_JOINT_PRESENTS
from mytable t1
join mytable t2 on t1.DATE = t2.DATE
where t1.NAME = 'kate'
and t2.NAME = 'celia';

答案 3 :(得分:0)

与波希米亚人给出的答案有关: -

我认为他想要计算这两个人来的日子......

select count(*)
from mytable t1 join mytable t2 
     on t1.DATE = t2.DATE 
where t1.NAME = 'kate' and t2.NAME = 'celia';

答案 4 :(得分:0)

如果你想要

  

礼物凯特和西莉亚在同一天得到的数量

然后:

SELECT
    presents1.date AS presents_date,
    (COUNT(*) + 1) AS presents_count
FROM
    presents presents1,
    presents presents2
WHERE presents1.date = presents2.date
AND presents1.name = 'kate'
AND presents2.name = 'celia'
GROUP BY presents1.date

假设表名为presents