How to turn a column of ints into one array in postgres - postgresql

I currently have a table with one column and 400 rows; each row has an integer. How can I create an int array with all of these integers that preserves order?
I am using postgreSQL-9.2.

select array_agg(int_column order by some_column) as int_array_column
from the_table;
Where some_column is the column that defines the "order" of the integer values. Rows in relational database do not have "an order", so your request "that preserves order" only makes sense if you have a column that defines that sort order that you try to preserve.

SELECT array_agg(column_name ORDER by sort_column_name) AS ints
FROM table

Related

Unnest a two-dimensional array to a table

Is it possible to convert the following into a two-columned table?
SELECT * FROM UNNEST([[1,'a'],[2,'b'], [3,'c']) AS tbl (num,str);
Or more generally into a table of n columns where n is the size of the inner array (i.e., the length of a row).
The array structure is arr[rows][cols]
Arrays in PostgreSQL can contain elements of one type (but it can be composite type). So array like array[1,2,'e'] is invalid in PostgreSQL.
What can be done is to unnest two arrays in one statement
SELECT UNNEST(array[1,2,3]),unnest(array['a','b','c'])

Is distinct function deterministic? T-sql

I have table like below. For distinct combination of user ID and Product ID SQL will select product bought from store ID 1 or 2? Is it determinictic?
My code
SELECT (DISTINCT CONCAT(UserID, ProductID)), Date, StoreID FROM X
This isn't valid syntax. You can have
select [column_list] from X
or you can have
select distinct [column_list] from X
The difference is that the first will return one row for every row in the table while the second will return one row for every unique combination of the column values in your column list.
Adding "distinct" to a statement will reliably produce the same results every time unless the underlying data changes, so in this sense, "distinct" is deterministic. However, it is not a function so the term "deterministic" doesn't really apply.
You may actually want a "group by" clause like the following (in which case you have to actually specify how you want the engine to pick values for columns not in your group):
select
concat(UserId, ProductID)
, min(Date)
, max(Store)
from
x
group by
concat(UserId, ProductID)
Results:
results

Which row is returned using DISTINCT ON in Postgres

When I use DISTINCT ON in PostgreSQL (distinct in django) which rows are retrieved in the group of rows with same fields?
The documentation says:
A set of rows for which all the expressions are equal are considered
duplicates, and only the first row of the set is kept in the output.
Note that the "first row" of a set is unpredictable unless the query
is sorted on enough columns to guarantee a unique ordering of the
rows arriving at the DISTINCT filter.
So if you add an ORDER BY clause, the first row in that order is kept.
Without an ORDER BY clause, there is no way of telling which row will be kept.

how to efficiiently select first or last rows from a KDB table stored on disk

For an in-memory table, I can use sublist or take syntax to retrieve first x, last x elements.
How to do this efficiently for an on-disk table which may be very large? The constraint is that I don't want to cache all the data from table to memory to run the query.
.Q.ind - it takes a table and (long!) indices into the table - and returns the appropriate rows
http://code.kx.com/q/ref/dotq/#qind-partitioned-index
I suppose you can use the i column which is the row number (per partition!) on a historical.
So the first row would be select from trade where date=first date, i = 0
The last row would I guess be select from trade where date=last date, i=max i
This assumes normal partitioned by date stuff. If you have just a non-partitioned tables, probably select from trade where i=0 would be fine

Create an index for json_array_elements in PostgreSQL

I need to create an index from a query that uses json_array_elements()
SELECT *, json_array_elements(nested_json_as_text::json) as elements FROM my_table
Since the json contains multiple elements, the result is that the original index is now duplicated across rows and no longer unique.
I am not very familiar with creating indices and want to avoid doing anything destructive. What is the best way to create a column of unique integers for this case?
Found an answer:
SELECT *, json_array_elements(nested_json_as_text::json) as elements, row_number() over () as my_index FROM my_table