I have this problem in a query using laravel groupBy, it simply return a groupBy error. I have read the documentation about this but can't really figure it out. I also read the same problem pointing that it is because of postgreSQL that I need to include all the columns in grouBy clause. I tried it but still it doesn't return the distinct values. Please help me with this. Below is my code. Thanks a lot.
Controller function
public function index(){
$purchases = Purchase::groupBy('purchase_order_id')->get();
return view('purchases/purchases_crud', ['allPurchases' => $purchases]);
}
Table to query
Error
QueryException in Connection.php line 680:
SQLSTATE[42803]: Grouping error: 7 ERROR: column "purchases.id" must appear
in the GROUP BY clause or be used in an aggregate function
LINE 1: select * from "purchases" group by "purchase_order_id"
^ (SQL: select * from "purchases" group by "purchase_order_id")
There you have it add group by "purchases.id" or restrict the select to only the operates that are needed.
->select("purchase_order_id","purchases.id")
->groupBy("purchases.id") // add if possible
Agreggates for your case should mean something like ->select("sum(po_total)")
If we group by id, we get all results as id is unique, my mistake. You want something like this
DB::table("purchases")->select("purchase_order_id", DB:raw("sum(po_total)"))->groupBy("purchase_order_id")->get();
Rule of thumb is you either select a field with Sum() or have it on the group by
Related
I have two tables user_master and user_teams with common field user_name.
I want to join the table and get the team value group by teams
tried as
$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->groupBy('user_teams.team')
->pluck('user_teams.team')
->toArray();
by may values are duplicating.I'm using postgresql
since you didn't determine select fields ... the default will be '*'
this is why you are getting duplicated fields ...
just add :
->select('user_teams.team')
and i think that all.
i recommend not using group without aggregation ....
so my advice your query to like this:
$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->select('user_teams.team')->distinct()
->pluck('user_teams.team')
->toArray();
Sorry on my English. I add new condition to my query like this:
$query->andWhere(['or',
["<", "o.score", $score_operator],
["<", "d.score", $score_delivery],
["<", "SUM(sd.score)/count(DISTINCT sd.id)",$score_dishes]]);
Buth I got error:
General error: 1111 Invalid use of group function
Then I read that I must use having in 3-d row, but I don't know how to put "having" in "where" condition.
For condition/filter related to aggregation function you should use having and not where
$query->having("SUM(sd.score)/count(DISTINCT sd.id) < " .$score_dishes);
where work on table rows having work on selected result rows.
http://www.yiiframework.com/doc-2.0/yii-db-query.html#having()-detail
starting for 2.0.11 you could use also filteHaving in case you having http://www.yiiframework.com/doc-2.0/yii-db-query.html#filterHaving()-detail
I'm using Laravel with serverfireteam/panel and I need that the validation checks for a unique value in column.
I tested this:
$this->edit->add('nr','Nummer','text')->rule('required')->rule('unique:nr');
result Query:
SQL: select count(*) as aggregate from `nr` where `nr` = 22
and this:
$this->edit->add('nr', 'Nummer','text')->rule('required')->rule('unique:.kunden nr');
which results in:
(SQL: select count(*) as aggregate from `kunden nr` where `nr` = 22)
Is it possible to check for uniqueness with rules in serverfireteam/panel and how does it work?
I found the solution for the problem. It's similar Syntax used like in laravel's default Syntax https://laravel.com/docs/5.1/validation.
But here you have always to add the tablename:
->rule('unique:tablename,columnname')
for example:
$this->edit->add('nr', 'Nummer','text')->rule('required')->rule('unique:kunden,nr');
I already posted a question regarding this issue on another post but I want to try to be more specific here
So I have the following query here using codeigniter with a library from https://github.com/IgnitedDatatables/Ignited-Datatables/wiki/Function-Reference
<?php
public function GetQuery(){
$this->datatables->select('tablessc.Name As region,p.name As name,tabled.test As test,
MAX(CASE WHEN p.id= p.cid and flavor = 2 THEN \'Pass\' ELSE \'Fail\' end) as \'Pass\'
')
->from('drinks p');
$this->datatables->join('tableb ', 'tableb.id=p.pid','inner');
$this->datatables->join('tablec','tablec.TerritoryId=tablec.TerritoryId','inner');
$this->datatables->join('tabled','tablec.AccountId=tablec.AccountId','inner');
$this->datatables->join('tables','d.AccountId = tabless.AccountId ','inner');
$this->datatables->join('tablessc','tablesc.Code = p.region and sc.CodeGroup =\'Region\'','inner');
$this->datatables->where('region >','0');
$this->datatables->where('p.location','0');
$this->datatables->group_by('tablessc.Name');
$this->datatables->group_by('p.id');
$this->datatables->group_by('p.name');
$this->datatables->group_by('atabled.test');
echo $this->datatables->generate();
}
?>
Ok so if I run this query on microsoft sql I get no error and I get the results I want, but if i call this function from a page it will give me an error
Error Number: 42000
[Microsoft][SQL Server Native Client 10.0][SQL Server]Column 'drinks.region' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
SELECT * FROM drinks p INNER JOIN.....
Just to take a note here why is the error showing the sql doing a select *
I tried taking the max aggregate function out and it still doing the same error.
THe only way to get rid of this error is to take out the aggregate function MAX and the group by clause. Which is no the query I want.Any one else have experience this problem with the library? Thank you
In SQL server all columns used in the select column must appear in group_by clause unless the column is used in an aggregate function.
The 'get_total_results' function in datatable library produces 'SELECT * FROM table_name GROUP_BY column_name ' query. So the 'drinks.region' column which was not actually selected in your query is appearing in the error message.
As a fix try adding
if(count($this->group_by) > 0)
{
$this->ci->db->select($this->columns);
}
inside the get_total_results() function in Datatable.php file
We are deploying the RT index in our architecture. But we need some clarification and some difficulties faced during the deployment.
Schema defined in Index:
index logtable
{
type = rt
path = /usr/local/sphinx20/var/data/logtable
rt_attr_string = TransactionId
rt_attr_uint = CustomerId
rt_attr_timestamp = DateOfTransaction
rt_attr_string = CustomerFeedback
rt_field = TransactionType
}
Faced Problem
Question 1:
How we can get the count() query result in SPHINXQL. Because its important for us, based on customer count we have to take it to show in our application.
Example below,
Query - select count(*) from logtable where CustomerId='871';
In SphinxQL - We didnt get this result and getting the following error.ERROR 1064 (42000): index logtable: invalid schema: Count(*) or #count is queried, but not available in the schema.
Question 2:
i declared as a STRING attribute in conf for the field of "TransactionId", but i cant able to retrieve the records if that fields use in where condition.
Example below,
select * from logtable where TransactionId='TRA23454';
Following error i am getting,
ERROR 1064 (42000): sphinxql: syntax error, unexpected $undefined, expecting CONST_INT or CONST_FLOAT or '-' near '"TRA23454"'
Please help us to close these issues if knows.
Kumaran
select * from logtable where TransactionId='TRA23454';
Answer :
select * from logtable where MATCH('#TransactionId TRA23454')
In first example instead of count(*) you need to use 'show meta;' query after search query, it will contain total_count field.
select id from logtable where CustomerId='871';
show meta;
In the second example string attributes can't be used in WHERE, ORDER or GROUP clauses.
Actually you need to convert TransactionId into integer and use integer attribute. It is quite simple to do using crc32 mysql function.