ActiveRecord, Nested includes with counter - ruby-on-rails

I have 3 tables (a, b, c) and "a" has_many "b" and "b" has_many "c". I have this line c.includes(b: :a), but I need to count how many "a" are for each "c" so the results be like 2 columns. Also there are some data in C that doesn't have b, so when I show them, it throw me an error.
I can do it with a query from pgAdmin and it show me how I need it but I can't make it on ActiveRecords.
The query that works fine is:
select count(c.id), a.name from c
left Join b on b.id = c.b_id
left join a on a.id = b.a_id
where c.created_at between '2018-11-08 00:00:00' and '2018-11-08 23:59:59' group by a.name

A.
joins(:b).
joins(b: :c).
group("a.name").
where("c.created_at between ? and ?", DateTime.new(2018,
11, 08), DateTime.new(2018, 11, 08).end_of_day).
count
returns a hash where the keys are "a.name" and the values are the count of C for each A.
If you only want to count unique C for each A, you can specify count("distinct c.id").
For more complex queries you can always query with raw SQL:
ActiveRecord::Base.connection.execute(<<~SQL).values
select count(c.id), a.name from c
left Join b on b.id = c.b_id
left join a on a.id = b.a_id
where c.created_at between '2018-11-08 00:00:00' and '2018-
11-08 23:59:59' group by a.name
SQL
returns a jagged array of the results.
As for your orphaned C records, when you query for them you can inner join to B. That will only return C that have B.

Related

How to only join on numeric values

Right now I have:
select
a.id
b.colone
b.coltwo
from tablea a
left join tableb b on b.id = a.id
I'm getting the "numeric value not recognized" error because one value from b.id is not numeric. How do I join results from tableb to tablea just omitting the non-numeric value/row? Or how else can I bypass this error? Using snowflake.
Thank you!
You can use TRY_TO_NUMBER():
select
a.id
b.colone
b.coltwo
from tablea a
left join tableb b on TRY_TO_NUMBER(b.id) = a.id

RIGHT OUTER JOIN returns empty results with WHERE

I need to produce a report of all records (businesses) created by a particular user each month over last months. I produced the following query and expect it to provide me with a row for each month. However, this user didn't create any records (businesses) these months so I get an empty result [].
I'm still expecting to receive a row for each month, since I'm selecting a generate_series column using RIGHT OUTER JOIN but it doesn't happen.
start = 3.months.ago
stop = Time.now
new_businesses = Business.select(
"generate_series, count(id) as new").
joins("RIGHT OUTER JOIN ( SELECT
generate_series(#{start.month}, #{stop.month})) series
ON generate_series = date_part('month', created_at)
").
where(created_at: start.beginning_of_month .. stop.end_of_month).
where(author_id: creator.id).
group("generate_series").
order('generate_series ASC')
How can I change my query to get a row for each month instead of an empty result? I'm using PosgreSQL.
UPDATE
This code works:
new_businesses = Business.select(
"generate_series as month, count(id) as new").
joins("RIGHT OUTER JOIN ( SELECT
generate_series(#{start.month}, #{stop.month})) series
ON (generate_series = date_part('month', created_at)
AND author_id = #{creator.id}
AND created_at BETWEEN '#{start.beginning_of_month.to_formatted_s(:db)}' AND
'#{stop.end_of_month.to_formatted_s(:db)}'
)
").
group("generate_series").
order('generate_series ASC')
Your problem is in the where part which is breaks any outer joins. Consider the example:
select *
from a right outer join b on (a.id = b.id)
It will returns all rows from b and linked values from a, but:
select *
from a right outer join b on (a.id = b.id)
where a.some_field = 1
will drops all rows where a is not present.
The right way to do such sings is to place the filter into the join query part:
select *
from a right outer join b on (a.id = b.id and a.some_field = 1)
or use subquery:
select *
from (select * from a where a.some_field = 1) as a right outer join b on (a.id = b.id)

Insert multiple records with join

I need some help in figuring out how to insert more than one records in a table using a join (when the join returns more than one values). So here is the scenerio:
Table A:
A_ID bigserial, Role Varchar(25), Description varchar(25)
Table B:
B_ID bigserial, Role Varchar(25), Code varchar(25)
Table A and B are connected with column Role.
Example Entries in Table_A:
1, A, Standard
2, B , Test
3, C, Test
4, D, Standard
Example Entries in Table_B:
1, A, ABC
2, B, XYZ
3, C, XYZ
4, D, ABC
Basically what I need to do is check for Roles where description = Test, then insert entry for this Custom Role to Table_B with Code = ABC (If entry doesn't exist already)
The following query will give me all the Test description Roles which do not have any entry with Code = ABC in table B
Query1:
SELECT ROLE FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.ROLE=B.ROLE
WHERE A.Description ='Test'
AND B.CODE<>'ABC';
I have the following insert query:
insert into Table_B (Role , Code)
select (SELECT ROLE FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.ROLE=B.ROLE WHERE A.Description ='Test'AND B.CODE<>'ABC'), 'ABC';
The above insert query only works when Query1 returns one role, however I am not sure how to insert into table_A when Query1 returns more than 1 results.
Can someone pls help? Not looking to use Stored Procs for the same
Thanks.
Edited:
Example Entries in Table_A:
1, A, Standard
2, B , Test
3, C, Test
4, D, Standard
5, E, TEST
Example Entries in Table_B:
1, A, ABC
2, B, XYZ
3, B, ABC
4, C, DEF
5, C, XYZ
6, D, ABC
7, E, XYZ
8, E, LLL
Query1 will not work here:
SELECT ROLE FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.ROLE=B.ROLE
WHERE A.Description ='Test'
AND B.CODE<>'ABC';
Using this query now:
SELECT distinct ROLE FROM TB where role not in (
SELECT B.ROLE FROM TA A
INNER JOIN TB B
ON A.ROLE=B.ROLE
WHERE A.Description =Test
AND B.CODE=ABC)
and role in (select role from TA where Description =Test);
How will the insert work now?
You can make another column as 'Code'.
Something like:
insert into Table_B (Role , Code)
SELECT ROLE, 'ABC' CODE FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.ROLE=B.ROLE WHERE A.Description ='Test' AND B.CODE<>'ABC';
So number of columns will be match.

Why is Arel dropping the 'left outer join' when it builds SQL?

I'm trying to build the following query in Arel:
select a.* from (first nested query) as a
left outer join (second nested query) as b
on a.id = b.id
where b.id is null;
This is my best attempt:
query = a.
project(a[Arel.star]).
from(a_nested_sql).
join(b_nested_sql, Arel::Nodes::OuterJoin).
on(a[:id].eq(b[:id])).
where(b[:id].eq(nil))
But it keeps dropping the 'left outer join' producing invalid SQL:
select a.* from (first nested query) as a
(second nested query) as b
on a.id = b.id
where b.id is null;
What am I doing wrong?
After a lot of experimenting, this did the trick:
query = a_table.
project(a_table[Arel.star]).
from(a_nested_sql).
join(
b_table.join(b_nested_sql).join_sources,
Arel::Nodes::OuterJoin
).
on(a_table[:id].eq(b_table[:id])).
where(b_table[:id].eq(nil))
Not really sure why as I don't really understand what join_sources does.

DQL Select Join

I'm trying to make a DQL query (doctrine from symfony 2.2) with no success on these entities:
Lesson
Which has several LessonContent
Which are each linked to a User
I want to retrieve all the lessons of a user (should be pretty basic...).
SELECT l, lc FROM MyBundle:LessonContent lc
JOIN lc.lesson l JOIN lc.modifiedBy u
WHERE lc.creation=1 AND u.id = :userId
But this returns the LessonContent entities. If I select from Lesson, I can't JOIN the lessons (which is probably what I should be doing).
Can anyone help me?
The main table you query from is what doctrine gives you back as the main objects, so the following should work (assuming l.content points to the LessonContent association):
SELECT l, lc FROM MyBundle:Lesson l
JOIN l.content lc
JOIN lc.modifiedBy u
WHERE lc.creation=1 AND u.id = :userId
It turned out I needed to have a l.content*s* attribute in order to be able to select from lessons and then JOIN on the rest.
Entity:
/**
* #var ArrayCollection $contentHistory
* #ORM\OneToMany(targetEntity="AAA\CoreBundle\Entity\LessonContent", mappedBy="lesson", cascade={"persist", "remove"})
* #ORM\OrderBy({"lastModified" = "DESC"})
*/
private $contentHistory;
Query:
SELECT l FROM AAACoreBundle:Lesson l JOIN l.contentHistory lc JOIN lc.modifiedBy u WHERE lc.creation=1 AND u.id = :userId GROUP BY l
And with that it works like a charm!

Resources