Combine 2 results in one in SQL - sql

Update merge
Cama::PostType.first.posts.joins(:custom_field_values)
.where("cama_custom_fields_relationships.custom_field_slug = ? AND
cama_custom_fields_relationships.value LIKE ?","localization",
"%Paris%").merge(Cama::PostType.first.posts.joins(:custom_field_values)
.where("cama_custom_fields_relationships.custom_field_slug = ? AND
cama_custom_fields_relationships.value = ?","type-localization", "2"))
This also doesnt work. When executed seperatelty, it returns same AssociationRelation. I guess it only works for ActiveRecord:Relation
Update
I think Im looking for INTERSECT but don't know how to use it with where
There is another topic that I created and still can't find answer how to optimize it.
It goes likes this
I need to find "posts" by "other_model" values. Other model has relationship with posts throught another table but lets keep it simple. When i do
Foo.joins(:other_model).where("other_model.value = ? AND other_model.value = ?", "one", "two")
This of course won't find me any result because it contradicts itself.
When I do with OR instead of AND
Foo.joins(:other_model).where("other_model.value = ? OR other_model.value = ?", "one", "two")
It finds posts for me but... either it has one value or either has second value and...
I want to find posts based on other_model.value = one and other_model.value = two
Which means it looks for 2 seperate results and then I need to just return ids that covers each other... Does it make sense ?

I think you are looking for a query like:
Foo
.joins(:other_model)
.where('other_model.value = ? OR other_model.value = ?', 'one', 'two')
.group('foos.id')
.having('COUNT(other_models.id) >= 2')

Related

Filtering records based on all members of association

I have a model called Story, which has — and belongs to many — Tags. I'm trying to create functionality to display only certain stories based on the story attributes. I do this by chaining where()s:
q = Story.where(condition1)
q = q.where(condition2)
...et cetera. One of the things I want to be able to filter on is tags, which at first I tried to do as follows:
q = q.joins(:tags)
q = q.where(tagCondition1)
q = q.where(tagCondition2)
...
However, this only finds stories that have a single tag that matches all conditions. I want to find all stories that have at least one tag that matches each condition. That is, currently if I have the conditions LIKE %al% and LIKE %be%, it will match a story with the tag 'alpha beta'; I want it to also match a story with the tag 'alpha' and the tag 'beta'.
Maybe you need the below to match multiple conditions:
q.where([tagCondition1, tagCondition2, ...])
You can use HAVING with a count.
class Story < ActiveRecord::Base
has_and_belongs_to_many :tags
def self.with_tags(*tags, min: 1)
joins(:tags)
.where(tags: { name: tags })
.group('story.id')
.having("count(*) = ?", min)
end
end
Usage:
params[:tag] = "foo bar baz"
Story.with_tags(params[:tag], *params[:tag].split)
Would include stories with any of the tags ["foo bar baz", "foo", "bar", "baz"].
The query is wrong. Since you are using AND and you want to match the first OR the second condition. Maybe you should use something like
where("tags.field like '%al% or tags.field like '%be%'")
Okay, so here's what I ended up doing: (added for anyone who's googling and has a similar problem):
conditions.each do |condition|
q_part = select('1').from('stories_tags')
q_part = q_part.where('stories_tags.story_id = stories.id')
q_part = q_part.where('stories_tags.name SIMILAR TO ?', condition)
q = q.where("EXISTS (#{q_part.to_sql})")
end

Selecting related model: Left join, prefetch_related or select_related?

Considering I have the following relationships:
class House(Model):
name = ...
class User(Model):
"""The standard auth model"""
pass
class Alert(Model):
user = ForeignKey(User)
house = ForeignKey(House)
somevalue = IntegerField()
Meta:
unique_together = (('user', 'property'),)
In one query, I would like to get the list of houses, and whether the current user has any alert for any of them.
In SQL I would do it like this:
SELECT *
FROM house h
LEFT JOIN alert a
ON h.id = a.house_id
WHERE a.user_id = ?
OR a.user_id IS NULL
And I've found that I could use prefetch_related to achieve something like this:
p = Prefetch('alert_set', queryset=Alert.objects.filter(user=self.request.user), to_attr='user_alert')
houses = House.objects.order_by('name').prefetch_related(p)
The above example works, but houses.user_alert is a list, not an Alert object. I only have one alert per user per house, so what is the best way for me to get this information?
select_related didn't seem to work. Oh, and surely I know I can manage this in multiple queries, but I'd really want to have it done in one, and the 'Django way'.
Thanks in advance!
The solution is clearer if you start with the multiple query approach, and then try to optimise it. To get the user_alerts for every house, you could do the following:
houses = House.objects.order_by('name')
for house in houses:
user_alerts = house.alert_set.filter(user=self.request.user)
The user_alerts queryset will cause an extra query for every house in the queryset. You can avoid this with prefetch_related.
alerts_queryset = Alert.objects.filter(user=self.request.user)
houses = House.objects.order_by('name').prefetch_related(
Prefetch('alert_set', queryset=alerts_queryset, to_attrs='user_alerts'),
)
for house in houses:
user_alerts = house.user_alerts
This will take two queries, one for houses and one for the alerts. I don't think you require select related here to fetch the user, since you already have access to the user with self.request.user. If you want you could add select_related to the alerts_queryset:
alerts_queryset = Alert.objects.filter(user=self.request.user).select_related('user')
In your case, user_alerts will be an empty list or a list with one item, because of your unique_together constraint. If you can't handle the list, you could loop through the queryset once, and set house.user_alert:
for house in houses:
house.user_alert = house.user_alerts[0] if house.user_alerts else None

Fusion of two queries in active record rails

How could these two queries be merged in Rails 5?
Event
.where(starts_at: date.beginning_of_day..date.end_of_day)
.where(kind: "opening")
Event.where("cast(strftime('%w', starts_at) as int) = ?", date.wday)
.where(kind: "opening")
.where(weekly_recurring: true)
I need to take all these events in one query for performance.
Thanks for the help
If you want to combine results then
Event.where(starts_at: date.beginning_of_day..date.end_of_day).where(kind: "opening").where("cast(strftime('%w', starts_at) as int) = ?", date.wday).where(kind: "opening").where(weekly_recurring: true)
If you want two separate results in one query, i dont think active records has such method.
I have always easier to write complex queries such as the one you need directly in SQL; It will be faster for you to write than trying to shoehorn it into ActiveRecord.
In your case, I would do something like this:
query = "starts_at between #{date.beginning_of_day} and #{date.end_of_day}
and kind = 'opening'
and weekly_recurring = true
and cast(strftime('%w', starts_at) as int) = #{date.wday}"
Event.where(query)
I find this approach easier and more maintanable.

sqlalchemy: paginate does not return the expected number of elements

I am using flask-sqlalchemy together with a sqlite database. I try to get all votes below date1
sub_query = models.VoteList.query.filter(models.VoteList.vote_datetime < date1)
sub_query = sub_query.filter(models.VoteList.group_id == selected_group.id)
sub_query = sub_query.filter(models.VoteList.user_id == g.user.id)
sub_query = sub_query.subquery()
old_votes = models.Papers.query.join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id).paginate(1, 4, False)
where the database model for VoteList looks like this
class VoteList(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
arxiv_id = db.Column(db.String(1000), db.ForeignKey('papers.arxiv_id'))
vote_datetime = db.Column(db.DateTime)
group = db.relationship("Groups", backref=db.backref('vote_list', lazy='dynamic'))
user = db.relationship("User", backref=db.backref('votes', lazy='dynamic'), foreign_keys=[user_id])
def __repr__(self):
return '<VoteList %r>' % (self.id)
I made sure that the 'old_votes' selection above has 20 elements. If I use .all() instead of .paginate() I get the expected 20 result?
Since I used a max results value of 4 in the example above I would expect that old_votes.items has 4 elements. But it has only 2? If I increase the max results value the number of elements also increases, but it is always below the max result value? Paginate seems to mess up something here?
any ideas?
thanks
carl
EDIT
I noticed that it works fine if I apply the paginate() function on add_columns(). So if I add (for no good reason) a column with
old_votes = models.Papers.query.join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id)
old_votes = old_votes.add_columns(sub_query.c.vote_datetime).paginate(page, VOTES_PER_PAGE, False)
it works fine? But since I don't need that column it would still be interesting to know what goes wrong with my example above?
Looks to me that for the 4 rows returned (and filtered) by the query, there are 4 rows representing 4 different rows of the VoteList table, but they refer/link/belong to only 2 different Papers models. When model instances are created, duplicates are filtered out, and therefore you get less rows. When you add a column from a subquery, the results are tuples of (Papers, vote_datetime), and in this case no duplicates are removed.
I encountered the same issue and I applied van's answer but it did not work. However I agree with van's explanation so I added .distinct() to the query like this:
old_votes = models.Papers.query.distinct().join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id).paginate(1, 4, False)
It worked as I expected.

Active Record query to match every subset element

In my RoR application, I've got a database lookup similar to this one:
Client.joins(:products).where({'product.id' => [1,2,3]})
Unfortunately this will return all clients that have bought product 1, 2 or 3 but I only want to get back the clients, that bought all of the three products. In other words, I'd like to write a query that matches for n elements in a given set.
Are there any elegant solutions for this?
This is not really elegant. But it should translate into the needed SQL.
Client.joins(:products).
where({'products.id' => [1,2,3]}).
group('users.id').
having('COUNT(DISTINCT products.id) >= 3')
Same answer with more dynamic way
ids = [1,2,3]
Client.joins(:products).
where({'products.id' => ids}).
group('users.id').
having('COUNT(DISTINCT products.id) >= ?', ids.size)