简体   繁体   English

SELECT DISTINCT文件组和多个安装点之间的总可用空间(SQL Server)

[英]SELECT DISTINCT filegroups and the total free space across multiple mountpoints (SQL Server)

I am trying to write a query which is able to calculate the possible free space for database filegroups. 我正在尝试编写一个查询,该查询能够计算数据库文件组的可能可用空间。 I have a @table which contains the following info (this is filtered for 1 db only): 我有一个@table,其中包含以下信息(仅针对1 db进行过滤):

在此处输入图片说明

The column 'free_space' is calculated one to show the total free space in a file having in mind if the file has autogrow and the size of the mountpoint where its located. 计算“ free_space”列是为了显示文件中的总可用空间,请牢记文件是否具有自动增长功能以及文件所在的安装点的大小。

From this table I am trying to do a select which will return me a result for all databases and their filegroups calculating the free space for them across different mount points. 我正在尝试从该表中进行选择,该选择将为我返回所有数据库及其文件组的结果,以计算它们在不同安装点之间的可用空间。

Example: if 1 database has all its files on one disk - to return the free space for this disk; 示例:如果1个数据库的所有文件都放在一个磁盘上-返回该磁盘的可用空间;否则,返回0。 If it has its files on more than one disk - to return the free space for the two (or more) disks; 如果文件在多个磁盘上-返回两个(或多个)磁盘的可用空间; (the free space for each file is in the table above). (每个文件的可用空间在上表中)。

So far I am here: 到目前为止,我在这里:

SELECT
database_name
,groupid
,groupname
,SUM(file_size) as file_size
 --- The next two do the same thing;
,SUM(free_space) / (count (*) * 1.0) as free_space
,AVG(free_space)
,mount_point
FROM @file_size
--where database_name = 'kosevk'
GROUP BY mount_point, database_name, groupname
ORDER BY database_name

This runs okay until I add one more file on a different disk (example is file kosevk_data7 on disk G:) - then it returns two rows for PRIMARY filegroup. 直到我在另一个磁盘上添加另一个文件(例如,磁盘G:上的文件kosevk_data7)之前,此方法都可以正常运行-然后它为PRIMARY文件组返回两行。

http://i.imgur.com/pwDLnj1.png

I need to have each filegroup listed only once. 我只需要列出每个文件组一次。

You may remove mount_point from the GROUP BY clause and comment out mount_point in the select as below. 您可以从GROUP BY子句中删除mount_point,并在如下所示的select中注释掉mount_point。

Hopefully it will help you. 希望它将对您有所帮助。

SELECT database_name ,groupid ,groupname ,SUM(file_size) as file_size --- The next two do the same thing; SELECT database_name,groupid,groupname,SUM(file_size)as file_size ---接下来的两个做同样的事情; ,SUM(free_space) / (count (*) * 1.0) as free_space ,AVG(free_space) --,mount_point FROM @file_size --where database_name = 'kosevk' GROUP BY database_name, groupname ORDER BY database_name ,SUM(free_space)/(count(*)* 1.0)as free_space,AVG(free_space)-,mount_point FROM @file_size-其中database_name ='kosevk'GROUP BY数据库名称,groupname ORDER BY数据库名称

this seems to be the query : 这似乎是查询:

;With CTE AS (SELECT
database_name
,groupid
,groupname
,SUM(file_size) as file_size
 --- The next two do the same thing;
,SUM(free_space) / (count (*) * 1.0) as free_space
,AVG(free_space)
,mount_point
FROM @file_size)
select database_name, sum(file_size), sum(free_space), mount_point
group by database_name, mount_point

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

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