I usually a lot of logic with script/code, but I'm having to rely on access-sql a lot more, and i'm trying to do a couple of operations properly in sql, instead of hacking around it in VBA or PowershellI'm trying to collect information from multiple sources on servers, I can't use a clearly set of attributes, and I need to be able to add attributes on the fly.I've got the database setup, and the structure, but reporting off of it is a pain, I'm trying to automate a couple of things in order to keep all the data, but have usable data for some reporting/other operationsTable StructureTable StructureTable 1 ServersID (AutoNumber)DisplayName (String)Description (String)Table 2 AttributesID (AutoNumber)Displayname (String) # Usually something like IPOWNERID (FK link to ID in Servers Table)ATVALUE (String)1)How to find the most common result of attributesI may have data like this (combined from 2 different tables)SERVER1 IP_SCOM 192.168.1.1SERVER1 IP_SCCM 192.168.1.1SERVER1 IP_OTHR 192.168.1.1SERVER1 IP_OLDE 192.168.1.2SERVER2 IP_SCOM 192.168.1.2SERVER2 IP_SCCM 192.168.1.3SERVER2 IP_OTHR 192.168.1.3SERVER2 IP_OLDE 192.168.1.3In this case I'm looking for SERVER1 IP_COMMON 192.168.1.1SERVER2 IP_COMMON 192.168.1.3I have this so far, that works to give me the occurrence of IP addresses for a given server, but I'm having a problem getting it to only return the one with the highest count value.SELECT DISTINCT SERVER.DISPLAYNAME, SERVER.ID, Count(ATTRIBUTE.ATVALUE) AS CountOfATVALUE, ATTRIBUTE.ATVALUEFROM SERVER INNER JOIN ATTRIBUTE ON SERVER.ID = ATTRIBUTE.OWNERIDWHERE (((ATTRIBUTE.DISPLAYNAME) Like "IP_*"))GROUP BY SERVER.DISPLAYNAME, SERVER.ID, ATTRIBUTE.ATVALUEORDER BY SERVER.DISPLAYNAME, Count(ATTRIBUTE.ATVALUE) DESC;2) Pivot the structure with text valuesthere currently is a list of about 20 different ATTRIBUTE Display Names.I want to Pivot off of the ATTRIBUTE.DISPLAY Name and then use the attribute.value for the valueReferencing above assume the following data (between the SERVER and ATTRIBUTE Table)SERVER1 IP_SCOM 192.168.1.1SERVER1 IP_SCCM 192.168.1.1SERVER1 IP_OTHR 192.168.1.1SERVER1 IP_OLDE 192.168.1.2SERVER1 OS WINDOWSSERVER1 SN 12345SERVER2 IP_SCOM 192.168.1.2SERVER2 IP_SCCM 192.168.1.3SERVER2 IP_OTHR 192.168.1.3SERVER2 IP_OLDE 192.168.1.3SERVER2 OS LINUXOutputSERVERNAME IP_SCOM IP_SCCM IP_OTHR IP_OLDE OS SNSERVER1 192.168.1.1 192.168.1.1 192.168.1.1 192.168.1.2 WINDOWS 12345SERVER2 192.168.1.2 192.168.1.3 192.168.1.3 192.168.1.3 LINUX nullI won't actually have an idea of what all the column names will need to be, I'm thinking all of the distinct values of the ATTRIBUTE.DISPLAYNAME.
5/12/2014 6:52:31 PM
Dudehttp://stackoverflow.com/Not saying people here can't be of assistance, but you'll get a lot more action there.
5/12/2014 9:05:07 PM
1) Use a HAVING clause; it's basically a WHERE on a summary. You could also do an ORDER BY DESC and use TOP 1, but that would not work if there were multiple display values whose number of entries are the same and are the maximum...2) If you don't know all the column names, then you can't use a conventional PIVOT, at least not one that I'm aware of. It's a limitation of PIVOTs. You could use dynamic SQL to build the PIVOT statement, but at that point, you might as well just hack it out in Access or Crystal Reports.
5/12/2014 10:34:44 PM
I'm having trouble reading your English, but I think you fucked up designing your tables.
5/13/2014 10:35:13 AM
What would you change in the table design?I'm certainly open to reccommendations
5/13/2014 2:54:39 PM
I still don't understand why you don't put the server attributes in the servers table.That would solve this reporting problem.]
5/13/2014 2:57:55 PM
Okay, I need you to explain this:
5/13/2014 3:40:12 PM
^thats kind of my plan right now, is to have a Reporting Table for each object type, i was hoping to have that be a dynamically generated view, but I guess i'll need to use a table.Its not just servers, its actually a lot of stuff.Items could be servers, routers switches et cetra.I'm basically using the 2 tables to store classes of objects.Server table has an object type (I.E. Server, router, switch) and a nameAttribute table contains things that describe it (I.E. IP Address).I'm pulling in data from about 10 different sources, which overlap objects I.E. 2 different services SCOM and Solarwinds might both have data about a device.I also need compliance reporting, I.E. a Windows Server must exist in these 5 systems, but I also need functional data.I do have a Class type structure built into the database. I.E. Servers have IP, Serial, and OSa Virtual Server has everthing a Server has and then adds HypervisorA week or so later I get a new report, which provides a new attribute say Hypervisor Hostso now a Virtual Server has IP, Serial, OS, Hypervisor and Hypervisor host
5/13/2014 4:30:11 PM