Laravel Query. Obtain in Eloquent only totals > 0 - sql

I have this query:
public static function totalByAgent(int $agentId)
{
return PropertyListing::select(
DB::raw('SUM(property_listing.rental) as rental'),
DB::raw('SUM(property_listing.sale) as sale'),
'property_category.name as category_name',
'property_category.id as category_id'
)->join(
'property_category',
'property_category.id',
'property_listing.category_id'
)->where('agent_id', $agentId)->groupBy('property_category.id')->get();
}
With this query I obtain the sum of properties for sale and sum of properties to rent group by property categories. But If some property have only properties for sale and 0 to rent I obtain 0 in sum of rents.
I tried adding:
->having('sale', '>', 0)->get()
after groupBy. this hides the rents if they had.
Any idea?
Best regards

Finally this query works for me:
return Property::published()->select(DB::raw('SUM(property.rental) as rental'),
DB::raw('SUM(property.sale) as sale'),
'property_category.name as category_name',
'property_category.id as category_id')
->join('property_category', 'property_category.id', 'property.category_id')
->whereExists(function ($query) use ($agentId){
$query->select('property_listing.agent_id')
->from('property_listing')
->whereRaw('property.id = property_listing.property_id')
->where('property_listing.agent_id', $agentId);
})->published()
->groupBy('property_category.id')->get();

Related

Transforming Raw Sql to Laravel equolent

I have written this SQL code
SELECT drugs.*, COUNT(*) as 'views' from drugs INNER JOIN drug_seen on drugs.id = drug_seen.drug_id GROUP BY drugs.id order by views ASC
And now I am trying to write in in the Laravel equolent but I am facing some troubles.
This is what I have tried
$drugs = Drug::select(DB::raw('drugs.*,count(*) as views'))
->join('drug_seen', 'drugs.id', 'drug_seen.drug.id')
->groupBy('drug.id')->orderByRaw('views');
I am having errors like column not found i think the code is not written properly
Drug class
class Drug extends Model
{
use HasFactory;
use SoftDeletes;
...
...
...
public function drugVisits()
{
return $this->hasMany(DrugSeen::class);
}
Hop this will solve your problem.
$drugs = Drug::with('drugVisits')->get();
$drugs->count(); //for total records in drugs table.
You have typo error in join instead on drug_id you use drug.id
Try this:
$drugs = Drug::select(DB::raw('drugs.*,count(*) as views'))
->join('drug_seen', 'drugs.id', 'drug_seen.drug_id')
->groupBy('drugs.id')->orderByRaw('views');
}
As soon as you use join() you're leaving Eloquent and entering Query\Builder, losing the benefits of Model configurations in the process. And with() eager-loads aren't the answer, if you're looking to filter the results by both tables. What you want is whereHas().
Also, as far as your grouping and count manipulation there, I think you're looking more for Collection handling than SQL groups.
$drugModel = app(Drugs::class);
$results = $drugModel->whereHas('drugVisits')->with('drugVisits')->get();
$organizedResults = $results
->groupBy($drugModel->getKey())
->sortyBy(function (Drugs $drugRecord) {
return $drugRecord->drugVisits->count();
});
If you want to have a 'views' property that carries the count in the root-level element, it would look like this:
$drugModel = app(Drugs::class);
$results = $drugModel->whereHas('drugVisits')->with('drugVisits')->get();
$organizedResults = $results
->groupBy($drugModel->getKey())
->map(function (Drugs $drugRecord) {
$drugRecord->views = $drugRecord->drugVisits->count();
return $drugRecord;
});
->sortyBy('views');

using sum with relation Laravel Eloquent ORM

i have Model Material which has
protected $with = ['costPrices'];
public function costPrices(){
return $this->hasMany(CostPrice::class);
}
the table of cost_prices has multiple quantity
//create_cost_prices_table
Schema::create('cost_prices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('material_id');
$table->unsignedBigInteger('supplier_id');
$table->integer('quantity');
$table->timestamps();
});
i want to get (insufficient materials ) in another way select all materials form materials table where sum of all quantities of cost_prices table < 3 and get me the resulted materials not the count
You have 2 options:
You could fix your option with whereHas() by adding select and groupBy:
$materials = Material
::whereHas('costPrices', function($q) {
$q
->select('meterial_id')
->groupBy('material_id')
->havingRaw('SUM(quantity) > 4');
})
->get();
Or use subquery:
For laravel 6.x:
$materials = Material
::addSelect([
'cost_prices_sum' => CostPrice::selectRaw('sum(quantity)')->whereColumn('material_id', 'material.id')
])
->having('cost_prices_sum', '>', 4)
->get();
For laravel 5.x:
$materials = Material
::selectRaw('carts.*, (select sum(quantity) from cost_prices where cost_prices.matrerial_id = meterials.id) as cost_prices_sum')
->having('cost_prices_sum', '>', 4)
->get();
UPDATE: option with whereHas() is faster on a large number of rows.

Get hierarchies valuesName mdx

I created a hierarchies like this photo below:
http://i.stack.imgur.com/ErOXQ.png
On MDX query
Select [AccSetting].[UserN].[UserN] ON 1
FROM [Olap_report]
Where [AccSetting].[AgID].[115]
This query return all UserN are children of AgID 115. but I want to return exactly UserN of AgID =115?
Think I understand now - the name is is a property of the member.
Create a custom measure that returns the property.
WITH
MEMBER [Measures].[NameColumn] AS
[AccSetting].[AgID].CURRENTMEMBER.Member_Name
MEMBER [Measures].[NameColumn2] AS
[AccSetting].[AgID].CURRENTMEMBER.Name
SELECT
{
[Measures].[NameColumn]
,[Measures].[NameColumn2]
} ON 0,
[AccSetting].[AgID].[115] ON 1
FROM [Olap_report]
Reference to msdn:
https://msdn.microsoft.com/en-us/library/ms145583.aspx
Edit
First check the member exists:
SELECT
{
[Measures].[<add measure here>],
[Measures].[<add measure here>]
} ON 0,
{
[AccSetting].[AgID].[115],
[AccSetting].[AgID].[113]
} ON 1
FROM [Olap_report]

Group By Sum Linq to SQL in C#

Really stuck with Linq to SQL grouping and summing, have searched everywhere but I don't understand enough to apply other solutions to my own.
I have a view in my database called view_ProjectTimeSummary, this has the following fields:
string_UserDescription
string_ProjectDescription
datetime_Date
double_Hours
I have a method which accepts a to and from date parameter and first creates this List<>:
List<view_UserTimeSummary> view_UserTimeSummaryToReturn =
(from linqtable_UserTimeSummaryView
in datacontext_UserTimeSummary.GetTable<view_UserTimeSummary>()
where linqtable_UserTimeSummaryView.datetime_Week <= datetime_To
&& linqtable_UserTimeSummaryView.datetime_Week >= datetime_From
select linqtable_UserTimeSummaryView).ToList<view_UserTimeSummary>();
Before returning the List (to be used as a datasource for a datagridview) I filter the string_UserDescription field using a parameter of the same name:
if (string_UserDescription != "")
{
view_UserTimeSummaryToReturn =
(from c in view_UserTimeSummaryToReturn
where c.string_UserDescription == string_UserDescription
select c).ToList<view_UserTimeSummary>();
}
return view_UserTimeSummaryToReturn;
How do I manipulate the resulting List<> to show the sum of the field double_Hours for that user and project between the to and from date parameters (and not separate entries for each date)?
e.g. a List<> with the following fields:
string_UserDescription
string_ProjectDescription
double_SumOfHoursBetweenToAndFromDate
Am I right that this would mean I would have to return a different type of List<> (since it has less fields than the view_UserTimeSummary)?
I have read that to get the sum it's something like 'group / by / into b' but don't understand how this syntax works from looking at other solutions... Can someone please help me?
Thanks
Steve
Start out by defining a class to hold the result:
public class GroupedRow
{
public string UserDescription {get;set;}
public string ProjectDescription {get;set;}
public double SumOfHoursBetweenToAndFromDate {get;set;}
}
Since you've already applied filtering, the only thing left to do is group.
List<GroupedRow> result =
(
from row in source
group row by new { row.UserDescription, row.ProjectDescription } into g
select new GroupedRow()
{
UserDescription = g.Key.UserDescription,
ProjectDescription = g.Key.ProjectDescription,
SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
}
).ToList();
(or the other syntax)
List<GroupedRow> result = source
.GroupBy(row => new {row.UserDescription, row.ProjectDescription })
.Select(g => new GroupedRow()
{
UserDescription = g.Key.UserDescription,
ProjectDescription = g.Key.ProjectDescription,
SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
})
.ToList();

codeigniter change complex query into active record

I have a codeigniter app.
My active record syntax works perfectly and is:
function get_as_09($q){
$this->db->select('m3');
$this->db->where('ProductCode', $q);
$query = $this->db->get('ProductList');
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row['m3'])); //build an array
}
return $row_set;
}
}
This is effectively
select 'm3' from 'ProductList' where ProductCode='$1'
What I need to do is convert the below query into an active record type query and return it to the controller as per above active record syntax:
select length from
(SELECT
[Length]
,CONCAT(([width]*1000),([thickness]*1000),REPLACE([ProductCode],concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options
FROM [dbo].[dbo].[ProductList]) as p
where options='25100cr' order by length
I picture something like below but this does not work.
$this->db->select(length);
$this->db->from(SELECT [Length],CONCAT(([width]*1000),([thickness]*1000),REPLACE[ProductCode],concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options
FROM [dbo].[dbo].[ProductList]);
$this->db->where(options, $q);
$this->db->order(length, desc);
Help appreciated as always. Thanks again.
You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now subquery writing in available And now here is your query with active record
$select = array(
'Length'
'CONCAT(([width]*1000)',
'thickness * 1000',
'REPLACE(ProductCode, concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options'
);
$this->db->select($select);
$this->db->from('ProductList');
$Subquery = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->select('length');
$this->db->from("($Subquery)");
$this->db->where('options','25100cr');
$this->db->order_by('length');
And the thing is done. Cheers!!!
Note : While using sub queries you must use
$this->db->from('myTable')
instead of
$this->db->get('myTable')
which runs the query.
Source