Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --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 );
- 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
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement