简体   繁体   English

在Access中需要有关SQL查询的帮助

[英]Need Help with SQL Query in Access

I have been handed a database to run a few queries on. 我已经交给一个数据库来运行一些查询。

For one query I have to find the top 10 applications, from two different tables with hundreds of records. 对于一个查询,我必须从具有数百条记录的两个不同表中找到前10个应用程序。 then on row (11) I will need to SUM or Count the remaining records and name the row "Other". 然后在第(11)行中,我需要对剩余记录求和或计数,并将行命名为“其他”。

I have worked out the following code so far. 到目前为止,我已经制定了以下代码。

SELECT TOP 10 ApplicationTbl.AppName, Count(*) AS SessionNos
FROM ApplicationTbl INNER JOIN SessionTbl ON ApplicationTbl.AppID = SessionTbl.AppID
GROUP BY ApplicationTbl.AppName;
ORDER BY Count(*) DESC; 

I am displayed with 10 top records, but I know need to sum the remaining SessionNos together onto row 11 and rename the AppName to "Other" 显示了10条最重要的记录,但是我知道需要将剩余的SessionNos总计到第11行,并将AppName重命名为“ Other”

Can anyone please help, or recommend anything. 任何人都可以帮忙,或推荐任何东西。

FYI: I am using Access 2007 built in SQL View, and I know that there is limits to how much can be done. 仅供参考:我使用的是SQL View内置的Access 2007,我知道可以完成的操作是有限的。

I am not very good with SQL, its new to me. 我对SQL不太好,它对我来说是新的。

Thanks :) 谢谢 :)

What you need is 您需要的是
1) The Query above that Gets the top 10, call it Top10Apps. 1)上面获得前10名的查询,称为Top10Apps。
2) A second Query that selects from the same tables above, but where the rows are not in Top10Apps, and sums the rows / returns the sums & aggrigates, with the "Other" tag. 2)第二个查询,该查询从上面的相同表中选择,但其中行不在Top10Apps中,并对行求和/返回带有“ Other”标记的总和与集结。 Call This SumOfNotTop10Apps 将此称为SumOfNotTop10Apps
3) A third query that unions Top10App & SumOfNotTop10Apps 3)第三个查询结合了Top10App和SumOfNotTop10Apps

If that's not clear post some comments and I'll try to make it clearer. 如果还不清楚,请发表一些评论,我会尽力使其更清楚。

SELECT TOP 10 ApplicationTbl.AppName, Count(*) AS SessionNos
FROM ApplicationTbl 
INNER JOIN SessionTbl 
ON ApplicationTbl.AppID = SessionTbl.AppID
GROUP BY ApplicationTbl.AppName
ORDER BY Count(*) DESC

UNION ALL

SELECT "Other" AS AppName, Count(*) AS SessionNos
FROM (ApplicationTbl 
INNER JOIN SessionTbl 
ON ApplicationTbl.AppID = SessionTbl.AppID) 
LEFT JOIN (SELECT TOP 10 ApplicationTbl.AppName, Count(*) AS SessionNos
           FROM ApplicationTbl 
           INNER JOIN SessionTbl 
           ON ApplicationTbl.AppID = SessionTbl.AppID
           GROUP BY ApplicationTbl.AppName;
           ORDER BY Count(*) DESC)  AS s 
ON ApplicationTbl.AppName = s.AppName
WHERE s.AppName Is Null
GROUP BY "Other"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM