Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

How to join one table to another that needs pivoting?

$
0
0
Hi All,I've inherited a database that contains VMWare server information such as CPU, RAM, Disks, etc.And I need to run a monthly report that shows the server spec for each VM.VCenter is polled for data every hour and the info on the VMs is recorded in a SQL Server database.So at the end of each month i've got approx. 700 rows of data - CPU, RAM, etc - for each VM and I need to report on the largest value for each column.This is because, If for example a department increases RAM from 32Gb to 64Gb they will be charged for the larger value for the entire month.Simple enough, where it gets tricky is for the storage info which is kept in a separate table.Each VM has an ID so you can identify which disks are associated to it, however the data for each is disk is recorded in separate rows.So for example, a VM could have 6 disks associated with it. So every hour 6 rows of data are inserted into the db for that VM showing the disk size & storage tier.I want the report to show the max values for all data items on a single line (rather than have 6 lines for each server where the only data changing is the disk size). This is not a problem for CPU, RAM, etc but it order to get the disk info on a single line, i'm guessing the simplest way is to use PIVOT ?Where i'm getting confused is how I can join my server and disk info tables together when one needs pivoting.So if I query table A for server info i get backVM_ID ServerName CPU RAM PLATFORM Replicated1 MyServer 4 48 Windows YIf I query TABLE B I getVM_ID DISK_ID Size Tier1 1234/1 500 21 1234/2 1024 21 1234/3 400 2The end result should look like:VM_ID ServerName CPU RAM PLATFORM Replicated Disk 1 Size Disk 1 Tier Disk 2 Size Disk 2 Tier Disk 3 Size Disk 3 Tier1 MyServer1 4 48 Windows Y 500 2 1024 2 400 2So I need a report that shows 1 line for each server with the max value for each item displayed for that month (except tier which will be min value).Also, as disks can be added, it needs to be dynamic to pick up additional disk ID's as they appear.Any suggestions on the best way to achieve this?Many thanks!

Viewing all articles
Browse latest Browse all 3145

Trending Articles