mysql - How to subtract from two queries? - php

I have 2 queries from 2 tables(purchase_products & sales_products) and i want to subtract from first query quantity to second query quantity.
First Query:
$purchase_products = DB::table('purchase_products')
->join('products','products.id','purchase_products.product_id')
->select('products.product_name','purchase_products.purchase_price',DB::raw("SUM(purchase_products.quantity) as purchase_quantity"))
->groupby('purchase_products.product_id','purchase_products.purchase_price')
->get();
Second Query:
$sales_products = DB::table('sales_products')
->select('sales_products.purchase_price',DB::raw("SUM(sales_products.quantity) as sales_quantity"))
->groupby('sales_products.product_id','sales_products.purchase_price')
->get();
How do i subtract only quantity from second query and keep as it is of first query?
unique column: product_id & purchase_price

According to laravel documentation you can join two or more queries tables by joinSub, leftJoinSub, and rightJoinSub.
you can see example in
https://laravel.com/docs/8.x/queries#subquery-joins
for substration you can use joinSub

Related

How Propel Get Sum of a column

I want to get query Result from a table day wise sum. In propel How get two columns date and sum(rate)
I get all the table columns instead of selected columns,
$results = VoiceQuery::create()
->select(array('call_date', 'rated_units'))
->withColumn('call_date', 'call_date')
->withColumn('SUM(rated_units)', 'rated_units')
->filterByOriginatingCli('1300690045')
->filterByCallDate($week_ago_date, Criteria::GREATER_EQUAL)
->groupBy("call_date")
->limit(10);
I want only selected two columns but according to above query I get all the table columns data.
Try this:
VoiceQuery::create()
->withColumn('SUM(rated_units)', 'sum_rated_units')
->filterByOriginatingCli('1300690045')
->filterByCallDate($week_ago_date, Criteria::GREATER_EQUAL)
->groupBy("call_date")
->select(['call_date', 'sum_rated_units'])
->limit(10)
->find();

Laravel : Select and GroupBy SQL Query Error. Multiple Selects with groupBy? Is it Possible?

I have an 2 tables Ratings and Attributes , i pass the month and get all record of both users and count this , i have already count this record , and divided into groups by user_id , But in my attribute table i have a user name , and join this table to get attribute name , My query is that
$months = \Request::get('month');
$records = DB::table("ratings")
->select(DB::raw("SUM(ratings.attribute_score) as score"))
->whereRaw('MONTH(ratings.created_at) = '.$months)
->join('users','ratings.user_id','=','users.id')
->groupBy('ratings.user_id')
->get();
dd($records);
And get json like that
But i want to get name of the specific user with score but here multiple selects not working in this query how to use multiple select to get attributes table records? if i am using this query. Thanks Developers in advance
->select(DB::raw("SUM(ratings.attribute_score) as score , ratings.user_id , attributes.attributes_name"))
Using this the select not working here it always return the score Jason like show in the Sceenshot. How can i do to selects multiples using groupBy??
USE THIS TO GET USERNAME WITH SCORE COUNTS. Try This
$months = \Request::get('month');
$records = DB::table("ratings")
->select(DB::raw('SUM(ratings.attribute_score) as score, users.name as
user_name'))
->whereRaw('MONTH(ratings.created_at) = '.$months)
->join('users','ratings.user_id','=','users.id')
->groupBy('users.name')
->get();
return view('attribute.show' , compact('records'));

Yii2 : how to use distinct , groupby , sum and where conditions

I want execute below query in using Yii2 syntax selection query,
select distinct pfs_id, unitprice, sum(qty)
from order_items
where order_id= id
and item_farm_id = id
group By pfs_id
I tried several times but still not getting luck,
Please any one help.
Below query return you desire result as array.
$data=ModelName::find() //replace your model name here
->select('distinct(pfs_id),unitprice,sum(qty)')
->where(['order_id'=>id,'item_farm_id'=>id])
->groupBy('pfs_id')
->asArray->all();
You can also use alias:
$data=ModelName::find()
->select('distinct(pfs_id) as pfs_id,unitprice,sum(qty) as sum_of_qty')
->where(['order_id'=>id,'item_farm_id'=>id])
->groupBy('pfs_id')
->asArray->all();

eloquent query that finds top 5 records

I'm creating a query that gives me the top 5 countries with more visits, but my query isn't working.
My query I can limit, how would I count all record by country, to see the 5 countries more visited.
My query:
$top5 = DB::table('visits') ->select('ip','country', 'browser') ->groupBy('ip') ->get();
output example:
http://pastebin.com/wtu8CnL8
First you need to group your query by country, since that's what you're looking at. Then you need to count your results, order it by that number, and just take what you need.
$top5 = DB::table('visits')
->select('ip','country', 'browser', DB::raw("count(*) as total_visits"))
->groupBy('country')
->orderBy('total_visits','DESC')
->take(5);

Use selected MAX value of a table to join another table in single query

Can I use selected MAX value of a table to join another table in single query ??I am trying this in magento .Here is my code :
$collection = Mage::getResourceModel('merc/checkin_collection');
$collection->getSelect()->join(array("table1"=>"table1"),"main_table.id = table1.checkin_id",array('MAX(table1.checkout_id) as max_checkout'))
->joinleft(array("table2"=>"table2"),"table1.max_checkout = table2.id",array('table2.check_time as checkout_time'));
here I am selecting MAX(table1.checkout_id) as max_checkout from "table1" and using "max_checkout" for joining table2...this is not working for me ..even I have tried with main_table.max_checkout instead of table1.max_checkout for joining second table.If this is possible..Can anyone help me out??

Categories