[英]Combine data with same ID
I have an assignment where I need to use aggregate functions in the queries.我有一个作业需要在查询中使用聚合函数。 I keep running into a problem where there are multiply entries for the same ID, and I would rather them be combined into one run (added together for the same ID).
我一直遇到同一个 ID 有多个条目的问题,我宁愿将它们组合成一个运行(为同一个 ID 添加在一起)。
-- Aggregate #3 - Show the total amount of pledges for each Team
-- for particular year. Order the results by amount of pledges highest
-- to lowest. This query should result in 1 row per Team.
-- It should include the Team ID, Team name (or a combination of Sport,
-- Level, and Gender), the total pledge amount and the Event date/year.
SELECT DISTINCT SUM(TEGS.monPledgeAmount) AS TotalPledge
,TE.intEventID
,TTC.intTeamandClubID
,TE.dtmEventDate
,TGS.strGenderDesc
,TLT.strLevelDesc
FROM TEventGolfers AS TEG JOIN TEvents TE
ON TEG.intEventID = TE.intEventID
JOIN TGolfers AS TG
ON TG.intGenderID = TEG.intGolferID
JOIN TEventGolferSponsors AS TEGS
ON TEGS.intEventGolferID = TEG.intEventGolferID
JOIN TEventGolferTeamandClubs AS TEGTC
ON TEGTC.intEventGolferID = TEG.intEventGolferID
JOIN TTeamandClubs AS TTC
ON TTC.intTeamandClubID = TEGTC.intTeamandClubID
JOIN TLevelofTeams AS TLT
ON TLT.intLevelofTeamID = TTC.intLevelofTeamID
JOIN TGenders AS TGS
ON TGS.intGenderID = TTC.intGenderID
GROUP BY
TEGS.monPledgeAmount
,TE.intEventID
,TTC.intTeamandClubID
,TE.dtmEventDate
,TGS.strGenderDesc
,TLT.strLevelDesc
Output below (IDs to be combined in column 3, "intTeamandClubID"):下面的 Output(要在第 3 列“intTeamandClubID”中合并的 ID):
0.80 2 3 2016-01-01 Female Varsity Football
0.80 2 4 2016-01-01 Male Varsity Golf
4.00 2 3 2016-01-01 Female Varsity Football
4.00 2 4 2016-01-01 Male Varsity Golf
10.00 2 3 2016-01-01 Female Varsity Football
50.00 2 3 2016-01-01 Female Varsity Football
I want the "intTeamandClubID" to be 1 row for the same ID, and the "TotalPledge" to be added together.我希望同一 ID 的“intTeamandClubID”为 1 行,并将“TotalPledge”加在一起。
You can remove the DISTINCT in the SELECT clause and the column TEGS.monPledgeAmount in the GROUP BY clause.您可以删除SELECT子句中的DISTINCT和GROUP BY子句中的列TEGS.monPledgeAmount 。
SELECT SUM(TEGS.monPledgeAmount) AS TotalPledge
,TE.intEventID
,TTC.intTeamandClubID
,TE.dtmEventDate
,TGS.strGenderDesc
,TLT.strLevelDesc
FROM TEventGolfers AS TEG JOIN TEvents TE
ON TEG.intEventID = TE.intEventID
JOIN TGolfers AS TG
ON TG.intGenderID = TEG.intGolferID
JOIN TEventGolferSponsors AS TEGS
ON TEGS.intEventGolferID = TEG.intEventGolferID
JOIN TEventGolferTeamandClubs AS TEGTC
ON TEGTC.intEventGolferID = TEG.intEventGolferID
JOIN TTeamandClubs AS TTC
ON TTC.intTeamandClubID = TEGTC.intTeamandClubID
JOIN TLevelofTeams AS TLT
ON TLT.intLevelofTeamID = TTC.intLevelofTeamID
JOIN TGenders AS TGS
ON TGS.intGenderID = TTC.intGenderID
GROUP BY
TE.intEventID
,TTC.intTeamandClubID
,TE.dtmEventDate
,TGS.strGenderDesc
,TLT.strLevelDesc
You want to sum the TotalPledge
values for each intTeamandClubID
.您想要对每个
intTeamandClubID
的TotalPledge
值求和。 For that you would need to create a different group for each intTeamandClubID
.为此,您需要为每个
intTeamandClubID
创建一个不同的组。 In your case, since you are selecting multiple columns, you will create a group for each unique combination of those columns, which is done by a GROUP BY statement.在您的情况下,由于您选择了多列,因此您将为这些列的每个唯一组合创建一个组,这是由GROUP BY语句完成的。
You already have that in your query, but you are also grouping by TotalPledge
, which you don't want.你已经在你的查询中有了它,但你也按
TotalPledge
分组,这是你不想要的。 You want to SUM that value, so you should remove it from the GROUP BY
:您想要对该值求和,因此您应该将其从
GROUP BY
中删除:
GROUP BY
TE.intEventID
,TTC.intTeamandClubID
,TE.dtmEventDate
,TGS.strGenderDesc
,TLT.strLevelDesc
Note that, even though it has some use-cases alongside GROUP BY, DISTINCT
is unnecessary here since each group (combination of columns values) is unique.请注意,尽管它在 GROUP BY 旁边有一些用例,但这里不需要
DISTINCT
,因为每个组(列值的组合)都是唯一的。 Leaving it in the query, however, won't impact the result但是,将其留在查询中不会影响结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.