所以我对SQL非常不好,但是我想知道是否有人能够为我一直在为Postgresql数据库设计的SQL查询提供帮助。
select darkweb.site_title, count(*) from darkweb WHERE online ='t' GROUP BY darkweb.site_title HAVING count(*) > 1 ORDER BY count(*) DESC;
这有输出:
No title | 1773
100x Your Coins in 24 Hours - Officially Hidden Service Anonymous | 319
403 Forbidden | 284
Index of / | 215
Thank you guys! | 182
404 Not Found | 155
SecureDrop | Protecting Journalists and Sources | 108
Clone CC : No.1 Trusted onion site for Cloned Credit Card. $2000/$5000 balance available | 77
| 76
Red Room | 63
DDos Challenge | 52
Dir | 51
Hacker | Cyber Crime Solution | 51
Deep Web Hosting | Secured and Anonymous | Linux PHP hosting 100MB and Unlimited Bandwidth | 51
BKA - Seizure Banner | 50
TorLinks | .onion Link List The Hidden Wiki Deep Web Onion Urls Onionland Tor linklist | 44
ONIONLIST - SAFE .ONION LINKS LISTING | 44
输出一个: 理想情况下,我想要的输出是数量超过1的site_titles的数量。
输出b: 我还希望将所有计数(count(*)的结果)总计为一个数字。
这样一来,我最终可以使用这两个输出(在sql之外),从输出b中减去输出a,以计算重复站点的数量。
请让我知道是否有什么方法可以使我对此有所了解或可以提供帮助。
答案 0 :(得分:1)
您可以通过将上面的查询变成子查询,并使用COUNT()获取输出A,然后使用SUM()获取输出B,来实现所需的功能:
select count(*) as a, sum(amount) as b from
(
select darkweb.site_title, count(*) as amount from darkweb WHERE online ='t' GROUP BY darkweb.site_title HAVING count(*) > 1
) site_title_counts
但是请注意,这将为您提供count> 1的所有计数的总和-不确定是否正是您想要的。如果需要包括1在内的所有计数的总和,则需要在单独的查询中进行:
select sum(amount) as b from
(
select darkweb.site_title, count(*) as amount from darkweb WHERE online ='t' GROUP BY darkweb.site_title
) site_title_counts