2

To getting the disk space details I am using the following query.

E.g.
SELECT distinct(volume_mount_point), 
 total_bytes/1048576 as Size_in_MB, 
 available_bytes/1048576 as Free_in_MB,
 (total_bytes/1048576)-(available_bytes/1048576) as Used_in_MB,
 (((total_bytes/1048576)-(available_bytes/1048576))/(total_bytes/1048576))*100 as Used_in_Percentage
FROM sys.master_files AS f CROSS APPLY 
 sys.dm_os_volume_stats(f.database_id, f.file_id)
group by volume_mount_point, total_bytes/1048576, 
 available_bytes/1048576 order by 1

But the above query returns '0' in percentage column.How to fix this error?

asked Feb 24, 2017 at 11:36

1 Answer 1

2

Change some of the 1048576's to 1048576.0.

SELECT distinct(volume_mount_point), 
 total_bytes/1048576 as Size_in_MB, 
 available_bytes/1048576.0 as Free_in_MB,
 (total_bytes/1048576)-(available_bytes/1048576.0) as Used_in_MB,
 (((total_bytes/1048576)-(available_bytes/1048576.0))/(total_bytes/1048576))*100 as Used_in_Percentage
FROM sys.master_files AS f CROSS APPLY 
 sys.dm_os_volume_stats(f.database_id, f.file_id)
group by volume_mount_point, total_bytes/1048576, 
 available_bytes/1048576.0 order by 1

It's an awful query though.

answered Feb 24, 2017 at 11:42
2
  • your suggestion returns the following error.'Column 'sys.dm_os_volume_stats.total_bytes' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause' Commented Feb 24, 2017 at 11:44
  • It's due to the way you do the group by. See the code sample I've posted. Commented Feb 24, 2017 at 11:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.