I have a table with some same id so I display them groupBy option with sum. I have many column like id, item_name, rate, quantity, value Here is my code.
$orders = Order::where('item_id', $id)
->where('delivery_status', 1)
->groupBy('item_id')
->selectRaw('sum(quantity) as quantity, sum(value) as value, item_id')
->pluck('item_id','quantity', 'value');
By this I got three column with id and quantity and value with sum. My problem is that I also want to display other column like item_name, rate, delivery_status etc. and I don't want to sum them like rate. How can I display them? I can groupBy also with item_name and it works but can not group with rate because other items may also have same rate.
Don't use pluck, as that will only return the columns you have specified. Instead, try using get to return all the table columns:
$orders = Order::where([
'item_id' => $id,
'delivery_status' => 1
])
->groupBy('item_id')
->selectRaw('orders.*, sum(quantity) as quantity, sum(value) as value, item_id')
->get();
Related
I have a product table with id, product_name and added_date.
I want to display product_name and added_date but I have the following condition to display the results
For product_name column: if multiple records with the same product_name are registered, the latest id value should be displayed.
For added_date column: multiple records with the same added_date are registered, the latest id value should be displayed.
I tried the following but it didnot gave result as exprected:
$products = $this->Products->find()
->group([
'Products.product_name',
'Products.added_date',
])
->order([
'Product.added_date DESC',
'Product.id DESC'
])
->all();
I have 3 tables:
Shifts {vehicled_id, user_id, subdriver_id)
Users {user_id, subdriver_id)
Vehicles {vehicle_id}
I want to group Shifts when I join with Users and Vehicles but some shifts only have user_id or subdriver_id and some have both.
This is the picture of shifts tables.
The result I want is a groupBy of vehicle_id and:
If it has only user_id or subdriver_id it will return {user_id, vehicle_id, shift_id (it can be an array)}
or {subdriver_id, vehicle_id, shift_id (it can be an array)}.
If it has both user and subdriver it will return {user_id, vehicle_id, shift_id(it can be array)}
shift_id is based on user_id and vehicle_id or subdriver_id and vehicle_id.
How can I groupBy and return a result like this?
You have to use CASE statement which goes through multiple conditions and return a value based on respective condition.
DB::table('shifts')
->select(DB::raw('CASE WHEN shifts.user_id IS NULL THEN shifts.subdriver_id WHEN shifts.subdriver_id IS NULL THEN shifts.user_id ELSE shifts.user_id END as user_id, shifts.vehicle_id, GROUP_CONCAT(shifts.id) as shift_id'))
->leftJoin('users as u1','u1.id','shifts.user_id')
->leftJoin('users as u2','u2.subdriver_id','shifts.subdriver_id')
->groupBy('shifts.vehicle_id')
->get();
I have 2 tables Bag(cart) and products. I am saving quantity added by user in bad table and sale_price in products table. I need to get sum of bag.quantity * products.sale_price for each item. I am using query builder to get sum. How can i get that
I am able to use join to get list of all attributes
$items = DB::table('bag')->where('bag.user_id' , $user_id)->where('bag.order_id', 0)->join('products','products.id','=','bag.product_id')->get();
api link:
https://apptest.factory2homes.com/api/get-bag-products/3
I need to multiply quantity and sale_price for each unique product_id and then take total sum of it
you can use selectRaw to do such thing:
$items = DB::table('bag')->where('bag.user_id' , $user_id)
->where('bag.order_id', 0)
->join('products','products.id','=','bag.product_id')
->selectRaw('*, (bag.quantity * products.sale_price) as totalPrice')
->get();
and to get sum of all, you can use sum:
$sum=$items->sum('totalPrice');
order
id
currency_code
ex_rate_with_base
status
total_in_base_currency
status
currency
currency_code
symbol
currency_name
currency_value
ex_rate_with_base
status
order_list
id
order_id
product_code
qty
unit_price_in_base_currency
status
I want the following output:
Here I want to multiply the ex_rate_with_base field of order table with unit_price of order_list table using eloquent.
I think you can try this:
DB::table('order')
->select(DB::raw('(order.ex_rate_with_base * order_list.unit_price_in_base_currency) as orderRate'))
->leftjoin('order_list','order_list.order_id','=','order.id)
->get();
Hope this work for you !!!
If you have Order and OrderList Eloquent Models, you can try something like this from a controller.
YourOrderController.php
$order= Order::find($orderId);
$orderList = OrderList::where('order_id',$orderId)->get();
$calculatedPrice = $orderList->unit_price_in_base_currency * $order->ex_rate_with_base;
To preface this question - I know I'm missing something really obvious here but it is Friday afternoon!
I'm trying to COUNT to number of times certain values appear within the same column and produce a table of the results.
Here's my code so far:
MYSQL
SELECT COUNT(order_status) as printed
FROM orders WHERE order_status = 'Printed Order'
UNION
SELECT COUNT(order_status) as charged
FROM orders WHERE order_status = 'Charged Order'
UNION
SELECT COUNT(order_status) as exchanged
FROM orders WHERE order_status = 'Exchanged Order'
UNION
SELECT COUNT(order_status) as refunded
FROM orders WHERE order_status = 'Refunded Order'
UNION
SELECT COUNT(order_status) as cancelled
FROM orders WHERE order_status = 'Cancelled Order'
GROUP BY order_status
Result of the above query
printed
-------
224
19190
593
2618
2899
The code is producing the correct figures, however, I would prefer the result to look as follows:
Desired result
printed - 224
charged - 19190
exchanged - 593
refunded - 2618
cancelled -2899
This way I can easily reference them via associative array call i.e. $order_status['printed']
Any help would be great.
Add a column specifying the type. The easiest way is to use group by:
select order_status, count(*)
from orders o
group by order_status;
If you use PDO, set the fetch mode to create associative arrays by default by:
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
try:
SELECT order_status, COUNT(order_status) as printed
FROM orders WHERE order_status = 'Printed Order'
GROUP BY order_status