I'm building a small stocks/portfolio tracker and I'm having some trouble retrieving and counting cells of my transactions. Below you can find the dummy data in my transactions table of my database.
// Transactions table
ID | Name | Symbol | Currency | amount_bought | amount_sold | price | commission | bought | portfolio_id
_____________________________________________________________________________________________________________________
1 | Ocugen Inc | FRA.2H51 | EUR | 55 | NULL | 0.29 | 7.51 | 1 | 1
2 | Tesla, Inc | NASDAQ.TSLA | EUR | 5 | NULL | 654.87 | 4.23 | 1 | 1
3 | Ocugen Inc | FRA.2H51 | EUR | NULL | 40 | 1.31 | 7.55 | 0 | 1
I'm using a boolean named bought (final column) in order to "identify" my transaction as either being a sold or bought stock. Next, I want to retrieve all my transactions using my portfolio_id and group them on their name, symbol and currency in order to output the following:
// Desired result:
Name | Symbol | Currency | amount_current | commission_total | bought_total | sales_total
_____________________________________________________________________________________________________
Ocugen Inc | FRA.2H51 | EUR | 15 | 15.06 | 15.95 | 52.4
Tesla, Inc | NASDAQ.TSLA | EUR | 5 | 4.23 | 3274.35 | 0
Currently my code works exactly like I wanted, except for how my rows are being grouped. Because I'm using a case, in order to calculate the total amount of buys and sales of a single stock, I'm forced to include the bought column into my groupBy(). Therefore my results are also grouped on the bought in addition to the name, symbol and currency:
// Current result:
name | symbol | currency | amount_current | commission_total | bought_total | sales_total
_____________________________________________________________________________________________________
Ocugen Inc | FRA.2H51 | EUR | 15 | 7.51 | 15.95 | 0
Tesla, Inc | NASDAQ.TSLA | EUR | 5 | 4.23 | 3274.35 | 0
Ocugen Inc | FRA.2H51 | EUR | NULL | 7.55 | 0 | 52.4
Below you can find my code that generates the result above.
$transactions = Transaction::where('portfolio_id', $portfolio->id)
->groupBy('name', 'symbol', 'currency', 'bought')
->select([
'name',
'symbol',
'currency',
DB::raw('sum(amount_bought) - sum(amount_sold) as amount_current'),
DB::raw('sum(commission) AS commission_total'),
DB::raw('case when bought = 1 then sum(price) * sum(amount_bought) else 0 end as bought_total'),
DB::raw('case when bought = 0 then sum(price) * sum(amount_sold) else 0 end as sales_total')
])
->get();
How can I group my transactions on the stock name, symbol and currency and calculate their totals without grouping them on the bought column?
I don't really know Laravel, but you should be able to use:
$transactions = Transaction::where('portfolio_id', $portfolio->id)
->groupBy('name', 'symbol', 'currency')
->select([
'name',
'symbol',
'currency',
DB::raw('sum(amount_bought) - sum(amount_sold) as amount_current'),
DB::raw('sum(commission) AS commission_total'),
DB::raw('sum(case when bought = 1 then price * amount_bought else 0 end as bought_total'),
DB::raw('sum(case when bought = 0 then price * amount_sold) else 0 end) as sales_total')
])
->get();
That is, remove bought from the group by and make the case the argument to sum().
This is my product table.I want to store customer_id from 1000 and save by +1 how much data i stored
id | customer_id | name |
1 | 1000 | ABC |
2 | 1001 | Tripathi |
3 | 1002 | Leaptrig |
4 | 1003 | Falcon |
5 | 1004 | Savillan |
6 | 1005 | Molt |
7 | 1006 | Falt |
My Controller
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=1000+($lastProduct+1);
}
$product->save();
But In this code,Customer id i increment by 1000 2001,3002 like this. so how should i avoid it ?
id | customer_id | name |
1 | 1000 | ABC |
2 | 2001 | Tripathi |
3 | 3002 | Leaptrig |
4 | 4003 | Falcon |
5 | 5004 | Savillan |
6 | 6005 | Molt |
7 | 7006 | Falt |
You can try this :-
$lastProduct=Product::pluck('customer_id')->last();
$product=new Product();
$product->name=$request->name;
if($lastProduct){
$product->customer_id=$lastProduct+1;
}
$product->save();
We have a table which we record our students' attendances in. We need to find out which students missed n number of contacts in a row.
Here is the attendance table structure with example records
----------------------------------------------------
| id | student_id | class_id | checkedin_time |
--------------------------------------------------
| 1 | 1 | 1 | null |
| 2 | 1 | 2 | 2019-07-09 10:30 |
| 3 | 1 | 3 | null |
| 4 | 1 | 4 | null |
| 5 | 1 | 5 | 2019-07-12 12:00 |
----------------------------------------------------
What I'm looking for is a code that show me records with id 3 and 4 (two subsequent missed contacts) grouped by student_id
This was my starting point:
$attendances = \App\Attendance::where('checked_time' , null)->get();
$attendances->groupBy('student_id')
Having a hive table with age column consisting of age of persons.
Have to count and display the top 3 age categories.
Ex: whether below 10, 10-15, 15-20, 20-25, 25-30, ...
Which age category appears more.
Please suggest me a query to do this.
select case
when age <= 10 then '0-10'
else concat_ws
(
'-'
,cast(floor(age/5)*5 as string)
,cast((floor(age/5)+1)*5 as string)
)
end as age_group
,count(*) as cnt
from mytable
group by 1
order by cnt desc
limit 3
;
You might need to set this parameter:
set hive.groupby.orderby.position.alias=true;
Demo
with mytable as
(
select floor(rand()*100) as age
from (select 1) x lateral view explode(split(space(100),' ')) pe
)
select case
when age <= 10 then '0-10'
else concat_ws('-',cast(floor(age/5)*5 as string),cast((floor(age/5)+1)*5 as string))
end as age_group
,count(*) as cnt
,sort_array(collect_list(age)) as age_list
from mytable
group by 1
order by cnt desc
;
+-----------+-----+------------------------------+
| age_group | cnt | age_list |
+-----------+-----+------------------------------+
| 0-10 | 9 | [0,0,1,3,3,6,8,9,10] |
| 25-30 | 9 | [26,26,28,28,28,28,29,29,29] |
| 55-60 | 8 | [55,55,56,57,57,57,58,58] |
| 35-40 | 7 | [35,35,36,36,37,38,39] |
| 80-85 | 7 | [80,80,81,82,82,82,84] |
| 30-35 | 6 | [31,32,32,32,33,34] |
| 70-75 | 6 | [70,70,71,71,72,73] |
| 65-70 | 6 | [65,67,67,68,68,69] |
| 50-55 | 6 | [51,53,53,53,53,54] |
| 45-50 | 5 | [45,45,48,48,49] |
| 85-90 | 5 | [85,86,87,87,89] |
| 75-80 | 5 | [76,77,78,79,79] |
| 20-25 | 5 | [20,20,21,22,22] |
| 15-20 | 5 | [17,17,17,18,19] |
| 10-15 | 4 | [11,12,12,14] |
| 95-100 | 4 | [95,95,96,99] |
| 40-45 | 3 | [41,44,44] |
| 90-95 | 1 | [93] |
+-----------+-----+------------------------------+
Hi I want query from 3 table
user:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Denny |
| 2 | Agus |
| 3 | Dini |
| 4 | Angel |
+----+-----------+
History_Education
+----+-----------+-------------+
| id | userId | educationId |
+----+-----------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
+----+-----------+-------------+
Education
+----+-----------+----------+
| id | level | Name |
+----+-----------+----------+
| 1 | 1 | SD |
| 2 | 2 | SMP |
| 3 | 3 | SMA |
| 4 | 4 | S1 |
+----+-----------+----------+
How to query with laravel Eloquent to get the latest user education order by Level DESC
expected:
+----+-----------+----------------------+
| id | Name | Latest_Education |
+----+-----------+----------------------+
| 1 | Denny | SMP |
| 2 | Agus | SMP |
| 3 | Dini | - |
| 4 | Angel | - |
+----+-----------+----------------------+
In normal query:
select id,name ,(select E.name from education E inner join History_eductaion HE on E.id = HE.education_id where HE.userId =U.id limit 1 order by E.level DESC )latest_education from USER U
How to translate to laravel eloquent?
The quick answer is use the query builder - your query needed to be altered slightly to match the table names and columns as listed in your question:
$result = DB::table('user')
->select([
'id',
'name',
DB::raw("(select E.name from Education E inner join History_Education HE on E.id = HE.educationId where HE.userId = user.id order by E.level DESC limit 1) as latest_education")
])
->get();