可以进行 Laravel 雄辩查询或数据库外观,我从原始 SQL 查询中获得结果?

问题描述 投票:0回答:1
SELECT * FROM (
SELECT FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia,
(SUM(OpeningQty) - SUM(IssueQty)) AS fOpeningQty, SUM(PreReceivedQty) AS fReceivedQty, 
SUM(PreIssueQty) AS fIssueQty, 
(SUM(OpeningQty) - SUM(IssueQty) - SUM(PreIssueQty) + SUM(PreReceivedQty)) AS fClosingQty
FROM (
    SELECT FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia,
    SUM(Rcv_Qty) AS OpeningQty, 0 AS 'PreIssueQty', 0 AS 'PreReceivedQty', 0 AS 'IssueQty'
    FROM Tbl_Production_RCV
    WHERE ProductType='Finish Fabric Production' 
    AND CONVERT(date, Prodction_Date) < convert(date, GETDATE())
    GROUP BY FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia
    
    UNION 
    
    SELECT FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia,
    0 AS OpeningQty, SUM(Rcv_Qty) AS 'PreIssueQty', 0 AS 'PreReceivedQty', 0 AS 'IssueQty'
    FROM Tbl_Production_IS
    WHERE ProductType='Finish Fabric Production' 
    AND CONVERT(date, Prodction_Date) BETWEEN convert(date, GETDATE()) AND convert(date, GETDATE())
    GROUP BY FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia
    
    UNION 
    
    SELECT FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia,
    0 AS OpeningQty, 0 AS 'PreIssueQty', SUM(Rcv_Qty) AS 'PreReceivedQty', 0 AS 'IssueQty'
    FROM Tbl_Production_RCV
    WHERE ProductType='Finish Fabric Production' 
    AND CONVERT(date, Prodction_Date) BETWEEN convert(date, GETDATE()) AND convert(date, GETDATE())
    GROUP BY FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia
    
    UNION 
    
    SELECT FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia,
    0 AS OpeningQty, 0 AS 'PreIssueQty', 0 AS 'PreReceivedQty', SUM(Rcv_Qty) AS 'IssueQty'
    FROM Tbl_Production_IS
    WHERE ProductType='Finish Fabric Production' 
    AND CONVERT(date, Prodction_Date) < convert(date, GETDATE())
    GROUP BY FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, Dia
) y
GROUP BY  FileNo, Order_No, Style_No, Buyer_Name, Item_Code, [Item_Name], BatchNo, GSM, 
Dia
) z
WHERE fOpeningQty > 0 OR fReceivedQty > 0 OR fIssueQty > 0 OR fClosingQty > 0

这是我的原始 SQL 查询,我得到了我想要的结果。我是初级开发人员。我在整个网络上搜索这个问题,但从未找到任何预期的答案。任何人都可以帮助我如何通过 Laravel eloquent 或 DB Facade 获得预期结果。预先感谢。

laravel eloquent
1个回答
0
投票

这是您查询的更新版本:

use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

$today = Carbon::now();

$subquery = DB::table('Tbl_Production_RCV')
    ->selectRaw('FileNo, Order_No, Style_No, Buyer_Name, Item_Code, Item_Name, BatchNo, GSM, Dia,
                 SUM(Rcv_Qty) AS OpeningQty, 0 AS PreIssueQty, 0 AS PreReceivedQty, 0 AS IssueQty')
    ->where('ProductType', 'Finish Fabric Production')
    ->where('Prodction_Date', '<', $today->toDateString())
    ->groupBy('FileNo', 'Order_No', 'Style_No', 'Buyer_Name', 'Item_Code', 'Item_Name', 'BatchNo', 'GSM', 'Dia')
    ->unionAll(
        DB::table('Tbl_Production_IS')
            ->selectRaw('FileNo, Order_No, Style_No, Buyer_Name, Item_Code, Item_Name, BatchNo, GSM, Dia,
                         0 AS OpeningQty, SUM(Rcv_Qty) AS PreIssueQty, 0 AS PreReceivedQty, 0 AS IssueQty')
            ->where('ProductType', 'Finish Fabric Production')
            ->whereBetween('Prodction_Date', [$today->toDateString(), $today->toDateString()])
            ->groupBy('FileNo', 'Order_No', 'Style_No', 'Buyer_Name', 'Item_Code', 'Item_Name', 'BatchNo', 'GSM', 'Dia')
    )
    ->unionAll(
        DB::table('Tbl_Production_RCV')
            ->selectRaw('FileNo, Order_No, Style_No, Buyer_Name, Item_Code, Item_Name, BatchNo, GSM, Dia,
                         0 AS OpeningQty, 0 AS PreIssueQty, SUM(Rcv_Qty) AS PreReceivedQty, 0 AS IssueQty')
            ->where('ProductType', 'Finish Fabric Production')
            ->whereBetween('Prodction_Date', [$today->toDateString(), $today->toDateString()])
            ->groupBy('FileNo', 'Order_No', 'Style_No', 'Buyer_Name', 'Item_Code', 'Item_Name', 'BatchNo', 'GSM', 'Dia')
    )
    ->unionAll(
        DB::table('Tbl_Production_IS')
            ->selectRaw('FileNo, Order_No, Style_No, Buyer_Name, Item_Code, Item_Name, BatchNo, GSM, Dia,
                         0 AS OpeningQty, 0 AS PreIssueQty, 0 AS PreReceivedQty, SUM(Rcv_Qty) AS IssueQty')
            ->where('ProductType', 'Finish Fabric Production')
            ->where('Prodction_Date', '<', $today->toDateString())
            ->groupBy('FileNo', 'Order_No', 'Style_No', 'Buyer_Name', 'Item_Code', 'Item_Name', 'BatchNo', 'GSM', 'Dia')
    );

// Now, wrapping this subquery to perform final calculations
$finalQuery = DB::table(DB::raw("({$subquery->toSql()}) as sub"))
    ->selectRaw('FileNo, Order_No, Style_No, Buyer_Name, Item_Code, Item_Name, BatchNo, GSM, Dia,
                 SUM(OpeningQty) - SUM(IssueQty) AS fOpeningQty, SUM(PreReceivedQty) AS fReceivedQty, SUM(PreIssueQty) AS fIssueQty,
                 (SUM(OpeningQty) - SUM(IssueQty) - SUM(PreIssueQty) + SUM(PreReceivedQty)) AS fClosingQty')
    ->groupBy('FileNo', 'Order_No', 'Style_No', 'Buyer_Name', 'Item_Code', 'Item_Name', 'BatchNo', 'GSM', 'Dia')
    ->havingRaw('fOpeningQty > 0 OR fReceivedQty > 0 OR fIssueQty > 0 OR fClosingQty > 0')
    ->get();

$results = $finalQuery;
© www.soinside.com 2019 - 2024. All rights reserved.