我正在尝试创建一个将在Access 2.0中运行的查询。
主表有两个标题列和两个信用列。一个人将出现在标题A或标题B中,并将获得A或B的学分。只有标题A获得学分A和标题B学分B
**Title A Title B Credit A Credit B**
James Sam 1 0
Jason James 1 1
Justin Sara 1 1
Sam James 1 0
Justin Jason 1 1
Sara James 1 0
Jason Sam 0 1
我喜欢Access 2.0中的结果集,其中包含A和B总人数的人名
**Person Credits**
Jason 2
James 2
Sam 2
Sara 2
Justin 2
在访问中执行此操作的最佳方法是什么?为A和B创建两个单独的查询并将它们组合在一起并组合?
答案 0 :(得分:1)
您可以尝试此查询。
使用SUM titleA
和titleB
进行两次查询,然后使用UNION Combine两个物种。你可以按人格获得他们的总计积分
SELECT Person,sum(totle) Credits
FROM
(
SELECT TitleA Person,SUM(CreditA) totle
FROM T
GROUP BY TitleA
UNION ALL
SELECT TitleB Person,SUM(CreditB) totle
FROM T
GROUP BY TitleB
) t
GROUP BY Person
sqlfiddle:http://sqlfiddle.com/#!18/331f2/1
答案 1 :(得分:1)
是的,这是正确的!您使用UNION ALL组合所有人员和信用列,然后执行GROUP BY。获得总和信用列的总和。
SELECT person, sum(credit) as credits
FROM
(
SELECT TitleA as person,CreditA as credit
FROM T
UNION ALL
SELECT TitleB,CreditB
FROM T
) t
GROUP BY Person
答案 2 :(得分:0)
在FROM子句中获取错误'语法错误'在Access 2.0中
SELECT FlightData.Person, FlightData.TailNumber, FlightData.Aircraft, Sum(FlightData.Landings) AS TotalLandings
FROM (
SELECT [Flight Data].Date, [Flight Data].Pilot AS Person, [Flight Data].[Aircraft Number] AS TailNumber, Aircraft.[Aircraft Type] AS Aircraft, [Flight Data].[Pilot Landings] AS Landings FROM [Flight Data] INNER JOIN Aircraft ON [Flight Data].[Aircraft Number] = Aircraft.[Tail Number]
UNION ALL
SELECT [Flight Data].Date, [Flight Data].CoPilot as Person,[Flight Data].[Aircraft Number] AS TailNumber, Aircraft.[Aircraft Type] as Aircraft, [Flight Data].[CoPilot Landings]AS Landings FROM [Flight Data] INNER JOIN Aircraft ON [Flight Data].[Aircraft Number] = Aircraft.[Tail Number]
) AS FlightData
WHERE FlightData.[date] Between [Start Date] And [End Date]
GROUP BY FlightData.Person, FlightData.TailNumber, FlightData.Aircraft;