<p>I'm new to SQL Server Reporting Services, and was wondering the best way to do the following:</p>

<blockquote>
  <ul>
  <li>Query to get a list of popular IDs</li>
  <li>Subquery on each item to get properties from another table</li>
  </ul>
</blockquote>

<p>Ideally, the final report columns would look like this:</p>

<pre><code>[ID] [property1] [property2] [SELECT COUNT(*)
                              FROM AnotherTable 
                              WHERE ForeignID=ID]
</code></pre>

<p>There may be ways to construct a giant SQL query to do this all in one go, but I'd prefer to compartmentalize it. Is the recommended approach to write a VB function to perform the subquery for each row? Thanks for any help.</p>

[{'answer_id': 20895, 'author': 'Bryan Roth', 'author_id': 299, 'author_profile': 'https://Stackoverflow.com/users/299', 'pm_score': 3, 'selected': True, 'text': '<p>I would recommend using a <a href="http://msdn.microsoft.com/en-us/library/ms160348.aspx" rel="nofollow noreferrer">SubReport</a>.  You would place the SubReport in a table cell.</p>\n'}, {'answer_id': 20914, 'author': 'Carlton Jenke', 'author_id': 1215, 'author_profile': 'https://Stackoverflow.com/users/1215', 'pm_score': 0, 'selected': False, 'text': '<p>Simplest method is this:</p>\n\n<pre><code>select *,\n (select count(*) from tbl2 t2 where t2.tbl1ID = t1.tbl1ID) as cnt\nfrom tbl1 t1\n</code></pre>\n\n<p>here is a workable version (using table variables):</p>\n\n<pre><code>declare @tbl1 table\n(\n tbl1ID int,\n prop1 varchar(1),\n prop2 varchar(2)\n)\n\ndeclare @tbl2 table\n(\n tbl2ID int,\n tbl1ID int\n)\n\nselect *,\n (select count(*) from @tbl2 t2 where t2.tbl1ID = t1.tbl1ID) as cnt\nfrom @tbl1 t1\n</code></pre>\n\n<p>Obviously this is just a raw example - standard rules apply like don\'t select *, etc ...</p>\n\n<hr>\n\n<p><strong>UPDATE from Aug 21 \'08 at 21:27:</strong><br>\n@AlexCuse - Yes, totally agree on the performance.</p>\n\n<p>I started to write it with the outer join, but then saw in his sample output the count and thought that was what he wanted, and the count would not return correctly if the tables are outer joined. Not to mention that joins can cause your records to be multiplied (1 entry from tbl1 that matches 2 entries in tbl2 = 2 returns) which can be unintended.</p>\n\n<p>So I guess it really boils down to the specifics on what your query needs to return.</p>\n\n<hr>\n\n<p><strong>UPDATE from Aug 21 \'08 at 22:07:</strong><br>\nTo answer the other parts of your question - is a VB function the way to go? No. Absolutely not. Not for something this simple.</p>\n\n<p>Functions are very bad on performance, each row in the return set executes the function.</p>\n\n<p>If you want to "compartmentalize" the different parts of the query you have to approach it more like a stored procedure. Build a temp table, do part of the query and insert the results into the table, then do any further queries you need and update the original temp table (or insert into more temp tables).</p>\n'}, {'answer_id': 21037, 'author': 'AlexCuse', 'author_id': 794, 'author_profile': 'https://Stackoverflow.com/users/794', 'pm_score': 0, 'selected': False, 'text': '<p>Depending on how you want the output to look, a subreport could do, or you could group on ID, property1, property2 and show the items from your other table as detail items (assuming you want to show more than just count).</p>\n\n<p>Something like </p>\n\n<pre><code>select t1.ID, t1.property1, t1.property2, t2.somecol, t2.someothercol\nfrom table t1 left join anothertable t2 on t1.ID = t2.ID\n</code></pre>\n\n<p>@Carlton Jenke I think you will find an outer join a better performer than the correlated subquery in the example you gave.  Remember that the subquery needs to be run for each row.</p>\n'}]
['https://Stackoverflow.com/questions/20876', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/109/']