Guest User

Untitled

a guest
Feb 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. CREATE TYPE test_item AS
  2. (
  3. id INTEGER,
  4. duration INTEGER
  5. );
  6. SELECT run_command_on_workers($cmd$CREATE TYPE test_item AS
  7. (
  8. id INTEGER,
  9. duration INTEGER
  10. );$cmd$);
  11.  
  12.  
  13.  
  14. CREATE TABLE test_count_distinct_array (key int, value int , value_arr test_item[]);
  15. SELECT create_distributed_table('test_count_distinct_array', 'key');
  16.  
  17. INSERT INTO test_count_distinct_array SELECT i, i, ARRAY[(i,i)::test_item] FROM generate_Series(0, 1000) i;
  18.  
  19.  
  20.  
  21. SELECT
  22. key,
  23. count(DISTINCT value),
  24. count(DISTINCT (item)."id"),
  25.  
  26. count(DISTINCT (item)."id" * 3)
  27. FROM
  28. (
  29. SELECT key, unnest(value_arr) as item, value FROM test_count_distinct_array
  30. ) as sub
  31. GROUP BY 1
  32. ORDER BY 1 DESC
  33. LIMIT 5;
  34.  
  35.  
  36.  
  37. SELECT user_id,
  38. count(sub.a::int),
  39. count(DISTINCT sub.a::int),
  40. count(DISTINCT (sub).a)
  41. FROM
  42. (SELECT user_id,
  43. unnnest(ARRAY[user_id * 1.5])a,
  44. random() b
  45. FROM users_table
  46. ) sub
  47. GROUP BY 1
  48. ORDER BY 1 DESC
  49. LIMIT 5;
Add Comment
Please, Sign In to add comment