<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Titleless Blog - SQL</title>
    <link>http://www.hierview.com/Blog/</link>
    <description>This is all just stuff!</description>
    <language>en-us</language>
    <copyright>Craig Hier</copyright>
    <lastBuildDate>Fri, 26 Mar 2010 02:46:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@hierview.com</managingEditor>
    <webMaster>blog@hierview.com</webMaster>
    <item>
      <trackback:ping>http://www.hierview.com/Blog/Trackback.aspx?guid=84e53285-dd3c-4bb0-b056-1c743baacec7</trackback:ping>
      <pingback:server>http://www.hierview.com/Blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.hierview.com/Blog/PermaLink,guid,84e53285-dd3c-4bb0-b056-1c743baacec7.aspx</pingback:target>
      <dc:creator>Craig Hier</dc:creator>
      <wfw:comment>http://www.hierview.com/Blog/CommentView,guid,84e53285-dd3c-4bb0-b056-1c743baacec7.aspx</wfw:comment>
      <wfw:commentRss>http://www.hierview.com/Blog/SyndicationService.asmx/GetEntryCommentsRss?guid=84e53285-dd3c-4bb0-b056-1c743baacec7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
I have been playing around with WinAudit for the past week.  Overall it is an
awesome little package.  One of it’s major selling points is the fact you can
stuff the audit results into a database.  What more could an admin want?
</p>
          <p>
The only drawback is the schema of the collected data.  In order to be be “futureproof”
the data is stored into a single table.  The data is stored with a “Category”
and “ItemName” with up to 5 values.  For example, the version of a software package
would have a Category of “Installed Software”, an ItemName of “Adobe Flash” and an
ItemValue1 of “10.0.45.2”.  The type of the item values is implied and not really
specified anywhere.
</p>
          <p>
This is great for extensibility since you can add new lines with reckless abandon. 
Need to add some data about the CPU  <a href="http://www.hierview.com/Blog/content/binary/WindowsLiveWriter/SitnSpinPivotinSQLServer_B3CD/schema_4.jpg"><img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="schema" border="0" alt="schema" align="left" src="http://www.hierview.com/Blog/content/binary/WindowsLiveWriter/SitnSpinPivotinSQLServer_B3CD/schema_thumb_1.jpg" width="207" height="246" /></a>temperature,
no problem!  Simply add a “Category” of Hardware, an “ItemName” of CPU Temp and
set the value to the temperature.  The problem is that it is pants for generating
reports or summary type operations.
</p>
          <p>
What you want is another table that contains the data as columns.  The picture
to the left shows what I mean.
</p>
          <p>
This problem is not insurmountable.  We could take the long, hard road and select
the values one by one based on the <strong>Category</strong> and <strong>ItemName</strong>,
inserting them into another table with columns based on the selected data point. 
This is a pain and fraught with danger.  There has to be a better way.
</p>
          <p>
The better way is using the Pivot command in SQL.  As shown below, it is all
done in one command.  Unfortunately it isn’t crystal clear what is going on. 
Let’s take a look at some key parts…
</p>
          <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INSERT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INSERT</a><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INTO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INTO</a> [IT].[dbo].[ComputerInfo] </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2:            ([ComputerName]  ,[DomainName]
</pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:            ,[Description]   ,[OS]
</pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:            ,[Manufacturer]  ,[Model]
</pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:            ,[SerialNumber]  ,[AssetTag]
</pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:            ,[Processors]    ,[ProcessorDescription]
</pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:            ,[Memory]        ,[HardDrive]
</pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8:            ,[BIOS]          ,[UpTime]
</pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:            ,[Roles])
</pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=Select&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">Select</a><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Computer
Name]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=As&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">As</a> ComputerName, </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([<a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=Domain&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">Domain</a> Name]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> DomainName, <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Description]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> Description, </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Operating
System]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> OperatingSystem, </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Manufacturer]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> Manufacturer, <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Model]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> Model, </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Serial
Number]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> SerialNumber, <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Asset
Tag]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> AssetTag, </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Number <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=Of&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">Of</a> Processors]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> NumProcs, </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Processor
Description]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> ProcDescription, </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Total
Memory]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> TotalMemory, </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Total
Hard Drive]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> TotalHDD, </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([BIOS
Version]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> BIOS, <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([System
Uptime]), </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:        <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>([Roles]) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=As&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">As</a> Roles </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21:   <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=From&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">From</a> [IT].[dbo].[WinAudi2] <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> data </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: pivot (<a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=max&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">max</a>(ItemValue1) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=for&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">for</a> ItemName <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=in&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">in</a></pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:       ([Computer Name], [<a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=Domain&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">Domain</a> Name],
[Description], </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:        [Roles], [Operating System], [Manufacturer], [Model],
</pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:        [Serial Number], [Asset Tag], [Number <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=Of&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">Of</a> Processors], </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26:        [Processor Description], [Total Memory], [Total Hard Drive], [System Uptime], [BIOS Version]
</pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27:       )) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=as&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">as</a> result </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28:    <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=where&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">where</a> Category
= '<span style="color: #8b0000">System Overview</span>' </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29:    <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=group&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">group</a><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=by&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">by</a> Computer</pre>
          </pre>
          <p>
This is an overly complex example as I needed to combine several different audit records
for the same PC.  The basic structure of the pivot command is as follows…
</p>
          <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #800000">Select</span> &lt;<span style="color: #800000">New</span><span style="color: #800000">Columns</span>&gt; </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2:   <span style="color: #800000">From</span> &lt;<span style="color: #800000">Original</span><span style="color: #800000">Table</span>&gt; <span style="color: #800000">as</span><span style="color: #800000">TableTag</span></pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:  <span style="color: #800000">Pivot</span> (<span style="color: #800000">AggrergateCommand</span>(<span style="color: #800000">Column</span><span style="color: #800000">to</span><span style="color: #800000">use</span><span style="color: #800000">as</span><span style="color: #800000">value</span>) </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:            <span style="color: #800000">for</span><span style="color: #800000">OtherColumn</span><span style="color: #800000">in</span></pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:              ([<span style="color: #800000">Value</span><span style="color: #800000">that</span><span style="color: #800000">OtherColumn</span><span style="color: #800000">is</span><span style="color: #800000">equal</span><span style="color: #800000">to</span>], </pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:               [<span style="color: #800000">Next</span><span style="color: #800000">value</span><span style="color: #800000">that</span><span style="color: #800000">OtherColumn</span><span style="color: #800000">is</span><span style="color: #800000">equal</span><span style="color: #800000">to</span>], </pre>
            <pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:               ...)
</pre>
            <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: </pre>
          </pre>
          <p>
Pivoting is done on a single column.  In our case, it is the <strong>ItemName</strong> column. 
When <strong>ItemName</strong> is “Computer Name” we want the value in the ItemValue1
column to be the value associated with the <strong>ComputerName</strong> column. 
Makes sense.
</p>
          <p>
I was confuse by the [Name] notation at first.  This is basically a faux variable
that is also used as a comparison value.  Basically the internal phrase is used
as <strong>ItemName = ‘Operating System’</strong> for example.  
</p>
          <p>
The problem is that many rows might have <strong>ItemName = ‘Operating System’</strong>. 
That is why we have to have some sort of aggregation function.  I chose <strong>Max()</strong> since
it is simple and the values should be equal anyway.
</p>
        </blockquote>
        <img width="0" height="0" src="http://www.hierview.com/Blog/aggbug.ashx?id=84e53285-dd3c-4bb0-b056-1c743baacec7" />
      </body>
      <title>Sit ‘n Spin: Pivot in SQL Server</title>
      <guid isPermaLink="false">http://www.hierview.com/Blog/PermaLink,guid,84e53285-dd3c-4bb0-b056-1c743baacec7.aspx</guid>
      <link>http://www.hierview.com/Blog/2010/03/26/SitNSpinPivotInSQLServer.aspx</link>
      <pubDate>Fri, 26 Mar 2010 02:46:00 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
I have been playing around with WinAudit for the past week.&amp;#160; Overall it is an
awesome little package.&amp;#160; One of it’s major selling points is the fact you can
stuff the audit results into a database.&amp;#160; What more could an admin want?
&lt;/p&gt;
&lt;p&gt;
The only drawback is the schema of the collected data.&amp;#160; In order to be be “futureproof”
the data is stored into a single table.&amp;#160; The data is stored with a “Category”
and “ItemName” with up to 5 values.&amp;#160; For example, the version of a software package
would have a Category of “Installed Software”, an ItemName of “Adobe Flash” and an
ItemValue1 of “10.0.45.2”.&amp;#160; The type of the item values is implied and not really
specified anywhere.
&lt;/p&gt;
&lt;p&gt;
This is great for extensibility since you can add new lines with reckless abandon.&amp;#160;
Need to add some data about the CPU&amp;#160; &lt;a href="http://www.hierview.com/Blog/content/binary/WindowsLiveWriter/SitnSpinPivotinSQLServer_B3CD/schema_4.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="schema" border="0" alt="schema" align="left" src="http://www.hierview.com/Blog/content/binary/WindowsLiveWriter/SitnSpinPivotinSQLServer_B3CD/schema_thumb_1.jpg" width="207" height="246" /&gt;&lt;/a&gt;temperature,
no problem!&amp;#160; Simply add a “Category” of Hardware, an “ItemName” of CPU Temp and
set the value to the temperature.&amp;#160; The problem is that it is pants for generating
reports or summary type operations.
&lt;/p&gt;
&lt;p&gt;
What you want is another table that contains the data as columns.&amp;#160; The picture
to the left shows what I mean.
&lt;/p&gt;
&lt;p&gt;
This problem is not insurmountable.&amp;#160; We could take the long, hard road and select
the values one by one based on the &lt;strong&gt;Category&lt;/strong&gt; and &lt;strong&gt;ItemName&lt;/strong&gt;,
inserting them into another table with columns based on the selected data point.&amp;#160;
This is a pain and fraught with danger.&amp;#160; There has to be a better way.
&lt;/p&gt;
&lt;p&gt;
The better way is using the Pivot command in SQL.&amp;#160; As shown below, it is all
done in one command.&amp;#160; Unfortunately it isn’t crystal clear what is going on.&amp;#160;
Let’s take a look at some key parts…
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  1: &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=INSERT&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;INSERT&lt;/a&gt; &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=INTO&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;INTO&lt;/a&gt; [IT].[dbo].[ComputerInfo] &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  2:            ([ComputerName]  ,[DomainName]
&lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  3:            ,[Description]   ,[OS]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  4:            ,[Manufacturer]  ,[Model]
&lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  5:            ,[SerialNumber]  ,[AssetTag]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  6:            ,[Processors]    ,[ProcessorDescription]
&lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  7:            ,[Memory]        ,[HardDrive]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  8:            ,[BIOS]          ,[UpTime]
&lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  9:            ,[Roles])
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 10: &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=Select&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;Select&lt;/a&gt; &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Computer
Name]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=As&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;As&lt;/a&gt; ComputerName, &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 11:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=Domain&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;Domain&lt;/a&gt; Name]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; DomainName, &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Description]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; Description, &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 12:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Operating
System]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; OperatingSystem, &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 13:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Manufacturer]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; Manufacturer, &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Model]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; Model, &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 14:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Serial
Number]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; SerialNumber, &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Asset
Tag]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; AssetTag, &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 15:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Number &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=Of&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;Of&lt;/a&gt; Processors]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; NumProcs, &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 16:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Processor
Description]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; ProcDescription, &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 17:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Total
Memory]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; TotalMemory, &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 18:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Total
Hard Drive]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; TotalHDD, &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 19:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([BIOS
Version]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; BIOS, &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([System
Uptime]), &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 20:        &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;([Roles]) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=As&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;As&lt;/a&gt; Roles &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 21:   &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=From&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;From&lt;/a&gt; [IT].[dbo].[WinAudi2] &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; data &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 22: pivot (&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=max&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;max&lt;/a&gt;(ItemValue1) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=for&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;for&lt;/a&gt; ItemName &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=in&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;in&lt;/a&gt; &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 23:       ([Computer Name], [&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=Domain&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;Domain&lt;/a&gt; Name],
[Description], &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 24:        [Roles], [Operating System], [Manufacturer], [Model],
&lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 25:        [Serial Number], [Asset Tag], [Number &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=Of&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;Of&lt;/a&gt; Processors], &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 26:        [Processor Description], [Total Memory], [Total Hard Drive], [System Uptime], [BIOS Version]
&lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 27:       )) &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=as&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;as&lt;/a&gt; result &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 28:    &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=where&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;where&lt;/a&gt; Category
= '&lt;span style="color: #8b0000"&gt;System Overview&lt;/span&gt;' &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt; 29:    &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=group&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;group&lt;/a&gt; &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=by&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;by&lt;/a&gt; Computer&lt;/pre&gt;&lt;/pre&gt;
&lt;p&gt;
This is an overly complex example as I needed to combine several different audit records
for the same PC.&amp;#160; The basic structure of the pivot command is as follows…
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #800000"&gt;Select&lt;/span&gt; &amp;lt;&lt;span style="color: #800000"&gt;New&lt;/span&gt; &lt;span style="color: #800000"&gt;Columns&lt;/span&gt;&amp;gt; &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  2:   &lt;span style="color: #800000"&gt;From&lt;/span&gt; &amp;lt;&lt;span style="color: #800000"&gt;Original&lt;/span&gt; &lt;span style="color: #800000"&gt;Table&lt;/span&gt;&amp;gt; &lt;span style="color: #800000"&gt;as&lt;/span&gt; &lt;span style="color: #800000"&gt;TableTag&lt;/span&gt; &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  3:  &lt;span style="color: #800000"&gt;Pivot&lt;/span&gt; (&lt;span style="color: #800000"&gt;AggrergateCommand&lt;/span&gt;(&lt;span style="color: #800000"&gt;Column&lt;/span&gt; &lt;span style="color: #800000"&gt;to&lt;/span&gt; &lt;span style="color: #800000"&gt;use&lt;/span&gt; &lt;span style="color: #800000"&gt;as&lt;/span&gt; &lt;span style="color: #800000"&gt;value&lt;/span&gt;) &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  4:            &lt;span style="color: #800000"&gt;for&lt;/span&gt; &lt;span style="color: #800000"&gt;OtherColumn&lt;/span&gt; &lt;span style="color: #800000"&gt;in&lt;/span&gt; &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  5:              ([&lt;span style="color: #800000"&gt;Value&lt;/span&gt; &lt;span style="color: #800000"&gt;that&lt;/span&gt; &lt;span style="color: #800000"&gt;OtherColumn&lt;/span&gt; &lt;span style="color: #800000"&gt;is&lt;/span&gt; &lt;span style="color: #800000"&gt;equal&lt;/span&gt; &lt;span style="color: #800000"&gt;to&lt;/span&gt;], &lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  6:               [&lt;span style="color: #800000"&gt;Next&lt;/span&gt; &lt;span style="color: #800000"&gt;value&lt;/span&gt; &lt;span style="color: #800000"&gt;that&lt;/span&gt; &lt;span style="color: #800000"&gt;OtherColumn&lt;/span&gt; &lt;span style="color: #800000"&gt;is&lt;/span&gt; &lt;span style="color: #800000"&gt;equal&lt;/span&gt; &lt;span style="color: #800000"&gt;to&lt;/span&gt;], &lt;/pre&gt;&lt;pre style="background-color: #ade2b0; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  7:               ...)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;  8: &lt;/pre&gt;&lt;/pre&gt;
&lt;p&gt;
Pivoting is done on a single column.&amp;#160; In our case, it is the &lt;strong&gt;ItemName&lt;/strong&gt; column.&amp;#160;
When &lt;strong&gt;ItemName&lt;/strong&gt; is “Computer Name” we want the value in the ItemValue1
column to be the value associated with the &lt;strong&gt;ComputerName&lt;/strong&gt; column.&amp;#160;
Makes sense.
&lt;/p&gt;
&lt;p&gt;
I was confuse by the [Name] notation at first.&amp;#160; This is basically a faux variable
that is also used as a comparison value.&amp;#160; Basically the internal phrase is used
as &lt;strong&gt;ItemName = ‘Operating System’&lt;/strong&gt; for example.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
The problem is that many rows might have &lt;strong&gt;ItemName = ‘Operating System’&lt;/strong&gt;.&amp;#160;
That is why we have to have some sort of aggregation function.&amp;#160; I chose &lt;strong&gt;Max()&lt;/strong&gt; since
it is simple and the values should be equal anyway.
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://www.hierview.com/Blog/aggbug.ashx?id=84e53285-dd3c-4bb0-b056-1c743baacec7" /&gt;</description>
      <comments>http://www.hierview.com/Blog/CommentView,guid,84e53285-dd3c-4bb0-b056-1c743baacec7.aspx</comments>
      <category>SQL</category>
    </item>
  </channel>
</rss>