Skip to Content
author's profile photo Former Member
Former Member

How does COUNT work/defined

It seems that COUNT takes a complete table column of values and returns the number of elements. I need to craft a sql procedure, which, takes a table column of values, which returns a VARCHAR. The issue i'm running into is with the definition of the function. It seems, that in order to take a table as input, the function must also return a table.

consider table:


first last uri

----- -------------- ------------------

andy smith

andy smith

andy smith

fred jones pkg.something.core

fred jones

testing count on a cartesian table with procedure:

drop procedure test_counter;

create procedure test_counter() language SQLSCRIPT as


declare cnt,sze integer;

declare uri_size,uri_cnt INTEGER;

declare str VARCHAR(5000);

declare str_num NVARCHAR(1023);

declare uri_1,uri_2,uri_lnk VARCHAR(200);

declare lt_lnk_uri TT_STRINGS;

lt_first = select distinct("FIRST") as "FIRST_1" from test_count;

lt_last = select distinct("LAST") as "LAST_1" from test_count;

lt_cartesian = select first_1,last_1 from :lt_first, :lt_last;

select first_1,last_1,count(pkg) from test_count,:lt_cartesian

where "FIRST" = first_1 and "LAST" = last_1 group by grouping sets ((first_1,last_1));


call test_counter();

correctly returns distinct first,last combinations with their counts as I wanted.

first_1 last_1 count(pkg)

----- ---------- -------------------------

1 andy smith 3

2 fred jones 2

i need something like: select first,last,makestr(mytable.pkg) from myschema.mytable;

this would ideally return:

first last str

----- -------------- -----------------------------------------------

1 andy smith,,

2 fred jones pkg.something.core,

How can I define the function signature to do this?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Aug 16, 2016 at 01:56 PM

    AH, just found STRING_AGG (using sps12)

    works perfectly, similar to COUNT(pkg), I simply use: STRING_AGG(pkg,',');

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 16, 2016 at 01:50 PM


    you can add a group by clause

    select first, last, count(*)

    from myTable

    group by first, last

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.