How to Get a Single Column's Values into an Array

How to get a single column's values into an array

In Rails 3.2 there is a pluck method for this

Just like this:

Person.pluck(:id) # SELECT people.id FROM people
Person.pluck(:role).uniq # unique roles from array of people
Person.distinct.pluck(:role) # SELECT DISTINCT role FROM people SQL
Person.where(:confirmed => true).limit(5).pluck(:id)

Difference between uniq and distinct

MySQL - Convert single column into an array

You can use the GROUP_CONCAT() function for that.

Usage is straightforward:































idval
11001
21002
342022
4203412
524252

get column values into an array

You should move the print command out of the while loop:

List l = new ArrayList();
try {
String sql = "select * from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String con = rs.getString("Contact");
l.add(con);
}
} catch (Exception e) {
e.getMessage();
}
System.out.println(l);

OR only print the current element in each iteration;

List l = new ArrayList();
try {
String sql = "select * from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String con = rs.getString("Contact");
l.add(con);
System.out.print(con + " ");
}
} catch (Exception e) {
e.getMessage();
}

convert row values to single column as array (or to multiple columns)

Consider below approach

select empid, totaltax, totaldeductions, taxes, deductions
from emptbl e
left join (
select empid, array_agg(t.tax order by tax_line_item) taxes
from tax t group by empid
) using (empid)
left join (
select empid, array_agg(t.deduction order by ded_line_item) deductions
from deductions t group by empid
) using (empid)

if applied to sample data in your question - output is

Sample Image

How to save just one column values of a table into an array?

As suggested in a comment you may use BULK COLLECT

select your_col BULK COLLECT 
INTO your_collection from my_array where some_condition = 'something';

Regarding your question

if query returns less than 6 rows is it possible to put NULL into
array element where there is no rows for it

You have not said why it's required but a BULK COLLECT will create as many elements in the array as the number of rows present in the query result. In case you need NULL elements, you may use count and extend collection methods to check and allocate null elements until the count is 6.

DECLARE
TYPE myarrtype is table of integer;
my_array myarrtype;
BEGIN
select level bulk collect into my_array from dual
connect by level <= 4; --generates 4 integers 1-4 and loads into array.
dbms_output.put_line('OLD COUNT '||my_array.count);

if my_array.count <= 6 then
my_array.extend(6 - my_array.count); --This appends required number of
--NULL elements
dbms_output.put_line('NEW COUNT '||my_array.count);
end if;
END;
/

Output

1 rows affected

dbms_output:
OLD COUNT 4
NEW COUNT 6

Demo

Converting lists in a column into an array

This doesn't rely on a loop, but I think it will do what you want.

I took the liberty to create a test dataframe:

d1 = {'id': [1, 2, 3],
'features': [[1, 2, 3], [5, 6, 3,], [9, 2, 9]]}
df1 = pd.DataFrame(data=d1)

here we convert the features to a numpy array:

ar = np.array(df1.features.tolist(), dtype=np.float32)

then we add it as a DataFrame column, using the Series constructor:

df1["features_array"] = pd.Series(list(ar))


Related Topics



Leave a reply



Submit