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();
Related
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
I was working with a project where I want to get the count of values in a column. I will explain with an example.
I have a table column named name. Where it's values are A,B,C,D,A,D,B,A,C.
Now I want the output as
Count: A-3,B-2,C-2,D-2.
I have tried using group by and distinct. but both don't give me what I want. It all getting the total count of that item. In the code given below is the query I tried. There I want the count of particulars_id and the $public_page_id will be common for all particulars_id I am fetching. There will be a number of public_page_id in the table, and each will have some particulars_id under them.
$output = '';
$this->db->select('COUNT(service_appointment_details.particulars_id)
as count,particulars.particulars_name');
$this->db->from('particulars');
$this->db->group_by('particulars.particulars_id');
$this->db->where('particulars.public_page_id',$public_page_id);
$this->db-
>join('service_appointment_details','particulars.public_page_id =
service_appointment_details.public_page_id','right');
$this->db->where($where_date);
$query = $this->db->get();
Expected Result
Expected Result is (based on the above example)
Count: A-3,B-2,C-2,D-2.
Actual Result
But what I'm getting now is
Count: A-9,B-9,C-9,D-9.
I need to fetch count of each particulars_id under the given public_page_id
You should use count(*)
$this->db->select('COUNT(*) as count,particulars.particulars_name');
........
As pointed out by #scaisEdge, you need to use count(*) instead of count(service_appointment_details.particulars_id). By using count(service_appointment_details.particulars_id), you basically count the number of rows from your select which is not what you want.
Final snippet would be:
$output = '';
$this->db->select('COUNT(*) as count, particulars.particulars_name');
$this->db->from('particulars');
$this->db->group_by('particulars.particulars_name');
$this->db->where('particulars.public_page_id',$public_page_id);
$this->db->join('service_appointment_details','particulars.public_page_id = service_appointment_details.public_page_id','right');
$this->db->where($where_date);
$query = $this->db->get();
Whenever you want to count occurrence of a column values, you'd do
SELECT column_to_count, count(*) FROM table GROUP BY column_to_count
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'));
I have 6 Records in following Table
I Need to get Unique Leads id Columns Order by id DESC.
Following is my Query
Telecallerfirststep::WhereDate('created_at',date('Y-m-d'))->groupBy('leadsid')->OrderBy('id','DESC')->count();
Its Return Number of Records is : 1
Just Add distinct
Telecallerfirststep::WhereDate('created_at',date('Y-m-d'))-
>groupBy('leadsid')->OrderBy('id','DESC')->distinct()->count();
You should try this:
Telecallerfirststep::whereDate('created_at','=',date('Y-m-d'))->groupBy('leadsid')->count();
Use php count after fetching result
$reault = Telecallerfirststep::whereDate('created_at',date('Y-m-d'))->groupBy('leadsid')->OrderBy('id','DESC');
echo $count = count($reault);
I found similar issue MySql and SO
i am joining three tables and applying group by to pick a single distinct row of each type. when i join with table marketing_follow i have multiple entries and to pick one i am using group by . now i want to pick the entry with most recent date in follow up table . i've been tryn to get this done for quite some time now and really need some help
public function getRegionWiseUserDetails($array){
$this->db->select('mg.region_name, mu.contact_person, mu.contact_number, mu.status, mu.company_name, mu.user_type, mf.follow_up_date, mf.date, mu.user_id');
$this->db->from('marketing_users as mu');
$this->db->join('marketing_group as mg','mg.id = mu.region_id','left');
$this->db->join('marketing_follow_up as mf','mf.user_id_fk =
mu.user_id','left');
foreach($array as $condition){
$this->db->or_where('mg.id',$condition);
}
$this->db->order_by('mf.follow_up_date','asc');
$this->db->group_by('mf.user_id_fk');
$query = $this->db->get();
return $query->result_array();
}
You need to sort the date field in desc order. Also for asc we dont have to pass it as second paramter it defaults accept asc
$this->db->order_by('mf.follow_up_date','desc');