Note that there are some explanatory texts on larger screens.

plurals
  1. POcounting entries, pooling over columns and returning boolean values
    primarykey
    data
    text
    <p>(Feel free to modify the title accordingly)</p> <p>Hi, I am using Postgres 9.3 on MacOSX. I am also new to any kind of SQL so bare with me. I am trying to create a query function (code and example below), which counts distinct entries (<code>individuals</code>) by pooling over certain columns by using <code>SELECT DISTINCT ON</code>. This works fine. The problem arrises when I in the same function add a <code>CASE</code> statement for checking if each rows of yet another column returns <code>NULL</code> or NOT.</p> <p>So, I have a table looking like this but with thousands of entries. <a href="http://i1326.photobucket.com/albums/u642/Awful-Dodger/ScreenShot2013-11-19at93404PM_zps12da3754.png" rel="nofollow">Here is a subset</a> where you can see data for a particular species and method. The geographical data are of different resolution, but here I am pooling over the columns <code>species, method, location, larger_region</code> (in example, pooling over <code>method</code> is redundant) See code and output below.</p> <pre><code>--CODE CREATE TABLE test_tbl (accn varchar(10) PRIMARY KEY, method TEXT, species VARCHAR(30), lat NUMERIC, lon NUMERIC, location TEXT, larger_region TEXT, coords BOOLEAN DEFAULT TRUE); FETCH FORWARD 1 FROM test_tbl; accn | method | species | lat | lon | location | larger_region | coords ---------+----------+-------------------+----------+----------+----------------------------+---------------+--------- EU336944 | Isolate | Dysidea granulosa | 10.07704 | 73.63238 | Lakshadweep Islands, India | Indian EEZ | t CREATE TABLE test_view_tbl ( species VARCHAR(30) NOT NULL, no_ind BIGINT NOT NULL, method TEXT NOT NULL, location TEXT NOT NULL, larger_region TEXT NOT NULL ); </code></pre> <p>Below function produces the correct output.</p> <pre><code>CREATE FUNCTION test_view() RETURNS SETOF test_view_tbl AS $$ SELECT DISTINCT ON (species, method, location, larger_region) species, count(species), method, location, larger_region FROM test_tbl GROUP BY species, method, location, larger_region ORDER BY species; $$ LANGUAGE sql SELECT * FROM test_view(); species | no_ind | method | location | larger_region ------------------+--------+---------+--------------------------------------+--------------- Dysidea granulosa | 6 | Isolate | Kavaratti Island, Lakshadweep, India | Indian EEZ Dysidea granulosa | 13 | Isolate | Lakshadweep Islands, India | Indian EEZ </code></pre> <p>Below I modify <code>test_view_tbl</code> by adding a <code>boolean</code> column <code>coords</code> and modify the code correspondingly. This checks if column <code>lat</code> is <code>NULL</code> or not, i.e. whether each entry has coordinates. However, this also screws up the above wanted result by NOT returning certain entries. </p> <pre><code>ALTER TABLE test_view_tbl ADD COLUMN coords BOOLEAN DEFAULT TRUE; CREATE FUNCTION test_view() RETURNS SETOF test_view_tbl AS $$ SELECT DISTINCT ON (species, method, location, larger_region) species, count(species), method, location, larger_region, CASE WHEN lat IS NULL THEN coords IS FALSE ELSE TRUE END FROM test_tbl GROUP BY species, method, location, larger_region, lat, coords ORDER BY species; $$ LANGUAGE sql SELECT * FROM test_view(); species | no_ind | method | location | larger_region | coords -------------------+--------+---------+--------------------------------------+---------------+-------- Dysidea granulosa | 6 | Isolate | Kavaratti Island, Lakshadweep, India | Indian EEZ | t Dysidea granulosa | 3 | Isolate | Lakshadweep Islands, India | Indian EEZ | t </code></pre> <p>So, each part of the code works, but apparently not together since it returns <code>NULL</code> on 10 entries. It seems that it only counts each distinct coordinates (<code>lat</code>) within <code>location</code> <code>Lakshadweep Islands, India</code> I have tried to fix it but without success. I am hoping for some pointers, thanks </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload