How do I join multiple hive queries? - join

I am trying to join a simple query with a very ugly query that resolves to a single line. They have a date and a userid in common but nothing else. Alone both queries work but for the life of me I cannot get them to work together. Can someone assist me in how I would do this?

Fixed it...when you union queries in hive it looks like you need to have an equal number of fields coming back from each.

Related

ActiveRecord WHERE IN with a separate query—combine to a single INNER JOIN query

I currently have a very complicated query which (simplified) looks like:
User.where(party_id: [Party.cool_parties.pluck(:id)])
It works great, but hits the db with 2 separate queries. I'm just wondering how to combine it into a single query? Some kind of inner join with a temporary table somehow? I'm not sure how to do that with Rails...
User.where(party_id: Party.cool_parties)
Just pass the assocation/relation and ActiveRecord will construct a subquery.
pluck should only be used in the cases where you actually want to force a query to load the data as an array - which is a lot less often then then you think.

ActiveRecord .joins breaking other queries

I'm writing a Rails API on top of a legacy database with tons of tables. The search feature gives users the ability to query around 20 separate columns spread across 13 tables. I have a number of queries that check the params to see if they need to return results. They look like this:
results << Company.where('city LIKE ?', "#{params[:city]}").select('id') unless params[:city].blank?
and they work fine. However, I just added another query that looks like this:
results << Company.joins("JOIN Contact ON Contact.company_id = Company.id").where("Contact.first_name LIKE ?", "%#{params[:first_name]}%").select('company_id') unless params[:first_name].blank?
and suddenly my first set of queries started returning null, rather than the list of IDs they had been returning. The query with the join works perfectly well whether the other queries are functional or not. When I comment the join query out, the previous queries start working again. Is there some reason the query with a join would break other queries on the page?
I can't think of a particular reason why the join would be breaking your previous queries however I do have some suggestions for your query overall.
Assuming you've modelled these relationships correctly you shouldn't need to define the join manually. On another note, you're not querying against the company at all so you can use an includes instead of a join - this will allow you to access its data without firing another query.
If you wanted to access company data (ie. query.company.name) use an includes like so:
Contact.includes(:company).where('first_name LIKE ?', param).select(:company_id).distinct
However it appears all you really want is an array of ID's (which exists on the contact model), because of this you can lighten things up and not include the company at all.
Contact.where('first_name LIKE ?', param).select(:company_id).distinct
Whenever you get stuck never forget to checkout the great resources over at: http://api.rubyonrails.org/ - they are an absolute life saver sometimes!
It turned out that the queries with a join needed to be placed above the queries without a join. I'm not sure why it behaves this way, but hopefully this helps someone else down the line.

Unique query based off another column

So, I'd like to get a bunch of ids based on uniqness of another column.
[1,'A']
[2,'B']
[3,'C']
[4,'A']
I'd like to return [1,2,3] (losing the 4th because 'A' is no longer unique). I've tried .select('DISTINCT letter, id') and some grouping, but I can't seem get it.
My database is PostgreSQL. SQL or ActiveRecord is fine.
Probably not the best solution, but it works with Postgres.
Model.select("min(id) as id, letter").group(:letter).map(&:id)
EDIT:
Actually, I found an even better (read: shorter) solution.
Model.group(:letter).pluck("min(id)")
ANOTHER EDIT:
I've found a solution that works without aggregation (min or max).
Model.select("distinct on (letter) letter, id").map(&:id)

How do I make a Grails criteria execute as a left join?

I have a Grails criteria that has associated children classes that are nullable. I need to get all results, even those with null children, but the criteria gets executed with an INNER JOIN. How do I get it to be executed with a LEFT JOIN?
Grails version is 1.3.7 (most recent), query is being executed via createCriteria().list
As question is old and lot of improvement has been done on this. Following will help someone searching like me.. Following works even with complex and{} and or{} blocks inside criteria query with collections. in eg. specialities is a collection.
List users = User.createCriteria().list(){
createAlias('specialities', 'sp', CriteriaSpecification.LEFT_JOIN)
ilike("sp.name","%"+trimPhrase+"%")
}
try to use a hql statement. for left joins see: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
hql statements can be performed as follows: DomainClass.executeQuery("select from ....")

Search a relation without a second query

My question is about how to perform varying levels of search into a database while limiting the number of queries.
Let's start simple:
#companies = Company.where("active = ?", true)
Let's say we display records from this set. Then, we need:
#clientcompanies = #companies.where("client_id = ?", #client.id)
We display something from #clientcompanies. Then, we want to drill down further.
#searchcompanies = #clientcompanies.where("name LIKE ? OR notes LIKE ?", "#{params[:search]}%", "#{params[:search]}%")
Are these three statements the most efficient way to go about this?
If indeed the database is starting with the entire Company table each time around, is there a way to limit the scope so each of the above statements would take a shorter amount of time as the size of the set diminishes?
In case it matters, I'm running Rails 3 on both MySQL and PostgreSQL.
It doesn't get much more optimized then what you're already doing. Exactly zero of those statements will execute a SQL query until you try to iterate over the results. Calling methods like all, first, inspect, any?, each etc will be when the query is executed.
Each time you chain on a new where or other arel method, it appends to the sql query that it'll execute at the end. If, somewhere in the middle, you want to see the query that'll be executed you can do puts #searchcompanies.to_sql
Note that if you run these commands in the console each statement appears to run a SQL query only because the console automatically runs .inspect on the line you entered.
Hopefully I answered your question :)
There's a great railscast here: http://railscasts.com/episodes/239-activerecord-relation-walkthrough that explains how ActiveRelation works, and what you can do with it.
EDIT:
I may have mis-understood your question. You indicated that after each where call you were displaying information from the query. What's the use-case for this? Are you displaying all companies on the same page that you have filtered-out companies from a search? If you display something from that very first query then you will be pulling every single company row from your database (which is not going to be very scalable or performant at larger quantities of company entries).
Would it not make sense to only display information from the #searchcompanies variable?

Resources