Get count of only required column data from a laravel controller output - laravel-4

$selected_subcats2 = EstablishmnetSubcategories::Join('establishments','establishments.id','=','establishment_subcategories.establishment_id')->where('establishment_subcategories.establishment_id',$establishment_id)->get(array('establishment_subcategories.status as status'));
Result:[{"status":0},{"status":1},{"status":1},{"status":0},{"status":0}]
This is my controller condition and output, in the above output i want to get the count of status whose value is only zero . i dont want count of all , how its possible to get only status zero count, please help ...thank you

try this
Add where('status',0) in your Query
it will work

Related

Laravel - Check if value is equal to any record in collection

I have a collection of records.
$pastSessions = Sessions::where('id', $id)->pluck('event_start_time');
This has 12 records.
I'm trying to check if a variable is equal to any of these records, but it's only checking the last one.
if($application->appointment == $pastSessions)
How do I check is it equal to any of the records.
This is all in my controller
You can use contains with name of key and value like this.
$pastSessions->contains('appointment', $application->appointment)

Using MAX as a measure only on one column

I have a table with thes headders;
Id ProcessName CurrentStatus Result ProcessOwner ProcessObjectCount ProcessObjectCurrentItem Errors RunBy HostSystem ConnectedWithAccount Step OData__DCDateModified Notes ID.1 Date ErrorCount HasErrors
I want to use MAX on OData__DCDateModified so i get the latest list of the ProcessName.
I add ProcessName in to a table, create a mesure for MAX('TABLENAME'[OData__DCDateModified]) ant it works but when i add ID or any other value in that i want displayed the MAX stops working as I hoped.
Any thoughs?
It sounds like you want to get the MAX OData__DCDateModified for each ProcessName, ignoring any other filter contexts. In order to do this you need to use CALCULATE combined with ALLEXCEPT.
MaxProcess = CALCULATE(MAX(TABLENAME[OData__DCDateModified]),ALLEXCEPT(TABLENAME,TABLENAME[ProcessName]))
When you pass ALLEXCEPT(TABLENAME,TABLENAME[ProcessName]) to CALCULATE, you are telling the formula to ignore every other filter context except the context on ProcessName

Extract more rows from a left join with Laravel

I have to extract the rows where the created_at is inside the week. Unfortunately, only one line is extracted from me and no more lines as I expected. Why?
Query:
$scadenze = DB::table('processi')
->leftJoin('scadenze', 'processi.id', '=', 'scadenze.processo_id')
->where('responsabile',$utente->id)
->whereNotIn('scadenze.stato', [4,5])
->whereBetween('scadenze.termine_stimato',[\Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
->avg('tempistica');
This query extract just one row, but in reality many more lines should be extracted.
Because ->avg('tempistica'); return average value from all your rows in this query, i.e. return just one value.
Solution:
I was wrong to use the avg with sum function. The rows were extracted correctly but instead of being added (by timing) an average was made. Thank you all for your help.

Way to fetch rown in batch from mysql using hibernate from mentioned row onward

I have one query in service class
PageRequest page = new PageRequest(pageNo,batchSize , new Sort(new Order(Direction.ASC, "Id")));
Page pPage = this.pRepository.findByStatusAndParentPId( Status.PENDING, -1, page);
where batchSize=500
In repository we have following code:
#Query("select p from Part p where p.succeedOn IS NOT NULL and p.Status=? and p.parentId=?")
Page<Payment> findByStatusAndParentId( String status, Integer parentId, Pageable p);
Now flow is like this, i want to fetch 500 rows everytime whose status is pending and then i need to process it and need to change the status to success. So using Pageable is giving me wrong result because , suppose it fetch first 500 rows whose status is pending , it processed it and changed the status to success, now it will again make sql query and will fetch row from 501 to 1000 , but actually row 1 to 500 also have status pending as older processed rows status changed to success, so they will not be covered in sql query.
Now to solve this i want to do pagenation with from last row of last fetched Id , say last time it fetched from 100 to 600, then i want to give 601 as argument and want to fetch all rows onward..... hopefully i am able to explain my answer. Limit in query do not work in JPA.
Thanks
I think you can just keep fetching page 1. Assuming each batch is processed in its own transaction, then by the time you process the second batch, page 1 should give you the next 500 un-processed rows

SOQL - single row per each group

I have the following SOQL query to display List of ABCs in my Page block table.
Public List<ABC__c> getABC(){
List<ABC__c> ListABC = [Select WB1__c, WB2__c, WB3__c, Number, tentative__c, Actual__c, PrepTime__c, Forecast__c from ABC__c ORDER BY WB3__c];
return ListABC;
}
As you can see in the above image, WB3 has number of records for A, B and C. But I want to display only 1 record for each WB3 group based on Actual__c. Only latest Actual__c must be displayed for each WB3 Group.
i.e., Ideally I want to display only 3 rows(one each for A,B,C) in this example.
For this, I have used GROUPBY and displayed the result using AggregateResults. Here is the result.
I got the Latest Actual Date for each WB3 as shown above. But the Tentative date is not corresponding to it. The Tentative Date is also the MAX in the list.
Here is the code I used
public List<SiteMonitoringOverview> getSPM(){
AggregateResult[] AgR = [Select WB_3__c, MAX(Tentaive_Date__c) dtTentativeDate , MAX(Actual_Date__c) LatestCDate FROM Site_progress_Monitoring__c GROUP BY WBS_3__c];
if(AgR.size()>0){
for(AggregateResult SalesList : AgR){
CustSumList.add(new SiteMonitoringOverview(String.ValueOf(SalesList.ge​t('WB_3__c')), String.valueOf(SalesList.get('dtTentativeDate')), String.valueOF(SalesList.get('LatestCDate')) ));
}
}
return CustSumList;
}
I am forced to use MAX() for tentative date. I want the corresponding Tentative date of the MAX Actual Date. Not the Max Tentative Date.
For group A, the Tentative Date of Max Actual Date is 12/09/2012. But it is displaying the MAX tentative date: 27/02/2013. It should display 12/09/2012. This is because I am using MAX(Tentative_Date__c) in my code. Every column in the SOQL query must be either GROUPED or AGGREGATED. That's weird.
How do I get the required 3 rows in this example?
Any suggestions? Any different approach (looping within in groups)? how?
Just ran into this issue myself. The solution I came up with only works if you want the oldest or newest record from each grouping. Unfortunately it probably won't work in your case. I'll still leave this here incase it does happen to help someone searching for a solution to this issue.
AggregateResult[] groupedResults = [Select Max(Id), WBS_3__c FROM Site_progress_Monitoring__c GROUP BY WBS_3__c];
Calling MAX or MIN on the Id will let you get 1 record per group condition. You can then query other information. I my case I just need 1 record from each group and didn't really care which one it was.

Resources