{"id":157,"date":"2007-11-09T15:05:00","date_gmt":"2007-11-09T15:05:00","guid":{"rendered":"\/lisa\/post\/2007\/11\/09\/SQL-for-Sideways-Layout-Reports.aspx"},"modified":"2007-11-09T15:05:00","modified_gmt":"2007-11-09T15:05:00","slug":"sql-for-sideways-layout-reports","status":"publish","type":"post","link":"https:\/\/spacefold.com\/lisa\/2007\/11\/09\/sql-for-sideways-layout-reports\/","title":{"rendered":"SQL for Sideways-Layout Reports"},"content":{"rendered":"<p>\n(I am supposed to be blogging about <a href=\"\/lisa\/2007\/10\/17\/What-LSN-was-Not-Allowed-to-Do-What-LSN-Plans-to-Do-About-It-Now-and-What-You-and-I-Will-Never-Call-It\/\" target=\"_blank\" title=\"VFP-TMM the origin story\" rel=\"noopener\">TMM<\/a> and nothing else this month&#8230; so this will be VERY hastily done and I apologize in advance for any screwed up formatting.)\n<\/p>\n<p>\nThere is a classic report layout problem involving showing rows-as-columns that you might have heard described as the &quot;snaking columns&quot; problem, or the &quot;label orientation&quot; problem, or the &quot;image proofsheet&quot; problem. As I&#39;ve <a href=\"\/lisa\/?s=matrix\" title=\"Matrix Rebuilt blog post\">mentioned before<\/a>, I find it difficult to get the SQL Server RS matrix to fit these cases and I tend to rebuild the data to fit how it needs to be shown rather than use the matrix data region in some instances.\n<\/p>\n<p>\nI posted a sample&nbsp;solution using&nbsp;completely in-RDL techniques, without manipulating the data, <a href=\"http:\/\/forums.microsoft.com\/MSDN\/ShowPost.aspx?PostID=1884918&amp;SiteID=1\" target=\"_blank\" title=\"'Sideways' RS-forum thread\" rel=\"noopener\">in an RS forum thread<\/a>&nbsp; which discussed this problem as &quot;the sideways report&quot;. While I flatter myself that it&#39;s a really cool idea, in some situations you&#39;re better off handling the issue in SQL, and I want to illustrate that method here, in response to another <a href=\"http:\/\/forums.microsoft.com\/MSDN\/ShowPost.aspx?PostID=2389825&amp;SiteID=1\" target=\"_blank\" title=\"RS Forum thread on showing images\" rel=\"noopener\">forum query on the same subject<\/a>.\n<\/p>\n<p>\nIf you have had to design a layout where data rows need to show up as columns, you probably know what I&#39;m talking about and have your own variant of the scenario.&nbsp; If you don&#39;t, when I get more time, I will come back and edit this post with more detail.&nbsp; I will also try to come back and show how to extrapolate from this simple example to write the query dynamically, using <a href=\"\/lisa\/2007\/11\/01\/Writing-Dynamic-SQL-in-and-for-RDLs\/\" title=\"Dynamic SQL blog post\">some techniques that I&#39;ve shown here<\/a> in the past.&nbsp; I didn&#39;t happen to post any examples in that post where JOINs or UNIONs are dynamically put into place based on a parameter, which is what is required in this case to build the SELECT string dynamically, but I do that in a number of reports and it&#39;s really just an extension of what I&#39;ve already written in that earlier post.\n<\/p>\n<p>\nSo for now, I&#39;ll just illustrate using concrete SQL examples, where the number across is shown as 3.&nbsp;&nbsp;To change the number 3 to a different number across you can either write dynamic SQL and do a bit of column-visibility expression work,&nbsp;or have separate reports, each using a different variant of this query appropriate to a different number of cells across.&nbsp; OK?\n<\/p>\n<p>\nI&#39;ll also, for now, use syntax that is SQL Server 2005 specific: the ROW_NUMBER() function.&nbsp; Realize that there are ways of doing this in pre-2005 SQL Server, Oracle, etc, and I have posted some examples of this on forums in the past too.&nbsp; When I have more time, if anybody queries this, I can add some additional code here.\n<\/p>\n<p class=\"NB\">\nThere may also be a cool way to do this using CTEs and recursions, and maybe somebody will post here to scold me for not using it &lt;g&gt;.&nbsp; I can only say: I can&#39;t think that through nearly as fast, and it will just be a variation on the same theme.&nbsp; So perhaps we&#39;ll refine this post to do it better, even in SQL Server 2005-specific code, later as well.\n<\/p>\n<p>\nReady?\n<\/p>\n<h3>1. Start with a basic query.<\/h3>\n<p>\nI will&nbsp;use a table in the MySQL tutorial database for this example, because it&#39;s handy.&nbsp; It&#39;s a table of cities.&nbsp;&nbsp;In the&nbsp;example, we&nbsp;want to display the names of cities in an ersatz matrix format. Substitute whatever column you want.&nbsp; In this example you&#39;ll see that I&#39;ve referenced the city as ImageNo in a bunch of places &#8212; because the person who asked the question on the recent thread happens to be displaying images, not city names.\n<\/p>\n<p><font color=\"#0000ff\"><font color=\"#0000ff\"><\/p>\n<p class=\"code\">\nSELECT<font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> City <\/font><font color=\"#0000ff\">ORDER<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">BY<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font>\n<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"codecomment\">\n<font face=\"courier new,courier\">&#8212; result, 4079 rows&nbsp;:<br \/>\n[San Crist&oacute;bal de] la Laguna&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n&acute;s-Hertogenbosch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nA Coru&ntilde;a (La Coru&ntilde;a)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nAachen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n:<br \/>\nZwolle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nZytomyr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/font>\n<\/p>\n<p>\nAs our starting conditions, we don&#39;t know how many cities there are in total, but we know we want 3 columns for this instance of the report.\n<\/p>\n<h3>2. How many rows?<\/h3>\n<p>\nWe&nbsp;need to know what the row number is for each row, for later use.\n<\/p>\n<p><font color=\"#0000ff\"><\/p>\n<p class=\"code\">\nSELECT <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">,<\/font> <font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font> <font color=\"#0000ff\">OVER<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font> <font color=\"#0000ff\">BY<\/font> <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">FROM<\/font> City\n<\/p>\n<p><\/font><\/p>\n<p class=\"codecomment\">\n<font face=\"courier new,courier\">&#8212; result:<br \/>\n[San Crist&oacute;bal de] la Laguna&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;1<br \/>\n&acute;s-Hertogenbosch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;2<br \/>\nA Coru&ntilde;a (La Coru&ntilde;a)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;3<br \/>\n:<br \/>\nZwolle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;4078<br \/>\nZytomyr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;4079 <\/font>\n<\/p>\n<p>\nWe also need to have a third as many rows as we have cities (rounding up).&nbsp;\n<\/p>\n<p><font color=\"#0000ff\"><\/p>\n<p class=\"code\">\nSELECT RowNo <font color=\"#0000ff\">FROM<\/font> <br \/>\n<font color=\"#808080\">(<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font> <br \/>\n&nbsp;&nbsp; <font color=\"#0000ff\">OVER<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font> <font color=\"#0000ff\">BY<\/font> <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">AS<\/font> RowNo <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">)<\/font> A<br \/>\n<font color=\"#0000ff\">WHERE<\/font> <font color=\"#808080\">(<\/font>RowNo <font color=\"#808080\">&lt;=<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">COUNT<\/font><font color=\"#808080\">(*)<\/font> <font color=\"#0000ff\">AS<\/font> <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font>10<font color=\"#808080\">,<\/font>2<font color=\"#808080\">))\/<\/font>3<font color=\"#808080\">)<\/font> <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">))<br \/>\n<\/font>\n<\/p>\n<p><\/font><\/p>\n<p class=\"codecomment\">\n&#8212; result: 1 through 1360\n<\/p>\n<p>\nNotice that we have to&nbsp;CAST&nbsp;one of the divisions before the CEILING() function is applied; otherwise, given SQL Server&#39;s rule of least precision in an operation,&nbsp;we are going to lose a row for the&nbsp;&quot;remainder&quot;&nbsp;cities on a final, non-full, row.\n<\/p>\n<h3>3. What cities belong in the first column?<\/h3>\n<p>\nWe need to determine which cities show in each column, which we can do with a Modulus operation.&nbsp; Here&#39;s an example, for the first column:\n<\/p>\n<p><font color=\"#0000ff\"><font color=\"#0000ff\"><\/p>\n<p class=\"code\">\nSELECT<font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">AS<\/font><font color=\"#000000\"> Col1 <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> <br \/>\n<\/font><font color=\"#808080\">&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#808080\">,<\/font><font color=\"#000000\"> <\/font><font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">OVER<\/font><font color=\"#000000\"> <\/font><font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">BY<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">AS<\/font><font color=\"#000000\"> ImageNo <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> City<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> X1<br \/>\n<\/font><font color=\"#0000ff\">&nbsp;&nbsp;&nbsp; WHERE<\/font> <font color=\"#808080\">(<\/font>ImageNo <font color=\"#808080\">%<\/font> 3 <font color=\"#808080\">=<\/font> 1<font color=\"#808080\">)<\/font>\n<\/p>\n<p><\/font><\/p>\n<p class=\"codecomment\">\n<font face=\"courier new,courier\">&#8212; result, 1360 rows: <br \/>\n[San Crist&oacute;bal de] la Laguna&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nAachen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nAbadan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n:<br \/>\nZunyi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nZwolle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/font>\n<\/p>\n<p><\/font><\/p>\n<p>\nObviously, we can repeat this for the other two columns, changing the WHERE clause to show a different result&nbsp;in the Modulus operation; column 2 cities have a MOD result of 2, and column 3 cities have a MOD result of 0.&nbsp;&nbsp;(When you do this dynamically, the last column is always the one with a MOD result of 0).\n<\/p>\n<h3>4. Put it together<\/h3>\n<p>\nWe can join each row with the correct column 1 city to display as follows.&nbsp;Notice, again, that we will CAST the divisions.&nbsp; Our WHERE clause is still here, limiting the number of rows to a third of the original total:\n<\/p>\n<p><font color=\"#0000ff\"><font color=\"#0000ff\"><font color=\"#0000ff\"><\/p>\n<p class=\"code\">\nSELECT<font color=\"#000000\"> RowNo<\/font><font color=\"#808080\">,<\/font><font color=\"#000000\"> Col1 <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> <br \/>\n<\/font><font color=\"#808080\">&nbsp;&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font><font color=\"#000000\"> <\/font><font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font><font color=\"#000000\"> <br \/>\n&nbsp;&nbsp; <\/font><font color=\"#0000ff\">OVER<\/font><font color=\"#000000\"> <\/font><font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">BY<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">AS<\/font><font color=\"#000000\"> RowNo <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> City<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> A<br \/>\n<\/font><font color=\"#808080\">LEFT<\/font><font color=\"#000000\"> <\/font><font color=\"#808080\">JOIN<br \/>\n&nbsp;&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">As<\/font><font color=\"#000000\"> Col1<\/font><font color=\"#808080\">,<\/font><font color=\"#000000\"> <\/font><font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font><font color=\"#000000\"> <br \/>\n&nbsp;&nbsp; <\/font><font color=\"#0000ff\">OVER<\/font><font color=\"#000000\"> <\/font><font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">BY<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">AS<\/font><font color=\"#000000\"> ImageNo <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> City<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> B<br \/>\n<\/font><font color=\"#0000ff\">&nbsp;&nbsp;&nbsp; ON<\/font><font color=\"#000000\"> B<\/font><font color=\"#808080\">.<\/font><font color=\"#000000\">ImageNo <\/font><font color=\"#808080\">%<\/font><font color=\"#000000\"> 3 <\/font><font color=\"#808080\">=<\/font><font color=\"#000000\"> 1 <\/font><font color=\"#808080\">AND<\/font><font color=\"#000000\"> <br \/>\n&nbsp;&nbsp; RowNo <\/font><font color=\"#808080\">=<\/font><font color=\"#000000\"> <\/font><font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font><font color=\"#000000\">B<\/font><font color=\"#808080\">.<\/font><font color=\"#000000\">ImageNo <\/font><font color=\"#0000ff\">AS<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font><font color=\"#000000\">10<\/font><font color=\"#808080\">,<\/font><font color=\"#000000\">2<\/font><font color=\"#808080\">))\/<\/font><font color=\"#000000\">3 <\/font><font color=\"#808080\">)<br \/>\n<\/font><font color=\"#0000ff\">WHERE<\/font><font color=\"#000000\"> <\/font><font color=\"#808080\">(<\/font><font color=\"#000000\">A<\/font><font color=\"#808080\">.<\/font><font color=\"#000000\">RowNo <\/font><font color=\"#808080\">&lt;=<\/font><font color=\"#000000\"> <br \/>\n&nbsp;&nbsp; <\/font><font color=\"#808080\">(<\/font><font color=\"#0000ff\">SELECT<\/font><font color=\"#000000\"> <\/font><font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">COUNT<\/font><font color=\"#808080\">(*)<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">AS<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font><font color=\"#000000\">10<\/font><font color=\"#808080\">,<\/font><font color=\"#000000\">2<\/font><font color=\"#808080\">))\/<\/font><font color=\"#000000\">3<\/font><font color=\"#808080\">)<\/font><font color=\"#000000\"> <\/font><font color=\"#0000ff\">FROM<\/font><font color=\"#000000\"> City<\/font><font color=\"#808080\">))<\/font>\n<\/p>\n<p><\/font><\/p>\n<p class=\"codecomment\">\n&#8211;result:<\/p>\n<p><font face=\"courier new,courier\">1&nbsp;[San Crist&oacute;bal de] la Laguna&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n2&nbsp;Aachen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n3&nbsp;Abadan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n:<br \/>\n1358&nbsp;Zonguldak&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n1359&nbsp;Zunyi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n1360&nbsp;Zwolle<\/font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n<\/p>\n<p><\/font><\/font><\/p>\n<p>\n&#8230; and of course we can add joins for each additional column to get the full set &#8212; notice the NULL in the final column of the final, un-full row; this is exactly what we want where the numbers don&#39;t come out perfectly:\n<\/p>\n<p><font color=\"#0000ff\"><\/p>\n<p class=\"code\">\nSELECT RowNo<font color=\"#808080\">,<\/font> Col1<font color=\"#808080\">,<\/font> Col2<font color=\"#808080\">,<\/font> Col3 <font color=\"#0000ff\">FROM<\/font> <br \/>\n<font color=\"#808080\">&nbsp;&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font> <font color=\"#0000ff\">OVER<\/font> <br \/>\n&nbsp;&nbsp; <font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font> <font color=\"#0000ff\">BY<\/font> <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">AS<\/font> RowNo <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">)<\/font> A<br \/>\n<font color=\"#808080\">LEFT<\/font> <font color=\"#808080\">JOIN<br \/>\n&nbsp;&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#0000ff\">Name<\/font> <font color=\"#0000ff\">As<\/font> Col1<font color=\"#808080\">,<\/font> <font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font> <br \/>\n&nbsp;&nbsp; <font color=\"#0000ff\">OVER<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font> <font color=\"#0000ff\">BY<\/font> <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">AS<\/font> ImageNo <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">)<\/font> B<br \/>\n<font color=\"#0000ff\">&nbsp;&nbsp; ON<\/font> B<font color=\"#808080\">.<\/font>ImageNo <font color=\"#808080\">%<\/font> 3 <font color=\"#808080\">=<\/font> 1 <font color=\"#808080\">AND<\/font> <br \/>\n&nbsp;&nbsp; RowNo <font color=\"#808080\">=<\/font> <font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font>B<font color=\"#808080\">.<\/font>ImageNo <font color=\"#0000ff\">AS<\/font> <font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font>10<font color=\"#808080\">,<\/font>2<font color=\"#808080\">))\/<\/font>3<font color=\"#808080\">)<br \/>\nLEFT<\/font> <font color=\"#808080\">JOIN<br \/>\n&nbsp;&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#0000ff\">Name<\/font> <font color=\"#0000ff\">As<\/font> Col2<font color=\"#808080\">,<\/font> <font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font> <br \/>\n&nbsp;&nbsp; <font color=\"#0000ff\">OVER<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font> <font color=\"#0000ff\">BY<\/font> <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">AS<\/font> ImageNo <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">)<\/font> C<br \/>\n<font color=\"#0000ff\">&nbsp;&nbsp; ON<\/font> C<font color=\"#808080\">.<\/font>ImageNo <font color=\"#808080\">%<\/font> 3 <font color=\"#808080\">=<\/font> 2 <font color=\"#808080\">AND<\/font> <br \/>\n&nbsp;&nbsp; RowNo <font color=\"#808080\">=<\/font> <font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font>C<font color=\"#808080\">.<\/font>ImageNo <font color=\"#0000ff\">AS<\/font> <font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font>10<font color=\"#808080\">,<\/font>2<font color=\"#808080\">))\/<\/font>3<font color=\"#808080\">)<br \/>\nLEFT<\/font> <font color=\"#808080\">JOIN<br \/>\n&nbsp;&nbsp; (<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#0000ff\">Name<\/font> <font color=\"#0000ff\">As<\/font> Col3<font color=\"#808080\">,<\/font> <font color=\"#ff00ff\">ROW_NUMBER<\/font><font color=\"#808080\">()<\/font> <br \/>\n&nbsp;&nbsp; <font color=\"#0000ff\">OVER<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">ORDER<\/font> <font color=\"#0000ff\">BY<\/font> <font color=\"#0000ff\">Name<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">AS<\/font> ImageNo <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">)<\/font> D<br \/>\n<font color=\"#0000ff\">&nbsp;&nbsp; ON<\/font> D<font color=\"#808080\">.<\/font>ImageNo <font color=\"#808080\">%<\/font> 3 <font color=\"#808080\">=<\/font> 0 <font color=\"#808080\">AND<\/font> <br \/>\n&nbsp;&nbsp; RowNo <font color=\"#808080\">=<\/font> <font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font>D<font color=\"#808080\">.<\/font>ImageNo <font color=\"#0000ff\">AS<\/font> <font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font>10<font color=\"#808080\">,<\/font>2<font color=\"#808080\">))\/<\/font>3<font color=\"#808080\">)<br \/>\n<\/font><font color=\"#0000ff\">WHERE<\/font> <font color=\"#808080\">(<\/font>A<font color=\"#808080\">.<\/font>RowNo <font color=\"#808080\">&lt;=<\/font> <font color=\"#808080\">(<\/font><font color=\"#0000ff\">SELECT<\/font> <font color=\"#ff00ff\">CEILING<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">CAST<\/font><font color=\"#808080\">(<\/font><font color=\"#ff00ff\">COUNT<\/font><font color=\"#808080\">(*) <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/font><font color=\"#0000ff\">AS<\/font> <font color=\"#0000ff\">Numeric<\/font><font color=\"#808080\">(<\/font>10<font color=\"#808080\">,<\/font>2<font color=\"#808080\">))\/<\/font>3 <font color=\"#808080\">)<\/font> <font color=\"#0000ff\">FROM<\/font> City<font color=\"#808080\">))<\/font>\n<\/p>\n<p><\/font><\/p>\n<p class=\"codecomment\">\n<font face=\"courier new,courier\" size=\"-2\">&#8212; result:RowNo&nbsp;Col1&nbsp;Col2&nbsp;Col3<br \/>\n1&nbsp;[San Crist&oacute;bal de] la Laguna&nbsp;&nbsp;&nbsp;&nbsp;&acute;s-Hertogenbosch&nbsp;&nbsp;A Coru&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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n1360&nbsp;Zwolle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zytomyr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;NULL <\/font>\n<\/p>\n<p>\nMaking this dynamic can be done, as usual, within T-SQL or VB code within a report and, as usual, there are better functions with which to perform the string manipulation in VB.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(I am supposed to be blogging about TMM and nothing else this month&#8230; so this will be VERY hastily done and I apologize in advance for any screwed up formatting.) There is a classic report layout problem involving showing rows-as-columns that you might have heard described as the &quot;snaking columns&quot; problem, or the &quot;label orientation&quot;<a class=\"more-link\" href=\"https:\/\/spacefold.com\/lisa\/2007\/11\/09\/sql-for-sideways-layout-reports\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[],"class_list":["post-157","post","type-post","status-publish","format-standard","hentry","category-reporting","category-sql-server"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}