简体   繁体   中英

How to run an SQL query on tables across multiple databases with Eloquent ORM?

I have an SQL query on tables across multiple databases and I want to run it through Eloquent ORM

I also have the following constraints:

  • I am using SQL Server
  • My database names are: HN_Ondata , and EVS_BKP

Here is my SQL query, plese help me run it with Eloquent ORM .

SELECT CONVERT(VARCHAR(10), CallLocalTime, 111) CallLTime,
    COUNT(ID) IDCount,
    SUM(IvrDuration) IvrDSum,
    SUM(TotalWaitDuration) TotalWDuration
FROM
    (
    SELECT
        Id,
        calltype,
        CallLocalTime,
        CallLocalTimeString,
        ConvDuration,
        IvrDuration,
        TotalWaitDuration,
        FirstAgent 
    FROM
        HN_Ondata.dbo.ODCalls UNION ALL
    SELECT
        Id,
        calltype,
        CallLocalTime,
        CallLocalTimeString,
        ConvDuration,
        IvrDuration,
        TotalWaitDuration,
        FirstAgent 
    FROM
        EVS_BKP.dbo.ODCalls_2016_11 
) x  

WHERE 
    CallLocalTime > '2016-11-01'
    AND CallLocalTime < '2016-11-30'
    AND calltype = 1 AND IvrDuration > 0 
    AND ConvDuration = 0 
    AND TotalWaitDuration > 0 
    AND ( FirstAgent IS NULL OR FirstAgent = 0 ) 
GROUP BY
    CONVERT(VARCHAR(10), CallLocalTime, 111)

use Illuminate\Database\Capsule\Manager as DB;

<?php
$ODCalls = DB::table("ODCalls")
                                ->select(DB::raw('COUNT(ID) IDCount'))
                                ->select(
                                "ID"
                                , "Calltype"
                                , "CallLocalTime"
                                , "CallLocalTimeString"
                                , "ConvDuration"
                                , "IvrDuration"
                                , "TotalWaitDuration"
                                , "FirstAgent"
                        );


                        $ODCalls_union = DB::table("ODCalls_2016_12")
                                ->select(
                                        "ID"
                                        , "Calltype"
                                        , "CallLocalTime"
                                        , "CallLocalTimeString"
                                        , "ConvDuration"
                                        , "IvrDuration"
                                        , "TotalWaitDuration"
                                        , "FirstAgent"
                                )
                                ->where('CallLocalTime', '>', '2016-12-01')
                                ->where('CallLocalTime', '<', '2016-12-30')
                                ->where('Calltype', 1)
                                ->where('ConvDuration', 0)
                                ->where('TotalWaitDuration', '>', 0)
                                ->where(function ($query) {
                                    $query->where('FirstAgent', '')->orWhere('FirstAgent', 0);
                                })
                                ->union($ODCalls);


                        $result= $ODCalls_union->get()->groupBy(function($row) {
                            return Carbon::parse($row->CallLocalTime)->format('Y-m-d');
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM