I have a model named Product , it's attributes name, quantity
I tried to use ActiveRecord to group by name in postgresql database and it was not working
This is errors:
Product Load (53.6ms) SELECT "".* FROM "products" GROUP BY name
PG::GroupingError: ERROR: column "products.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "products".* FROM "products" GROUP BY name
^
=> #<Product::ActiveRecord_Relation:0x4d26424>
Thank you for any answer!
I would like to lead you to http://guides.rubyonrails.org/active_record_querying.html#group
Here is a example
For example, if you want to find a collection of the dates orders were created on:
Product.select('name').group("name")
Product.select('name, sku').group("name, sku")
Note you need a select
Related
i have a view object that contains the following attributes
studentId
courseId
enrollDate
notes
and I have added 2 attributes, which are
studentName
courseName
and I want to select their values from another table(student,course) based on an SQL query.
i have tried to make a default value :SQL
and wrote the following query
select Course.courseName from Course where StudentCourse.CourseId = Course.id
but it didn't work.
You can get their values from an SQL query. This may help. Or this. Or this.
The below query pulls the result on Account and uses two inner queries on child objects Monthly_cc__c and Yearly_cc__c. I would like to sort the result by Processing date of the monthly_cc__c. Please help
Select id,
(SELECT Country__c, Current_Month_Active__c,
Distributor__c,Personal_CC_MTD__c, Total_CC_MTD__c,
Processing_Date__c, Total_Active_CC_MTD__c,
Non_Manager_CC_MTD__c,Leadership_CC_MTD__c, Processing_Month__c,
Processing_Year__c, Global_Case_Credits__c,
Eagle_Manager_Global_New_CC__c, Chairmans_Bonus_Global_New_CC__c
FROM Account.Monthly_CCs__r
WHERE Processing_Month__c IN ( 9,8,7) AND Op_Company__c = 'GBR' ),
(SELECT Total_CC_YTD__c, Total_Active_CC_YTD__c, Non_Manager_CC_YTD__c,
Leadership_Qualification_CC_YTD__c,Operating_Company__c
FROM Account.Yearly_CCs__r
WHERE Operating_Company__c= 'GBR' AND Processing_Date__c =2016-01-01)
From Account where ID IN
('001d000001VpPcyAAF','001d000001liZH4AAM','001d000001Q5sflAAB' )
Based on the fact that you want to sort in the SUB query you have two options:
Create a formula field with the field you want to Sort Account__r.Monthly_CC__c then simply add an ORDER BY SOQL clause with the new formula field
Add ORDER BY SOQL clause with Account__r.Monthly_CC__c
I am trying to fetch picklist records from sales force using soql. My Table name is Industry__c its having a field called Industry_Group__c whose data type is picklist. How can I fetch the records of the Industry_group__c picklist.
I have tried the below queries but it did not work.
select Industry_Group__c from Industry__c
First get the DurableId from the FieldDefinition:
Select DurableId
From FieldDefinition
where EntityDefinition.DeveloperName='Industry__c'
and DataType = 'Picklist'
and DeveloperName = 'Industry_Group__c'
Then you can run the following query to get out the picklist values, replacing the EntityParticleId with the DurableId from the last query:
SELECT DurableId, EntityParticleId, IsActive, IsDefaultValue, Label, ValidFor, Value
FROM PicklistValueInfo
WHERE EntityParticleId = 'Account.00N1a00000ZZSAS'
Actually the field is not accessible to user .. Please check the field level security whether it has a read/Write access to system Administrator and then try to access it..
I am working on an SQL query which should group by a column bidBroker and return all the columns in the table.
I tried it using the following query
select Product,
Term,
BidBroker,
BidVolume,
BidCP,
Bid,
Offer,
OfferCP,
OfferVolume,
OfferBroker,
ProductID,
TermID
from canadiancrudes
group by BidBroker
The above query threw me an error as follows
Column 'canadiancrudes.Product' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
Is there any other way which returns all the data grouping by bidBroker without changing the order of data coming from CanadadianCrudes?
First if you are going to agregate, you should learn about agregate functions.
Then grouping becomes much more obvious.
I think you should explain what you are trying to accomplish here, because I suspect that you are trying to SORT bu Bidbroker, rather than grouping.
If you mean you want to sort by BidBroker, you can use:
SELECT Product,Term,BidBroker,BidVolume,BidCP,Bid,Offer,OfferCP,OfferVolume,OfferBroker,ProductID,TermID
FROM canadiancrudes
ORDER BY BidBroker
If you want to GROUP BY, and give example-data you can use:
SELECT c1.Product,c1.Term,c1.BidBroker,c1.BidVolume,c1.BidCP,c1.Bid,c1.Offer,c1.OfferCP,c1.OfferVolume,c1.OfferBroker,c1.ProductID,c1.TermID
FROM canadiancrudes c1
WHERE c1.YOURPRIMARYKEY IN (
select MIN(c2.YOURPRIMARYKEY) from canadiancrudes c2 group by c2.BidBroker
)
Replace YOURPRIMARYKEY with your column with your row-unique id.
As others have said, don't use "group by" if you don't want to aggregate something. If you do want to aggregate by one column but include others as well, consider researching "partition."
I have two tables Patient_tbl and Consult_tbl in MS access (with the fields shown below). The first one is used to record patient info, and the second one (Consult_tbl) is used to record patient visits. They are related in a one-to-many relationship using a Patient_id field.
What I need to do is to count the visitors based on gender for a given period of time, based on the consult table using Patient_id. I don’t know how to do it. Would you please help?
Patient_tbl has the following fields:
{Patient_id
P_add
P_tel
P_gender
Other fields}
Consult_tbl has the following fields:
{Consult_id
Patient_id
C_date
C_ref
Other fields}
Join the tables on Patient_id, add a where clause for the date range and use the count() function and group by P_gender:
select p.P_gender, count(*) as "count"
from Patient_tbl p
inner join Consult_tbl c on p.Patient_id = c.Patient_id
where c.C_date >= '2015-01-10'
and c.C_date <= '2015-01-20'
group by p.P_gender