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:
id | val |
---|---|
1 | 1001 |
2 | 1002 |
3 | 42022 |
4 | 203412 |
5 | 24252 |
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
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
Editing Existing Rails Migrations Is a Good Idea
Error Installing Ruby with Rvm (Osx 10.8)
How to Run a Ruby File in a Rails Environment
Ruby: What Is the Easiest Method to Update Hash Values
Ruby Cannot Load Such File - Active_Support/Core_Ext/Object/Blank
How to Stub Applicationcontroller Method in Request Spec
Ruby Ternary Operator Without Else
Ruby on Rails: Delete Multiple Hash Keys
How to Write a Ruby Switch Statement (Case...When) with Regex and Backreferences
What's the Best/Easiest Gui Library for Ruby
What Is Your Preferred Way to Produce Charts in a Ruby on Rails Web Application
Create Directory If It Doesn't Exist with Ruby
What Is the Use of Secret_Key_Base in Rails 4
Ruby (And Rails) Nested Module Syntax