I have a data tab with following columns:
State => string
City => string
Person_ID => int
Status => values(0/1)
OnLeave => values(0/1)
Need to create a Pivot table like below:
Report Filter: State
Row Labels: City
Values: count(Person_ID), sum(Status), sum(OnLeave)
I want to do a conditional count for count(Person_ID) where this should count Person_ID only when OnLeave is 0.
Tried using Calculated Field, it doesn't work since its applied at the pivot table level instead of Data Row. Is there a way to do it directly in Pivot table?
Add a column to the source data with an IF statement that evaluates the OnLeave column and returns a 1 when true. Then sum that column in the pivot table.
add to report filter: onLeave column, select 0 as filter
Related
We have Employee table. We get all columns there are in that table through:
Employee.columns.map(&:name)
Next, we want to know how many fields are nil or empty in a single row.
How can we find it?
You can count empty columns in a single row using:
Employee.find(123).attributes.values.count { |v| v.nil? }
(just change 123 to your employee id)
You can use this to find the number of columns with nil value in a row:
id = 1 // or params[:id]
Employee.find(id).attributes.values.select(&:nil?).count
I have two tables, Table A is the parent, Table B is the child. Both tables have the column "order_number".
I want to find the value in the "order_number" column in Table A, that matches the value of the "order_number" column in Table B, then I'll save the "id" of the Table A record in another column of the Table B record.
I'm having trouble coming up with an activerecord query to find the matching column value. How should I do that?
Activerecord query to find the matching column value:
I think this should do it:
TableA.where(order_number: TableB.pluck(:order_number))
Then you can loop over it to update the tableB column for TableA id like below(assuming its called table_a_id)
tablea_records = TableA.where(order_number: TableB.pluck(:order_number))
tablea_records.each do |a|
b.where(order_number:a.order_number).update_all(table_a_id: a.id)
end
I have a table user_keywords which :belongs_to :keywords. user_keywords has a column keyword_id, user_id, and relevance_score (float).
keyword table has a column 'name'.
I want to group all the user_keywords by their keyword_id.
I want to take the average of each of those groups' relevance_score
I want to sort groups by the highest relevance score
I want to return the name of the keyword from the groups, sorted by highest relevance score.
What is the most efficient way to query this?
try this:
Keyword.joins(:user_keywords)
.select('keywords.name, avg(user_keywords.relevance_score) as score')
.group('keywords.name')
.order('score DESC')
.map(&:name)
In Rails 3, how do i select rows based on unique column values, i need to get all the columns for eg:
SELECT COUNT(DISTINCT date) FROM records
This only returns date column, but i want all the columns (name, date , age , created_at) columns not just the date.
Thanks for your help
The issue here is that, by definition, there may be multiple records with the same date. It requires logic in the user space to determine which of the multiple records with the unique date to use. Here's some code to get those rows:
Record.select("distinct date").each do |record|
records = Record.find_by_date record.date
puts records.count # do something with the records
end
If what you're really after is uniqueness among multiple columns, list all the relevant columns in the distinct query:
Record.select("distinct date, name, age, created_at").each do |record|
puts record.date
puts record.name
puts record.age
puts record.created_at
# ``record'' still represents multiple possible records
end
The fact that you are using distinct means that each "row" returned actually represents n rows, so the DB doesn't know which of the n rows to pull the remaining columns from. That's why it only returns the columns used in distinct. It can do no other...
I think this will help you
Model.find(:all, :select => 'DISTINCT name, date, age, created_at')
Please use it and let me know.
Model.group(:column)
For your case:
Record.group(:date)
This will return all your columns with no "date" repetitions.
For rails 3.2 and higher, Model.select('DISTINCT name, date, age, created_at')
In two tables mapped to ActiveRecord with unknown number of identical columns, e.g.:
Table A Table B
--------- ---------
id id
name name
age email
email is_member
How can I (elegantly) copy all identical attributes from a record of Table A to a record of Table B, except the id attribute?
For the example tables above, name and email fields should be copied.
Try this:
Get intersection of the columns between TableA and TableB
columns = (TableA.column_names & TableB.column_names) - ["id"]
Now iterate through TableA rows and create the TableB rows.
TableB.create( TableA.all(:select => columns.join(",") ).map(&:attributes) )
Edit: Copying one record:
table_a_record = TableA.first(:select => columns.join(","), :conditions => [...])
TableB.create( table_a_record.attributes)
Migt consider using a union function on the acitverecord attributes hash between the 2 tables. It's not a complete answer but may help