<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Reporting Services</title>
        <link>http://weblogs.sqlteam.com/jeffs/category/300.aspx</link>
        <description>Anything to do with designing reports using SQL Reporting Services.</description>
        <language>en-US</language>
        <copyright>Jeff Smith</copyright>
        <managingEditor>smith_jeffreyt@yahoo.com</managingEditor>
        <generator>Subtext Version 1.9.4.0</generator>
        <item>
            <title>The Mailbag: Referencing Assemblies in Reporting Services; some SQL help</title>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2007/10/18/60377.aspx</link>
            <description>As &lt;a href="http://en.wikipedia.org/wiki/David_Letterman" target="_blank"&gt;David Letterman&lt;/a&gt; would say, wake the kids, call the neighbors, it's time for &lt;a href="http://weblogs.sqlteam.com/jeffs/contact.aspx"&gt;The Mailbag!&lt;/a&gt;  Just some quickies today.&lt;br /&gt;
&lt;br /&gt;
Christopher writes:&lt;br /&gt;
&lt;br /&gt;
&lt;p style="font-family: Arial; margin-left: 40px; font-style: italic;"&gt;Greetings Jeff,&lt;br /&gt;
&lt;br /&gt;
First and foremost, great job with all of the blogs. I have a questions&lt;br /&gt;
that I cannot seem to get a straight answer for. I am working with SQL&lt;br /&gt;
Server Reporting Services (SSRS) and have the need to create VB&lt;br /&gt;
functions to customize the reports generated. For example, a setter/getter to&lt;br /&gt;
display information that would not be readily available from the&lt;br /&gt;
query. SSRS allows this type of custom Visual Basic code to reside in the&lt;br /&gt;
report itself, but since most of my code is across multiple reports, it&lt;br /&gt;
makes 0 sense to place the same code in each report. Do you know of a&lt;br /&gt;
way that I can create a "code library" so that there is one location for&lt;br /&gt;
all of the code and all of the reports can access it?&lt;br /&gt;
&lt;br /&gt;
Thank you very much for your time and effort.&lt;br /&gt;
&lt;br /&gt;
Sincerely,&lt;br /&gt;
Christopher McGraw&lt;/p&gt;
Chris, Here's all the information that you need:  &lt;a href="http://msdn2.microsoft.com/en-us/library/ms153561.aspx" target="_blank"&gt;Using Custom Assemblies with Reports&lt;/a&gt;  (from SQL Server 2005 Books On Line).  I can't really add much to it -- it explains everything in detail.&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;br /&gt;
&lt;/div&gt;
Traner writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-family: Arial; font-style: italic;"&gt;
&lt;p&gt;I have two tables, table1's primary key is table2's foreign key.&lt;br /&gt;
Table2 has multiple dates that relate to table1.&lt;br /&gt;
&lt;br /&gt;
ie. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;Table1  : Table2&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;JOE      : 10/18/07&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;DAVE    : 11/14/07&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;JOE      : 11/27/08&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br /&gt;
I am trying to get highest date from table2 related to table1.  Do you&lt;br /&gt;
know how this can be accomplished.  Any assistance will be greatly&lt;br /&gt;
appreciated.&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
That really isn't the clearest way to convey your schema, but I think I know what you mean.   Let's assume that Table1 has two columns, ID and Name, and that table2 has 3 columns, ID, Date and Value.&lt;br /&gt;
&lt;br /&gt;
First, to get the highest date per ID, you simply use the MAX() aggregate function along with GROUP BY when selecting from Table2:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;select ID, Max(Date) as MaxDate&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;from Table2&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;group by ID&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Be sure to read up and practice with GROUP BY and aggregate functions if you are not familiar with them -- they are very powerful and very fundamental to SQL. &lt;br /&gt;
&lt;br /&gt;
Now, you can select from Table1 and join to the above SQL statement in a derived table to return all rows from Table1 and the maximum date from table 2:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New; font-weight: bold;"&gt;select Table1.ID, Table1.&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-weight: bold;"&gt;Name, t2.MaxDate&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;from Table1&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;inner join &lt;/span&gt;&lt;br /&gt;
(  select ID, Max(Date) as MaxDate&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   from Table2&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   group by ID&lt;br /&gt;
) t2 on Table1.ID = Table2.ID&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
As you can see, we have enclosed our previous SQL statement in parenthesis, and assigned it an alias ("t2" in this case), and we can join to it and select from it just like any other table.  So, we have simply selected from Table1 and joined to a summarized version of Table2 that only returns the maximum date per ID.&lt;br /&gt;
&lt;br /&gt;
Finally, notice that all we are returning from Table2 is the date; if there are other columns from Table2 that we wish to return (for example, the "Value" column), we must add &lt;span style="font-style: italic;"&gt;another &lt;/span&gt;join to Table2, that returns only the row with the maximum date per ID:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;select &lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;Table1.&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;ID, &lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;Table1.&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;Name, t2.MaxDate, &lt;span style="font-weight: bold;"&gt;Table2.Value&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt; from Table1&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt; inner join &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt; (  select ID, Max(Date) as MaxDate&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   from Table2&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   group by ID&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt; ) t2 on Table1.ID = Table2.ID&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;inner join Table2 on t2.ID = Table2.ID and t2.MaxDate = t2.Date&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Notice that the "on" clause indicates that we are joining on ID as you might expect, but also on MaxDate = Date, so that we only return the row in Table2 that has the maximum date per ID.&lt;br /&gt;
&lt;br /&gt;
If you are using SQL Server 2005, this can be done a little easier, with only one join, using the new Rank() function:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;select x.ID, x.Name, x.Date, x.Value&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;from&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;  select Table1.ID, Table1.Name, Table2.Date, Table2.Value,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New; font-weight: bold;"&gt;   rank() over (partion by Table1.ID order by Table2.Date DESC) as Rank&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;  from Table1&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;  inner join Table2 on Table1.ID = Table2.ID&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;) x&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New; font-weight: bold;"&gt;where x.Rank = 1&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
More on Rank() and partitions &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/03/28/60146.aspx"&gt;here&lt;/a&gt;.  Hope this helps you out. &lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60377.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2007/10/18/60377.aspx</guid>
            <pubDate>Thu, 18 Oct 2007 16:19:54 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/jeffs/comments/60377.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2007/10/18/60377.aspx#feedback</comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60377.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60377.aspx</trackback:ping>
        </item>
    </channel>
</rss>