Following code is working fine. But I want to define array['a', 'b', 'c', 'd', 'e'] as a variable.
rows, err := db.Query("select colname from (SELECT date, unnest(array['a', 'b', 'c', 'd', 'e']) AS colname, unnest(array[a, b, c, d, e]) AS thing from test1 where date='123') as tester where thing=1;")
So I try following code using github.com/lib/pq .
arr1 := []string{"a", "b", "c", "d", "e"}
rows, err := db.Query("select colname from (SELECT date, unnest($1) AS colname, unnest($1) AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))
But getting error like "pq: function unnest(unknown) is not unique". Table structure and sample data--
test=# \d+ test1
Table "public.test1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
a | character varying(10) | | extended | |
b | character varying(10) | | extended | |
c | character varying(10) | | extended | |
d | character varying(10) | | extended | |
e | character varying(10) | | extended | |
date | character varying(10) | | extended | |
test=# select * from test1 ;
a | b | c | d | e | date
---+---+---+---+---+------
3 | 1 | 3 | 2 | 3 | 124
3 | 3 | 2 | 2 | 1 | 125
1 | 2 | 2 | 1 | 3 | 126
1 | 2 | 3 | 2 | 3 | 127
1 | 1 | 2 | 2 | 3 | 123
(5 rows)
Basically I want the column name (a,b,c,d or e) which have value '1' on any specific date.
postgresql
but in c# with standard sql i would create array of characters, then merge them with separator ", " and then use it as parameter. – Mussel