<?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: List function with sum? in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaa-p/13835047#M4865890</link>
    <description>&lt;P&gt;Did your head explode as you tried to make the subquery work with LIST and SUM together? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;One way to do this is "divide and conquer": do the SUM separately from the LIST.&lt;/P&gt;
&lt;P&gt;One way to divide and conquer is to do the SUM first, inside a derived table in the FROM clause, and then the LIST in the outer query.&lt;/P&gt;
&lt;P&gt;Another valuable technique is to build a complex STRING() call inside the LIST() call, using CR-LF as the line separator... LIST() is one of the most powerful and most useful features in SQL Anywhere, a wonderful invention indeed! (fast, too... entire commercial web pages have been constructed with LIST).&lt;/P&gt;
&lt;HR /&gt;

&lt;P&gt;It was a bit difficult to reverse-engineer the table layouts from the question, but here goes (let me know if it doesn't match your needs at all)...&lt;/P&gt;
&lt;PRE&gt;BEGIN
   DROP TABLE tblBill;
   EXCEPTION WHEN OTHERS THEN
END;

BEGIN
   DROP TABLE tblBatch;
   EXCEPTION WHEN OTHERS THEN
END;

CREATE TABLE tblBill(
   kfr_trans_id INTEGER NOT NULL PRIMARY KEY );

CREATE TABLE tblBatch(
   tr_id        INTEGER       NOT NULL REFERENCES tblBill ( kfr_trans_id ),
   line_id      INTEGER       NOT NULL,
   batchnr      VARCHAR ( 3 ) NOT NULL,
   quant        INTEGER       NOT NULL,
   PRIMARY KEY ( tr_id, line_id ) );

INSERT tblBill VALUES ( 10 );

INSERT tblBatch VALUES ( 10, 100, 'B12', 2 );
INSERT tblBatch VALUES ( 10, 200, 'B12', 1 );

INSERT tblBatch VALUES ( 10, 300, 'B13', 1 );
INSERT tblBatch VALUES ( 10, 400, 'B13', 2 );
INSERT tblBatch VALUES ( 10, 500, 'B13', 1 );

INSERT tblBatch VALUES ( 10, 600, 'B14', 1 );

INSERT tblBatch VALUES ( 10, 700, 'B43', 1 );

COMMIT;

-- The original subquery displays duplicate tblBatch.batchnr values.

SELECT tblBill.kfr_trans_id,
       (Select List(tblBatch.batchnr) From tblBatch where tblBatch.tr_id = tblBill.kfr_trans_id ) As Batchnr
  FROM tblBill;

/*
kfr_trans_id   Batchnr
10             B12,B12,B13,B13,B13,B14,B43
*/

-- Adding DISTINCT gets rid of the duplicates.

SELECT tblBill.kfr_trans_id,
       (Select List( DISTINCT tblBatch.batchnr) From tblBatch where tblBatch.tr_id = tblBill.kfr_trans_id ) As Batchnr
  FROM tblBill;

/*
kfr_trans_id   Batchnr
10             B12,B13,B14,B43  -- as posted in the question
*/

-- Use JOIN with derived table, then LIST and STRING together.

SELECT tblBill.kfr_trans_id,
       LIST ( STRING ( tblBatch_sum.sum_quant,
                       ' ',
                       tblBatch_sum.batchnr ),
              '\\X0D\
'
              ORDER BY tblBatch_sum.batchnr 
            ) AS Batchnr
  FROM tblBill
       INNER JOIN ( SELECT tblBatch.tr_id,
                           tblBatch.batchnr,
                           SUM ( tblBatch.quant ) AS sum_quant
                      FROM tblBatch
                     GROUP BY tblBatch.tr_id,
                           tblBatch.batchnr
                  ) AS tblBatch_sum
       ON tblBatch_sum.tr_id = tblBill.kfr_trans_id 
 GROUP BY tblBill.kfr_trans_id 
 ORDER BY tblBill.kfr_trans_id;

/*
kfr_trans_id  Batchnr
10            3 B12\\x0d\
4 B13\\x0d\
1 B14\\x0d\
1 B43
*/

-- In ISQL, check Tools - Options... - SQL Anywhere - Style - Text

/*
kfr_trans_id Batchnr 
------------ -------
          10 3 B12
4 B13
1 B14
1 B43
(1 rows)
Execution time: 0.031 seconds
*/
&lt;/PRE&gt;</description>
    <pubDate>Sun, 02 Mar 2014 16:09:08 GMT</pubDate>
    <dc:creator>Breck_Carter</dc:creator>
    <dc:date>2014-03-02T16:09:08Z</dc:date>
    <item>
      <title>List function with sum?</title>
      <link>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaq-p/13835046</link>
      <description>&lt;P&gt;I have a subquery that retunrns multiple lines. To solve it, I have used the List function. The separation of List function is comma. &lt;/P&gt;
&lt;P&gt;TblBill is in the main query with many other tables.&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Select&lt;/SPAN&gt; &lt;SPAN class="n"&gt;List&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;tblBatch&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;batchnr&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt; &lt;SPAN class="n"&gt;From&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tblBatch&lt;/SPAN&gt; &lt;SPAN class="n"&gt;where&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tblBatch&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;tr_id&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tblBill&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;kfr_trans_id&lt;/SPAN&gt; &lt;SPAN class="p"&gt;)&lt;/SPAN&gt; &lt;SPAN class="n"&gt;As&lt;/SPAN&gt; &lt;SPAN class="n"&gt;Batchnr&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;This subquery returns: &lt;CODE&gt;B12, B13, B14, B43&lt;/CODE&gt; and so on...&lt;/P&gt;
&lt;P&gt;Now I want to use SUM from another field (quant) from table tblBatch in the subquery and then separate those with a line break instead of comma.
Batch number may occur several times and I want to summarize these&lt;/P&gt;
&lt;P&gt;I want the subquery return like this:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="o"&gt;---&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Batchnr&lt;/SPAN&gt;&lt;SPAN class="o"&gt;---&lt;/SPAN&gt;
&lt;SPAN class="mi"&gt;3&lt;/SPAN&gt; &lt;SPAN class="n"&gt;B12&lt;/SPAN&gt; 
&lt;SPAN class="mi"&gt;4&lt;/SPAN&gt; &lt;SPAN class="n"&gt;B13&lt;/SPAN&gt;
&lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="n"&gt;B14&lt;/SPAN&gt; 
&lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="n"&gt;B43&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;and so on..&lt;/P&gt;
&lt;P&gt;The number before batchnr is the sum from quant field.&lt;/P&gt;
&lt;P&gt;How do I solve it the best way?&lt;/P&gt;</description>
      <pubDate>Sun, 02 Mar 2014 12:47:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaq-p/13835046</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-03-02T12:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: List function with sum?</title>
      <link>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaa-p/13835047#M4865890</link>
      <description>&lt;P&gt;Did your head explode as you tried to make the subquery work with LIST and SUM together? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;One way to do this is "divide and conquer": do the SUM separately from the LIST.&lt;/P&gt;
&lt;P&gt;One way to divide and conquer is to do the SUM first, inside a derived table in the FROM clause, and then the LIST in the outer query.&lt;/P&gt;
&lt;P&gt;Another valuable technique is to build a complex STRING() call inside the LIST() call, using CR-LF as the line separator... LIST() is one of the most powerful and most useful features in SQL Anywhere, a wonderful invention indeed! (fast, too... entire commercial web pages have been constructed with LIST).&lt;/P&gt;
&lt;HR /&gt;

&lt;P&gt;It was a bit difficult to reverse-engineer the table layouts from the question, but here goes (let me know if it doesn't match your needs at all)...&lt;/P&gt;
&lt;PRE&gt;BEGIN
   DROP TABLE tblBill;
   EXCEPTION WHEN OTHERS THEN
END;

BEGIN
   DROP TABLE tblBatch;
   EXCEPTION WHEN OTHERS THEN
END;

CREATE TABLE tblBill(
   kfr_trans_id INTEGER NOT NULL PRIMARY KEY );

CREATE TABLE tblBatch(
   tr_id        INTEGER       NOT NULL REFERENCES tblBill ( kfr_trans_id ),
   line_id      INTEGER       NOT NULL,
   batchnr      VARCHAR ( 3 ) NOT NULL,
   quant        INTEGER       NOT NULL,
   PRIMARY KEY ( tr_id, line_id ) );

INSERT tblBill VALUES ( 10 );

INSERT tblBatch VALUES ( 10, 100, 'B12', 2 );
INSERT tblBatch VALUES ( 10, 200, 'B12', 1 );

INSERT tblBatch VALUES ( 10, 300, 'B13', 1 );
INSERT tblBatch VALUES ( 10, 400, 'B13', 2 );
INSERT tblBatch VALUES ( 10, 500, 'B13', 1 );

INSERT tblBatch VALUES ( 10, 600, 'B14', 1 );

INSERT tblBatch VALUES ( 10, 700, 'B43', 1 );

COMMIT;

-- The original subquery displays duplicate tblBatch.batchnr values.

SELECT tblBill.kfr_trans_id,
       (Select List(tblBatch.batchnr) From tblBatch where tblBatch.tr_id = tblBill.kfr_trans_id ) As Batchnr
  FROM tblBill;

/*
kfr_trans_id   Batchnr
10             B12,B12,B13,B13,B13,B14,B43
*/

-- Adding DISTINCT gets rid of the duplicates.

SELECT tblBill.kfr_trans_id,
       (Select List( DISTINCT tblBatch.batchnr) From tblBatch where tblBatch.tr_id = tblBill.kfr_trans_id ) As Batchnr
  FROM tblBill;

/*
kfr_trans_id   Batchnr
10             B12,B13,B14,B43  -- as posted in the question
*/

-- Use JOIN with derived table, then LIST and STRING together.

SELECT tblBill.kfr_trans_id,
       LIST ( STRING ( tblBatch_sum.sum_quant,
                       ' ',
                       tblBatch_sum.batchnr ),
              '\\X0D\
'
              ORDER BY tblBatch_sum.batchnr 
            ) AS Batchnr
  FROM tblBill
       INNER JOIN ( SELECT tblBatch.tr_id,
                           tblBatch.batchnr,
                           SUM ( tblBatch.quant ) AS sum_quant
                      FROM tblBatch
                     GROUP BY tblBatch.tr_id,
                           tblBatch.batchnr
                  ) AS tblBatch_sum
       ON tblBatch_sum.tr_id = tblBill.kfr_trans_id 
 GROUP BY tblBill.kfr_trans_id 
 ORDER BY tblBill.kfr_trans_id;

/*
kfr_trans_id  Batchnr
10            3 B12\\x0d\
4 B13\\x0d\
1 B14\\x0d\
1 B43
*/

-- In ISQL, check Tools - Options... - SQL Anywhere - Style - Text

/*
kfr_trans_id Batchnr 
------------ -------
          10 3 B12
4 B13
1 B14
1 B43
(1 rows)
Execution time: 0.031 seconds
*/
&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Mar 2014 16:09:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaa-p/13835047#M4865890</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2014-03-02T16:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: List function with sum?</title>
      <link>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaa-p/13835048#M4865891</link>
      <description>&lt;P&gt;Thanks! &lt;/P&gt;
&lt;P&gt;This works...but I wanna use a subquery and it allowed only one Select list item? How do I do then?&lt;/P&gt;</description>
      <pubDate>Mon, 03 Mar 2014 02:37:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/list-function-with-sum/qaa-p/13835048#M4865891</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-03-03T02:37:48Z</dc:date>
    </item>
  </channel>
</rss>

