File: crosstab.sql

package info (click to toggle)
postgresql-unit 7.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,540 kB
  • sloc: sql: 1,768; ansic: 1,334; lex: 358; yacc: 140; perl: 100; makefile: 40; sh: 25
file content (92 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
SET unit.time_output_custom = false;

-- test multiplication
WITH i(i) AS (VALUES ('-2'::unit), ('-1'), ('0'), ('1'), ('2'))
SELECT
  unit, i, unit * i
FROM
  units CROSS JOIN i
WHERE
  (base OR coherent) AND NOT duplicate
\crosstabview

SELECT
  a.unit AS a, b.unit AS b, a.unit * b.unit AS mul
FROM
  units AS a CROSS JOIN units AS b
WHERE
  (a.base OR a.coherent) AND NOT a.duplicate AND (b.base OR b.coherent) AND NOT b.duplicate
\crosstabview

SELECT
  f, unit, f * unit
FROM
  factors CROSS JOIN units
WHERE
  (base OR coherent) AND NOT duplicate
\crosstabview

-- test division
WITH i(i) AS (VALUES ('-2'::unit), ('-1'), ('1'), ('2'))
SELECT
  unit, i, unit / i
FROM
  units CROSS JOIN i
WHERE
  (base OR coherent) AND NOT duplicate
\crosstabview

WITH i(i) AS (VALUES ('-2'::unit), ('-1'), ('0'), ('1'), ('2'))
SELECT
  i, unit, i / unit
FROM
  units CROSS JOIN i
WHERE
  (base OR coherent) AND NOT duplicate
\crosstabview

SELECT
  a.unit AS a, b.unit AS b, a.unit / b.unit AS div
FROM
  units AS a CROSS JOIN units AS b
WHERE
  (a.base OR a.coherent) AND NOT a.duplicate AND (b.base OR b.coherent) AND NOT b.duplicate
\crosstabview

-- test comparisons
WITH
  v(u) AS (VALUES
	('-2'::unit),
	('-1'::unit),
	(-kilogram()),
	(-meter()),
	('-0'::unit),
	('0'::unit),
	('1'::unit),
	(kilogram()),
	(meter()),
	('2'::unit),
	(kilogram(2)),
	(meter(2))
  ),
  va(a) AS (SELECT * FROM v),
  vb(b) AS (SELECT * FROM v)
SELECT
  a, b,
  CASE WHEN unit_cmp(a, b) < 0 THEN '<'
       WHEN unit_cmp(a, b) = 0 THEN '='
       WHEN unit_cmp(a, b) > 0 THEN '>'
  END AS cmp
FROM
  va CROSS JOIN vb
\crosstabview

-- test all prefix/unit combinations
SELECT
  u, p, (p||u)::unit
FROM
  prefixes CROSS JOIN units
WHERE
  u <> 'kg' AND
  (p||u) <> 'dat' -- ambiguous
\crosstabview