Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. --CODE
  2. CREATE TABLE test_tbl (accn varchar(10) PRIMARY KEY, method TEXT, species VARCHAR(30), lat NUMERIC,
  3. lon NUMERIC, location TEXT, larger_region TEXT, coords BOOLEAN DEFAULT TRUE);
  4.  
  5. FETCH FORWARD 1 FROM test_tbl;
  6.  
  7. accn | method | species | lat | lon | location | larger_region | coords
  8. ---------+----------+-------------------+----------+----------+----------------------------+---------------+---------
  9. EU336944 | Isolate | Dysidea granulosa | 10.07704 | 73.63238 | Lakshadweep Islands, India | Indian EEZ | t
  10.  
  11. CREATE TABLE test_view_tbl ( species VARCHAR(30) NOT NULL, no_ind BIGINT NOT NULL, method TEXT NOT NULL, location TEXT NOT
  12. NULL, larger_region TEXT NOT NULL );
  13.  
  14. CREATE FUNCTION test_view() RETURNS SETOF test_view_tbl AS $$
  15. SELECT DISTINCT ON (species, method, location, larger_region) species, count(species), method, location, larger_region
  16. FROM test_tbl
  17. GROUP BY species, method, location, larger_region
  18. ORDER BY species;
  19. $$ LANGUAGE sql
  20.  
  21. SELECT * FROM test_view();
  22.  
  23. species | no_ind | method | location | larger_region
  24. ------------------+--------+---------+--------------------------------------+---------------
  25. Dysidea granulosa | 6 | Isolate | Kavaratti Island, Lakshadweep, India | Indian EEZ
  26. Dysidea granulosa | 13 | Isolate | Lakshadweep Islands, India | Indian EEZ
  27.  
  28. ALTER TABLE test_view_tbl ADD COLUMN coords BOOLEAN DEFAULT TRUE;
  29.  
  30. CREATE FUNCTION test_view() RETURNS SETOF test_view_tbl AS $$
  31. SELECT DISTINCT ON (species, method, location, larger_region) species, count(species), method, location, larger_region,
  32. CASE WHEN lat IS NULL THEN coords IS FALSE ELSE TRUE
  33. END
  34. FROM test_tbl
  35. GROUP BY species, method, location, larger_region, lat, coords
  36. ORDER BY species;
  37. $$ LANGUAGE sql
  38.  
  39. SELECT * FROM test_view();
  40.  
  41. species | no_ind | method | location | larger_region | coords
  42. -------------------+--------+---------+--------------------------------------+---------------+--------
  43. Dysidea granulosa | 6 | Isolate | Kavaratti Island, Lakshadweep, India | Indian EEZ | t
  44. Dysidea granulosa | 3 | Isolate | Lakshadweep Islands, India | Indian EEZ | t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement