<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Use this SQL with Sybase in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839386#M4870229</link>
    <description>&lt;P&gt;Is there any kind of optimization included, i.e. do you try to use as less drums as possible? (I won't try to find this out by studying your CTEs...)&lt;/P&gt;
&lt;P&gt;From your sample, I would conclude it's much simpler and you simply add the rows in sequential order, until one drum is full. BTW: Is there an imposed order like a primary key?&lt;/P&gt;
&lt;P&gt;If so, a different approach might be to use a WINDOW function to build adjacent sums and group them to fit within the according limit. (I don't have an actual sample - it's just a guess that this could work...)&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;FWIW, here's a link to the SQL Anywhere SCN community with a &lt;STRONG&gt;blog article on WINDOW functions and an according whitepaper&lt;/STRONG&gt; - AFAIK the WINDOW clause in SQL Anywhere is much more powerful/feature-rich than with current MS SQL versions...:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://scn.sap.com/community/sql-anywhere/blog/2014/01/29/the-window-operator-and-window-aggregate-functions"&gt;From the Archives: The Window Operator and Window Aggregate Functions&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 27 Apr 2014 16:57:32 GMT</pubDate>
    <dc:creator>VolkerBarth</dc:creator>
    <dc:date>2014-04-27T16:57:32Z</dc:date>
    <item>
      <title>Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaq-p/13839372</link>
      <description>&lt;P&gt;I have a good working sql that works in sql server. The logical computation calculates how many lengths will fit a barrel. Maximum capacity per drum is 14300 meters. My question is how I can get this to work with Sybase.&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE&gt;Declare @tblLength table (num int, qty int, unit varchar(20))

insert @tblLength
values
(2,2500,''),
(1,3800,''),
(1,4200,''),
(1,6500,''),
(1,8300,'')

;with cte as
(
select i=1, qty, unit from @tblLength where num &amp;gt;= 1
union all
select i=i+1, t.qty, t.unit from @tblLength t, cte where t.qty=cte.qty and t.unit = cte.unit and t.num &amp;gt;= i+1
),
cte2 as
(
select *, id=row_number() over (order by qty desc) from cte
),
cte3 as
(
select j=1, qty, unit, id, tot=qty, drum=1 from cte2 where id = 1
union all
select j=j+1, cte2.qty, cte2.unit, cte2.id,
tot=case when cte3.tot+cte2.qty &amp;gt; 14300 then cte2.qty else cte3.tot+cte2.qty end,
drum=case when cte3.tot+cte2.qty &amp;gt; 14300 then cte3.drum + 1 else cte3.drum end
from cte2,cte3 where cte2.id = j+1
)
select drum, qty, unit from cte3&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Apr 2014 04:20:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaq-p/13839372</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-20T04:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839377#M4870220</link>
      <description>&lt;P&gt;Please tell us what the third view cte3 is trying to accomplish ( because it's the one that's causing me grief &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;...or wait until Ivan Bowman sees this question &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Changing the declare (and adding a semicolon), plus adding the RECURSIVE keyword, makes cte and cte2 work (or at least, they stop throwing error messages)...&lt;/P&gt;
&lt;PRE&gt;Declare LOCAL TEMPORARY TABLE @tblLength (num int, qty int, unit varchar(20));

insert @tblLength
values
(2,2500,''),
(1,3800,''),
(1,4200,''),
(1,6500,''),
(1,8300,'')

;with RECURSIVE cte ( i, qty, unit ) as
(
select i=1, qty, unit from @tblLength where num &amp;gt;= 1
union all
select i=i+1, t.qty, t.unit from @tblLength t, cte where t.qty=cte.qty and t.unit = cte.unit and t.num &amp;gt;= i+1
),
cte2 as
(
select *, id=row_number() over (order by qty desc) from cte
)
select * from cte2;

i,qty,unit,id
1,8300,'',1
1,6500,'',2
1,4200,'',3
1,3800,'',4
1,2500,'',5
&lt;/PRE&gt;

&lt;P&gt;However, the full query fails with the hopelessly vague "Invalid recursive query" error...&lt;/P&gt;
&lt;PRE&gt;Declare LOCAL TEMPORARY TABLE @tblLength (num int, qty int, unit varchar(20));

insert @tblLength
values
(2,2500,''),
(1,3800,''),
(1,4200,''),
(1,6500,''),
(1,8300,'')

;with RECURSIVE cte ( i, qty, unit ) as
(
select i=1, qty, unit from @tblLength where num &amp;gt;= 1
union all
select i=i+1, t.qty, t.unit from @tblLength t, cte where t.qty=cte.qty and t.unit = cte.unit and t.num &amp;gt;= i+1
),
cte2 as
(
select *, id=row_number() over (order by qty desc) from cte
),
cte3 ( j, qty, unit, id, tot, drum ) as
(
select j=1, qty, unit, id, tot=qty, drum=1 from cte2 where id = 1
union all
select j=j+1, cte2.qty, cte2.unit, cte2.id,
tot=case when cte3.tot+cte2.qty &amp;gt; 14300 then cte2.qty else cte3.tot+cte2.qty end,
drum=case when cte3.tot+cte2.qty &amp;gt; 14300 then cte3.drum + 1 else cte3.drum end
from cte2,cte3 where cte2.id = j+1
)
select drum, qty, unit from cte3;

Could not execute statement.
Invalid recursive query
SQLCODE=-921, ODBC 3 State="42000"
&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Apr 2014 09:03:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839377#M4870220</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2014-04-20T09:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839378#M4870221</link>
      <description>&lt;P&gt;The SQL should do this. Maybe there is another (smarter) way to do this in sybase?&lt;/P&gt;
&lt;P&gt;I have several lengths to be wound on drums (lengths may vary) &lt;/P&gt;
&lt;P&gt;Ex: Table tblLength &lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE&gt;Count----qty----Unit
---------------------
2--------2500---meter
1--------3800---meter
1--------4200---meter
1--------6500---meter
1--------8300---meter


&lt;P&gt;On every drum I only have a max capacity for 14,300 meters.&lt;/P&gt;
&lt;P&gt;How can I use a smart SQL to calculate it like this:&lt;/P&gt;
&lt;P&gt;To wrap on every drum:&lt;/P&gt;
&lt;/CODE&gt;&lt;PRE class="codehilite"&gt;&lt;CODE&gt;&lt;CODE&gt;1 x 13000 (2x2500 + 1x3800 + 1x4200)
1 x 6500 (1x6500)
1 x 8300 (1x8300)&lt;/CODE&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Apr 2014 13:35:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839378#M4870221</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-20T13:35:32Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839379#M4870222</link>
      <description>&lt;P&gt;Cte3 takes the first row and allocates it to drum 1. It then goes through the following rows allocating to the same drum until it's full then starts on the next. Keeps going until it runs out of lengths.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 14:38:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839379#M4870222</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-20T14:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839380#M4870223</link>
      <description>&lt;P&gt;Can we assume when you say "how I can get this to work with Sybase" that you mean SQL Anywhere and not Adaptive Server Enterprise or another Sybase RDBMS?  If it is SQL Anywhere, can you tell us what version of the product you are using?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 08:14:23 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839380#M4870223</guid>
      <dc:creator>regdomaratzki</dc:creator>
      <dc:date>2014-04-21T08:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839381#M4870224</link>
      <description>&lt;P&gt;Yes, I mean SQL Anywhere and we're talking about version 12...&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 08:23:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839381#M4870224</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-21T08:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839373#M4870216</link>
      <description>&lt;P&gt;I just went through the same exercise as Breck and I concur that the main issue arises with the inclusion of cte3.   &lt;/P&gt;
&lt;P&gt;I suspect this is due to the documented &lt;A href="http://dcx/index.html#sa160/en/dbusage/recursive-table-expr-sqlug.html"&gt;restriction&lt;/A&gt;:&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE&gt;"References to other recursive common table expressions cannot appear within 
 the definition of recursive common table expressions as recursive common table 
 expressions cannot be mutually recursive."


&lt;P&gt;In the case of this query, the definitions of cte and cte3 are both recursive (common table expressions), so that would seem to be the most likely candidate for the invalidness of cte3.&lt;/P&gt;
&lt;P&gt;I don't know what the best approach is here; one could possibly inject a layer like a stored procedure or temporary stored procedure to isolate out the references ... but that seems inefficient.&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Apr 2014 13:23:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839373#M4870216</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-21T13:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839382#M4870225</link>
      <description>&lt;P&gt;Ok. 
Can I do this SQL in a different way but with the same functionality?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 13:27:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839382#M4870225</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-21T13:27:41Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839374#M4870217</link>
      <description>&lt;P&gt;My simplistic attempt to split this into 2 parts with the use of a stored procedure fails.  I suspect that may be because of inlining ...&lt;/P&gt;
&lt;P&gt;Maybe some other readers have a better approach.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 14:33:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839374#M4870217</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-21T14:33:15Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839383#M4870226</link>
      <description>&lt;P&gt;It is best to try to do it without SP. But I can not do it, unfortunately. And it is perhaps as you say, it may not be possible without splitting...&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 15:15:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839383#M4870226</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-21T15:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839384#M4870227</link>
      <description>&lt;P&gt;Sadly, I'd love to help, but I still don't have a clue about what the query is trying to accomplish... in spite of your valiant attempts to explain it. Perhaps if you explained it as if you were speaking to a kindergarten class...&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="alt text" src="http://www.dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/20000/0000/900/20912/20912.strip.gif" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 16:48:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839384#M4870227</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2014-04-21T16:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839375#M4870218</link>
      <description>&lt;P&gt;For those who are algorithmically inclined ...
This appears to me as being the SQL approximation of the one-dimensional "First Fit" 
heuristic algorithm. &lt;EM&gt;{a heuristic for a family of N-P complete problems related to the "Bin Packing problem"}&lt;/EM&gt; As such, without some sort or recursion or look ahead ... it may take some time to find a simplified algorithm to make this happen.&lt;/P&gt;
&lt;P&gt;The original solution is a creative utilization of SQL Server features.  A similarly crafty SQL Anywhere solution may take a bit of effort.  &lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 16:49:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839375#M4870218</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-21T16:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839385#M4870228</link>
      <description>&lt;P&gt;I would be extremely grateful if someone could help me with this then I stalled. Needed SP so it's ok too.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Apr 2014 13:23:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839385#M4870228</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-27T13:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839386#M4870229</link>
      <description>&lt;P&gt;Is there any kind of optimization included, i.e. do you try to use as less drums as possible? (I won't try to find this out by studying your CTEs...)&lt;/P&gt;
&lt;P&gt;From your sample, I would conclude it's much simpler and you simply add the rows in sequential order, until one drum is full. BTW: Is there an imposed order like a primary key?&lt;/P&gt;
&lt;P&gt;If so, a different approach might be to use a WINDOW function to build adjacent sums and group them to fit within the according limit. (I don't have an actual sample - it's just a guess that this could work...)&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;FWIW, here's a link to the SQL Anywhere SCN community with a &lt;STRONG&gt;blog article on WINDOW functions and an according whitepaper&lt;/STRONG&gt; - AFAIK the WINDOW clause in SQL Anywhere is much more powerful/feature-rich than with current MS SQL versions...:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://scn.sap.com/community/sql-anywhere/blog/2014/01/29/the-window-operator-and-window-aggregate-functions"&gt;From the Archives: The Window Operator and Window Aggregate Functions&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Apr 2014 16:57:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839386#M4870229</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-04-27T16:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839387#M4870230</link>
      <description>&lt;P&gt;Someone...?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2014 14:22:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839387#M4870230</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-29T14:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839388#M4870231</link>
      <description>&lt;P&gt;Please answer my yesterday's comment on the question...:)&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2014 15:18:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839388#M4870231</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-04-29T15:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839389#M4870232</link>
      <description>&lt;P&gt;I can not see your comment somewhere? Where can I see it?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2014 15:32:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839389#M4870232</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-29T15:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839390#M4870233</link>
      <description>&lt;P&gt;Below the question, there are currently 6 nested comments. Click on "Show all" if not all are displayed...&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2014 15:37:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839390#M4870233</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-04-29T15:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839391#M4870234</link>
      <description>&lt;P&gt;I'd say this isn't a case for pure declarative SQL. Even if it is possible to implement it using recursive statements nobody will understand what's going on there. Use a procedure or implement it in the host language.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2014 17:59:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839391#M4870234</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-04-29T17:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: Use this SQL with Sybase</title>
      <link>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839392#M4870235</link>
      <description>&lt;P&gt;&amp;gt; nobody will understand what's going on there&lt;/P&gt;
&lt;P&gt;Always a danger... whenever I code recursive unions the "nobody will understand" includes ME, five minutes later &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2014 19:09:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/use-this-sql-with-sybase/qaa-p/13839392#M4870235</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2014-04-29T19:09:58Z</dc:date>
    </item>
  </channel>
</rss>

