I am trying to create a report using Microsoft SQL Server Management Studio 2012. I've got a fair amount of experience writing SQL reports, but have never had to use what I think should be a PIVOT until now.
Our RMM - Kaseya, uses the same column for all "custom audit fields", we have made a few custom "columns" like: SMART Drive Model, SMART Status, and SMART Last Updated date. You'd think these are defined as their own columns, but no -- regular entries in a cell, linked to an agentguid.
From looking at the table below, the fieldValue on the right is dependent on the fieldName to the left of it. I'm not sure how to turn these rows into their own columns based on the fieldName.
I'm seeking a query that produces five columns:
- agentguid
- Model
- Size
- Updated
- Status
I will later run an INNER JOIN on this data, to compare against the agentguid and be able to customize my final report.
Below is a sample of data
SELECT * FROM vSystemInfoManual;
Machine_GroupID | agentGuid | fieldName | fieldValue |
---|---|---|---|
COMPUTER1.companyB | 101159575290188 | SMART Drive Model | PLEXTOR PX-256M5Pro ATA Device |
COMPUTER1.companyB | 101159575290188 | Bitlocker Status | Off |
COMPUTER1.companyB | 101159575290188 | SMART Drive Size | 238GB |
COMPUTER1.companyB | 101159575290188 | SMART Updated | 01/14/2021 |
COMPUTER1.companyB | 101159575290188 | SMART Status | CAUTION |
COMPUTER2.companyA | 110275323879092 | SMART Drive Model | ST500DM002-1BD142 |
COMPUTER2.companyA | 110275323879092 | Bitlocker Status | Off |
COMPUTER2.companyA | 110275323879092 | SMART Drive Size | 455GB |
COMPUTER2.companyA | 110275323879092 | SMART Updated | 01/14/2021 |
COMPUTER2.companyA | 110275323879092 | SMART Status | CAUTION |
The table should end up looking like this:
agentguid | Model | Size | Updated | Status |
---|---|---|---|---|
101159575290188 | PLEXTOR PX-256M5Pro ATA Device | 238GB | 01/14/2021 | CAUTION |
101159575290188 | ST500DM002-1BD142 | 455GB | 01/14/2021 | CAUTION |
I think my goal is to use either a PIVOT, UNPIVOT, or a CROSS, but I can't figure out how to make any of them work. Any help or guidance would be appreciated, thanks!