Subtraction with aggregation using 2 tables - laravel

My Laravel query is not working properly. but MySQL query works fine
Laravel Query :
$data = DB::table(DB::raw('select (sum(case when type="credit" then amount else -amount end)) - (select sum(amount) from total) from report'))
Please refer mysql query in sqlfiidle : http://sqlfiddle.com/#!9/2d0343/9

Rather than try rewrite your MySQL query, let's simplify it and make it more eloquent.
Assuming $data is the overall balance and your model for the report table is called Report then the below should achieve what you are after:
$data = Report::where('type', 'credit')->sum('amount') - Report::where('type', 'debit')->sum('amount');
This will give you the sum of all your credits, minus the sum of all your debits.

Related

How can I write the below pure SQL statement in laravel without using DB:raw

I'm trying to show the total price according to month. I know how to retrieve the data with pure SQL statements but I don't know a way to apply it inside Laravel. And also I don't want to use DB::raw(). I need help!! Below is the pure SQL statement.
SELECT month(dt.created_at) as Month,SUM(dp.price) as Total_Price
FROM datapack_transactions dt
INNER JOIN datapack_packages dp ON dt.package_id=dp.id
GROUP BY month(dt.created_at);
Below is the result of the above pure SQL statement.
I want to use the Laravel Eloquent instead of using DB::raw().
Try this query:
$orders = datapack_transactions::select(
DB::raw('month(datapack_transactions.created_at) as Month'),
DB::raw("SUM(datapack_packages.price) as Total_Price")
)
->join('datapack_packages','datapack_packages.id','=','datapack_transactions.package_id')
->groupBy('datapack_transactions.created_at')
->get();
//Change model and columns as per yours

Laravel Paginate with OrderBy

When querying a product code table I have the following
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10);
This works fine on the initial load of 10 records but when a subsequent call is made for page 2 I get the following error
Incorrect syntax near 'offset'. (SQL: select * from [stock_records] order by [stk_physical] desc offset 10 rows fetch next 10 rows only)
I'm using Laravel 8.0 with SQL
You should append query string to the pagination like this
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10);
$results->appends(["order_by" => "stk_physical"]);
This will append the &order_by=stk_physical to each link in the view and you can also use withQueryString() to take in consideration query string in future pagination like this
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10)->withQueryString();

Slow MySQL query in Laravel, but very fast in phpMyAdmin

I have worked with one query in Laravel, i need to optimize it. At first i have such query
DB::select(DB::raw("select `discounts`.`id`, `discounts`.`discount` from `discounts` left join `discount_companies_criteria` on `discounts`.`id` = `discount_companies_criteria`.`discount_id` and `discount_companies_criteria`.`is_active` = 1 where `discount_companies_criteria`.`company_id` is null and `discounts`.`status` = 1 group by `discounts`.`id`")) order by discounts.discount ASC;
laravel debugbar shows - 579ms
in phpmyadmin this query run by 503ms
than i have refactored code and get modified query (just remove sorting)
DB::select(DB::raw("select `discounts`.`id`, `discounts`.`discount` from `discounts` left join `discount_companies_criteria` on `discounts`.`id` = `discount_companies_criteria`.`discount_id` and `discount_companies_criteria`.`is_active` = 1 where `discount_companies_criteria`.`company_id` is null and `discounts`.`status` = 1 group by `discounts`.`id`")) ;
laravel debugbar shows - 579ms
in phpmyadmin this query run by 3ms
Can somebody explain, how i can get the same speed in laravel as in phpMyadmin?
Table discounts - 1900 rows
Table discount_companies_criteria - 7500000 rows
General tip, don't ever run query without limit "select, delete, or update", in your case use pagination
First the following query has no limit, which obviously would have bad performance depends on the size of the data you're dealing with:
SELECT `discounts`.`id`,
`discounts`.`discount`
FROM `discounts`
LEFT JOIN `discount_companies_criteria` ON `discounts`.`id` = `discount_companies_criteria`.`discount_id`
AND `discount_companies_criteria`.`is_active` = 1
WHERE `discount_companies_criteria`.`company_id` IS NULL
AND `discounts`.`status` = 1
GROUP BY `discounts`.`id`
The reason why this query or the other one are faster in phpMyAdmin is because phpMyAdmin by default set limit to the query.

Need guidance on how to build a Laravel database query

In Laravel 6.18 I'm trying to figure out how to recreate the following Postgres query.
with data as (
select date_trunc('month', purchase_date) as x_month, date_trunc('year', purchase_date) AS x_year,
sum (retail_value) AS "retail_value_sum"
from coins
where user_email = 'user#email.com' and sold = 0
group by x_month, x_year
order by x_month asc, x_year asc
)
select x_month, x_year, sum (retail_value_sum) over (order by x_month asc, x_year asc rows between unbounded preceding and current row)
from data
I know how to build the main part of the query
$value_of_all_purchases_not_sold = DB::table('coins')
->select(DB::raw('date_trunc(\'month\', purchase_date) AS x_month, date_trunc(\'year\', purchase_date) AS x_year, sum(retail_value) as purchase_price_sum'))
->where('user_email', '=', auth()->user()->email)
->where('sold', '=', 0)
->groupBy('x_month', 'x_year')
->orderBy('x_month', 'asc')
->orderBy('x_year', 'asc')
->get();
but how do you build out the with data as ( and the second select?
I need the data to be cumulative and I'd rather do the calculation in the DB than in PHP.
Laravel doesn't have built-in method(s) for common table expression. You may use a third party package such as this - it has a very good documentation. If you don't want to use an external library, then you need use query builder's select method with bindings such as
$results = DB::select('your-query', ['your', 'bindings']);
return Coin::hydrate($results); // if you want them as collection of Coin instance.

Eloquent query alternative

How can I write the following in Laravel's Eloquent?
SELECT *
FROM
( SELECT real_estate.property_id,
real_estate.amount_offered,
payee.summa
FROM real_estate
LEFT JOIN
(SELECT property_id,
SUM(amount) AS summa
FROM payments
GROUP BY property_id) payee ON payee.property_id = real_estate.property_id ) yoot
WHERE summa = 0.05 * amount_offered
Been on this for a while now and really can't get around it. Lemme explain the whole cause for the panic.
I have two tables, one for property and another for payments made on those properties. Now at any given time I will like to query for what properties have been paid for to a certain percentage hence the 0.05 which reps 5%. As it is the query works but I need an Eloquent alternative for it. Thanks
Anywhere you have subqueries in your SQL you'll need to use DB::raw with Eloquent. In this case you have a big subquery for the FROM statement, so the easiest way would be to do this:
DB::table(
DB::raw('SELECT real_estate.property_id, real_estate.amount_offered, payee.summa FROM real_estate LEFT JOIN (SELECT property_id, SUM(amount) AS summa FROM payments GROUP BY property_id) payee ON payee.property_id = real_estate.property_id)')
)
->where('summa', DB::raw('0.05 * amount_offered'))->get();
Notice I used DB::raw for the WHERE statment value as well. That's because you are doing a multiplication using a column name, and the value would otherwise be quoted as a string.
If you want to go a step further and build each subquery using Eloquent, then convert it to an SQL string and injecting it using DB::raw, you can do this:
$joinQuery = DB::table('payments')
->select('property_id', 'SUM(amount) AS summa')
->groupBy('property_id')
->toSql();
$tableQuery = DB::table('real_estate')
->select('real_estate.property_id', 'real_estate.amount_offered', 'payee.summa')
->leftJoin(DB::raw('(' . $joinQuery . ')'), function ($join)
{
$join->on('payee.property_id', '=', 'real_estate.property_id');
})
->toSql();
DB::table(DB::raw('(' . $tableQuery . ')'))->where('summa', DB::raw('0.05 * amount_offered'))->get();
In this case, the second approach doesn't have any benefits over the first, except perhaps that it's more readable. However, building subqueries using Eloquent, does have it's benefitfs when you'd need to bind any variable values to the query (such as conditions), because the query will be correctly built and escaped by Eloquent and you would not be prone to SQL injection.

Resources