laravel search results grouping - mysql

I have a car rental website built with Laravel and when I search for a car I want the results to be grouped by brand for example (mercedes, ford, bmw). and when I click on "mercedes" for example then it has to display just " mercedes" cars in another page.
In other word I don't want to display all cars in one page. In first page I want grouped results. And when I click on "mercdes" then a new get request will be sent to get only "mercedes" cars in separate page.
I used groupBy() collection helper to group the results and display it in the first page. But the problem is the second step. I thought about using the same database query as the first except adding constraint for example (where brand = mercedes) to get only mercedes cars.
How can I achieve it ?

Laravel has a special formula for that is query builder . You can easily make your query by using this query builder. you can make a query by using the following query
$users = DB::table('cars')
->groupBy('brand')
->get();
you can see the full documentation of query builder Laravel Query Builder Documentation

Related

How to write a calculated field using Microsoft Access expression builder?

I'm trying to write a query that will calculate "Shares short" divided by "Shares floating". See my table below. I've tried the following expression, but I just get a big fat error.
Expr1: [Shares]![SharesType]="Shares short"/[Shares]![SharesType]="Shares floating"
I've also tried looking at Microsoft's expression tutorials (and the linked tutorials) but they're too general.
I find expression builder more confusing than helpful and don't use it.
If you are trying to do an aggregate query, click the Sigma icon on query designer tab and build in the design grid. Switch to SQL view to view statement which should be like:
SELECT CompanyID, Sum(IIf([SharesType]="s",[Shares],Null))/Sum(IIf([SharesType]="f",[Shares],Null)) AS Expr1
FROM Shares
GROUP BY CompanyID;
This worked.
SELECT Companies.CompanyName, Sum(IIf([SharesType]="Shares short",[Shares],Null))/Sum(IIf([SharesType]="Shares floating",[Shares],Null)) AS [Short as % of Float]
FROM Companies INNER JOIN Shares ON Companies.CompanyID = Shares.[Company ID]
GROUP BY Companies.CompanyName;
So my report now looks like this.

How to make a table on Access using "Count" and Age Range?

I downloaded a record of all the people on the first fleet from this website. http://firstfleet.uow.edu.au/download.html
I downloaded the Excel and imported it into Access. In the document, certain people’s age is not known so they are simply put as -1.
I am trying to make this table but I am struggling.
I’ve tried doing it by creating a table but that was too hard so I decided to create a query instead. I am using “Crosstab” but failing to use it successfully.
When I do try to sub the -1 for unknown:
Messages like this pop up:
How do we add the table in but sub the -1 for unknown while keeping the layout the same? I am hard stuck at a place where I cannot even view how my query looks.
How do I even make the layout the same as the picture shown above?
UPDATE:
Calculate the age groups with Switch() function and use that calculated field for CROSSTAB RowColumn. I renamed the Age field in table from the original import name to shorten the expression.
TRANSFORM Count(Convicts.ID) AS CountOfID
SELECT Switch([Age]=-1,"Unknown",[Age]<15,"10-14",[Age]<20,"15-19",[Age]<25,"20-24",[Age]<30,"25-29",[Age]<35,"30-34",[Age]<40,"35-39",[Age]<45,"40-44",[Age]<50,"45-49",[Age]<60,"50-59",[Age]<100,"60-99") AS AgeGrp
FROM Convicts
GROUP BY Switch([Age]=-1,"Unknown",[Age]<15,"10-14",[Age]<20,"15-19",[Age]<25,"20-24",[Age]<30,"25-29",[Age]<35,"30-34",[Age]<40,"35-39",[Age]<45,"40-44",[Age]<50,"45-49",[Age]<60,"50-59",[Age]<100,"60-99")
PIVOT Convicts.Gender;
The total row is added by clicking the Sigma(Totals) icon on the ribbon when query is in Datasheet view.
A simpler calculation will generate more groups:
TRANSFORM Count(Convicts.ID) AS CountOfID
SELECT Partition([Age],0,100,5) AS AgeGrp
FROM Convicts
GROUP BY Partition([Age],0,100,5)
PIVOT Convicts.Gender;
But a little adjustment to the SELECT clause will get same output as the first:
SELECT IIf([Age]=-1,"Unknown",IIf([Age]<50,Partition([Age],0,50,5),IIf([Age]<60,"50:59","60:99"))) AS AgeGrp
Could add a field in table and use one of those expressions in UPDATE action SQL to add calculated AgeGrp, which would simplify the CROSSTAB and other queries needing that group identifier.

How to build a query in order to get not expired records in Yii2

I have a model in my project which has several fields like id, name, due_year, due_quarter, etc. Here is my table
The issue is that I want to get that records where the field "due_year" is greater than 2017 and at the same time, the field "due_quarter" must be less than 4. In simple words, I am trying to get unexpired records. I tried to build a query like this:
$rows = Model::find()
->andFilterWhere(['>=', 'due_year', date('Y')])
->andFilterWhere(['<', 'due_quarter', 4])
->all();
but unfortunately i got expired rows as well.
Thank you in advance

IIF statement to return 1 or all based on criteria

I'm building a form in Access to return a list of projects with different completion states [project_state]. On the form, the user can select to view All projects or projects assigned to a particular team. Then user can select in a combo box [Combo100PStatus] on that same form to filter these results by project state. Onclick, a query is run to return the results. In the query, I used the expression builder with the following code:
IIf([Forms]![Main]![Sub].[Form]![Combo100PStatus]="All","Like '*'",[Forms]![Main]![Sub].[Form]![Combo100PStatus])
The second half of the code works -- I can get the results to return the correct projects when one state is selected from the [Combo100PStatus] box, but is there a way to get all rows returned when "All" is selected?
The criteria operator (=, <>, LIKE, etc) cannot be dynamic.
Like IIf([Forms]![Main]![Sub].[Form]![Combo100PStatus]="All", "", [Forms]![Main]![Sub].[Form]![Combo100PStatus]) & "*"

Use Top n on category group in chart

I have a report showing a table showing details, it has about 380 rows.
I want to add a simple bar chart using the same dataset.
In the chart is one value and one group: value = sum(amount) and category group = description order by sum(amount).
This is working fine, but chart is showing about 120 bars, because of 120 distinct descriptions. I only want to display the top n.
Can this be done in reporting services itself or do I need to do this is a query?
I found the answer myself.
There is a filter operator 'Top N' which can be used. It seems the field used in expression doesn't matter.
However it seems Reporting services first performs the topN en than sort the remaining data. So I had to make a second dataset anyway.