简体   繁体   English

如何在子选择中使用 Distinct 和 MAX

[英]how to use Distinct with MAX in subselect

I need to select distinct server_name, date with SUM of database_size, where I do a MAX of database_size in sub-select I can't manage to get distinct server_names.. my query is always returning duplicates (even though my table doesn't have duplicate values)我需要选择不同的 server_name,date with SUM of database_size,我在 sub-select 中做了 MAX 的 database_size 我无法获得不同的 server_names .. 我的查询总是返回重复项(即使我的表没有重复值)

My table我的桌子

id  server_name        database_size   received_at
1   abcd@ca.abc.com      271254016   2016-04-28
2   alphabet@ca.alp.com  61739776   2016-05-03
3   defg@ca.some.com     61438464   2016-05-03
4   1234@some.com        280043520  2016-05-03

The result I want: note* the date is distinct, the database_size is distinct and server_name is distinct-- none of these values should ever be the same.我想要的结果:注意*日期是不同的,database_size 是不同的,server_name 是不同的——这些值都不应该是相同的。

abcd@ca.abc.com 271254016   2016-04-28
server2@ca.com  274209280   2016-04-29
server3@ca.com  268375040   2016-04-30
server4@ca.com  277126400   2016-05-01

I tried this query: This is not right, I want DISTINCT server_name (but putting DISTINCT doesn't help - it still returns same server_name)我试过这个查询:这是不对的,我想要 DISTINCT server_name(但放置 DISTINCT 没有帮助 - 它仍然返回相同的 server_name)

select server_name, sum(data_size) as data_size, date from (select date(received_at) as date, sum(database_size) as data_size, 
server_name from schema.db_stats group by date(received_at), server_name) where  server_name like '%abc%' group by date, server_name order by date

But I get: I get server_name multiple times, I only want the server_name with the MAX amount of database_size但是我得到:我多次获得 server_name,我只想要具有 MAX 数量的 database_size 的 server_name

abcd@ca.abc.com 271254016   2016-04-28
abcd@ca.abc.com 274209280   2016-04-29
abcd@ca.abc.com 268375040   2016-04-30
abcd@ca.abc.com 277126400   2016-05-01
abcd@ca.abc.com 277126400   2016-05-02
alphabet@ca.alp.com 61739776    2016-05-03
defg@ca.some.com    61438464    2016-05-03
1234@some.com       280043520   2016-05-03

could someone please help ?有人可以帮忙吗? Will really appreciate it.真的会很感激。

I guess you are using Microsoft SQL Server, since you tagged your question .我猜您正在使用 Microsoft SQL Server,因为您将问题标记为 In the future, it would be helpful if you tag your questions with the brand of SQL database you use, because it will help people to answer with the best solution for that brand.将来,如果您用您使用的 SQL 数据库品牌标记您的问题会很有帮助,因为它将帮助人们用该品牌的最佳解决方案来回答。

I have not tested this, but I believe something like the following will do what you want:我没有测试过这个,但我相信像下面这样的东西会做你想做的:

SELECT server_name, date, data_size
FROM (
    SELECT server_name, date,
      ROW_NUMBER() OVER (PARTITION BY server_name, date ORDER BY data_size DESC) AS RowNumber  
    FROM (
        SELECT server_name, DATE(received_at) AS date, SUM(database_size) as data_size,
        FROM schema.db_stats
        WHERE server_name LIKE '%abc%'
        GROUP BY server_name, DATE(received_at)
    )
)
WHERE RowNumber = 1;  

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

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