samedi 17 juin 2023

Rails calculating a virtual column using 2 other virtual columns

I need help converting the following MySQL query into an ActiveRecord Query that will produce a relation with IP records as object. To explain this in a better way, How to convert the following query in How to sum two virtual column into third column in sql in ActiveRecord query ?

select a_id
u_c,
i_c,
(u_c + i_c) as t_c
from (
    select distinct a.id as a_id,
    (   SELECT count(b.id) FROM UP b
        WHERE b.i_p_id = a.id
        AND b.role = "L"
    ) as u_c,
    (   SELECT count(b.id) from UP b
        WHERE b.institution_package_id = a.id
        AND b.role = "I"
    ) as i_c
    from IP a
    left outer join UP b on b.IP = a.id       
) sub 

Let me explain a litte more. I have 2 tables where I am querying into the 1st table and calculating 3 virtual columns. The 3rd column will be the sum of the other 2 virtual columns. Thanks for the help.

Aucun commentaire:

Enregistrer un commentaire