First off I'm terrible at SQL and this is proving that point. The end goal is I have a database (What's Up Gold) that I'm trying to export all the devices out of and getting a few key fields in the process. I'm able to get all the data I want with the query below. However, due to the joins, it's returning duplicate rows where it finds a match. Ideally, I'd want each unique item added as a column so I'm left with one row per device with as many columns as needed to get all the attributes even if it's null.
SELECT Device.sDisplayName, NetworkInterface.sNetworkAddress, NetworkInterface.sNetworkName, DeviceGroup.sGroupName,
ActiveMonitorType.sMonitorTypeName, ActionPolicy.sPolicyName, Device.sNote , DeviceAttribute.sValue
FROM NetworkInterface join Device ON (NetworkInterface.nDeviceID = Device.nDeviceID)
left outer join dbo.DeviceType on Device.nDeviceTypeID = DeviceType.nDeviceTypeID
left outer join dbo.PivotDeviceToGroup on PivotDeviceToGroup.nDeviceID = Device.nDeviceID
left outer join dbo.DeviceGroup on DeviceGroup.nDeviceGroupID = PivotDeviceToGroup.nDeviceGroupID
left outer join dbo.PivotActiveMonitorTypeToDevice on PivotActiveMonitorTypeToDevice.nDeviceID = Device.nDeviceID
left outer join dbo.ActiveMonitorType on ActiveMonitorType.nActiveMonitorTypeID = PivotActiveMonitorTypeToDevice.nActiveMonitorTypeID
left outer join ActionPolicy ON Device.nActionPolicyID = ActionPolicy.nActionPolicyID
left outer JOIN DeviceAttribute ON Device.nDeviceID = DeviceAttribute.nDeviceID
WHERE Device.bRemoved = 0 AND sMonitorTypename NOT LIKE '%Interface%' ORDER BY sNetworkName
Example returned data:
sDisplayName sNetworkAddress sGroupName sMonitorTypeName sPolicyName sNote sValue
Portland DCI South 10.0.0.1 Main WAN MAP Ping DCI 10 Gig Alert Added from Discovery on Mon Sep 26 12:46:30 2011 Portland HQ Data Center
Portland DCI South 10.0.0.1 Main WAN MAP Ping DCI 10 Gig Alert Added from Discovery on Mon Sep 26 12:46:30 2011 PDX Data Center 10 Gig DCI South
Portland DCI South 10.0.0.1 Main WAN MAP Ping DCI 10 Gig Alert Added from Discovery on Mon Sep 26 12:46:30 2011 ETH10000-XXXXXXX
Portland DCI South 10.0.0.1 Main WAN MAP Ping DCI 10 Gig Alert Added from Discovery on Mon Sep 26 12:46:30 2011 CenturyLink - 1-888-345-4762 option 2
What I'd like to see is that for every unique item in sValue
column it would be returned as sValue1
sValue2
etc. That way I'm left with a single row per unique network device which is made unique by sNetworkAddress
field.
Desired query output:
sDisplayName sNetworkAddress sGroupName sMonitorTypeName sPolicyName sNote sValue1 sValue2 sValue3 sValue4
Portland DCI South 10.0.0.1 Main WAN MAP Ping DCI 10 Gig Alert Added from Discovery on Mon Sep 26 12:46:30 2011 Portland HQ Data Center PDX Data Center 10 Gig DCI South ETH10000-XXXXXXX CenturyLink - 1-888-345-4762 option 2
Updated Query to minimal Select
SELECT d.sDisplayName
, ni.sNetworkAddress
, ni.sNetworkName
, d.sNote
, da.sValue
FROM NetworkInterface ni
INNER join Device d ON ni.nDeviceID = d.nDeviceID
AND d.bRemoved = 0
left outer join dbo.PivotDeviceToGroup pdg on pdg.nDeviceID = d.nDeviceID
left outer join dbo.DeviceGroup dg on dg.nDeviceGroupID = pdg.nDeviceGroupID
left outer join dbo.PivotActiveMonitorTypeToDevice pamtd on pamtd.nDeviceID = d.nDeviceID
left outer join dbo.ActiveMonitorType amt on amt.nActiveMonitorTypeID = pamtd.nActiveMonitorTypeID
AND amt.sMonitorTypename NOT LIKE '%Interface%'
left outer join ActionPolicy ap ON ap.nActionPolicyID = d.nActionPolicyID
left outer JOIN DeviceAttribute da ON da.nDeviceID = d.nDeviceID
ORDER BY ni.sNetworkName
Updated Query with Pivot
SELECT d.sDisplayName
, ni.sNetworkAddress
, ni.sNetworkName
, d.sNote
, da.sValue
INTO #TEMP
FROM NetworkInterface ni
INNER join Device d ON ni.nDeviceID = d.nDeviceID
AND d.bRemoved = 0
left outer join dbo.PivotDeviceToGroup pdg on pdg.nDeviceID = d.nDeviceID
left outer join dbo.DeviceGroup dg on dg.nDeviceGroupID = pdg.nDeviceGroupID
left outer join dbo.PivotActiveMonitorTypeToDevice pamtd on pamtd.nDeviceID = d.nDeviceID
left outer join dbo.ActiveMonitorType amt on amt.nActiveMonitorTypeID = pamtd.nActiveMonitorTypeID
AND amt.sMonitorTypename NOT LIKE '%Interface%'
left outer join ActionPolicy ap ON ap.nActionPolicyID = d.nActionPolicyID
left outer JOIN DeviceAttribute da ON da.nDeviceID = d.nDeviceID
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.sValue)
FROM #TEMP c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT sDisplayName, sNetworkAddress, sNote' + @cols + ' from
(
SELECT
DisplayName
,sNetworkAddress
,sNetworkName
,sNote
,sValue
from temp
) x
pivot
(
max(sNote)
for sValue in (' + @cols + ')
) p '
execute(@query)
drop table #TEMP
Returns Error:
(2195 row(s) affected)
Msg 1038, Level 15, State 4, Line 11
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
Msg 1038, Level 15, State 4, Line 53
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.