jeudi 10 décembre 2020

Laravel eloquent - Converting large SQL query with multiple selects and join

I have large SQL query with nested select and inner join.

   "SELECT 
        @From_Date As From_Date, @To_Date As To_Date, cd_4.userfield, 
        COUNT( DISTINCT (Case When cd_4.disposition_Last =  'ANSWERED' Then cd_4.uniqueid ELSE NULL END) )  AS Answered_Count, 
        COUNT( DISTINCT (Case When cd_4.disposition_Last <> 'ANSWERED' Then cd_4.uniqueid ELSE NULL END) )  AS Not_Answered_Count 
    FROM 
    (SELECT 
        @From_Date As From_Date, @To_Date As To_Date, 
        cd_2.Userfield, cd_2.Start_Date, cd_2.uniqueid, cd_2.Row_Id_Max, 
        cd_3.disposition AS disposition_Last 
    FROM    
        (SELECT 
            cd_1.userfield, cd_1.Start_Date, cd_1.uniqueid, MAX(cd_1.Row_Id) AS Row_Id_Max 
        FROM 
            cdrnew As cd_1
        WHERE 
            (cd_1.Start_Date BETWEEN @From_Date AND @To_Date)
            AND cd_1.userfield = 'Inbound' 
        GROUP BY 
            cd_1.userfield, cd_1.Start_Date, cd_1.uniqueid
        ) AS cd_2
        INNER JOIN cdrnew As cd_3
            ON cd_3.Row_Id = cd_2.Row_Id_Max
    WHERE 
        (cd_3.Start_Date BETWEEN @From_Date AND @To_Date)
        AND cd_3.userfield = 'Inbound') AS cd_4";

As I am new in Laravel, I am familiar with simple Eloquent queries. But I don't have any idea as how can I convert such huge SQL query to Eloquent ?

I want some guidance regarding these so that I can achieve the required output. What steps I need to follow and how can I make it easier to convert into Eloquent query ? Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire