how to use GroupBy clause in doctrine2 - doctrine-orm

How to convert the following query into doctrine2 query
with using method createQuery() or createQueryBuilder()
select customer,sum(marks) from BlogBundle:Customers group by customer

$qb->select('customer', 'SUM(marks)')->from('BlogBundle:Customers', 'customer')->groupBy('customer.id');

Related

How to convert this SQL query to Django Queryset?

I have this query which selects values from two different tables and used array agg over matched IDs how can I get same results using the queryset. Thank you!
select
sf.id_s2_users ,
array_agg(sp.id)
from
s2_followers sf
left join s2_post sp on
sp.id_s2_users = sf.id_s2_users1
where
sp.id_s2_post_status = 1
and sf.id_s2_user_status = 1
group by
sf.id_s2_users
You can run raw SQL queries with Django's ORM if that's what you wanted. You don't have to change your query in that case, you can check documentation here.

Is there any way to convert this into django orm?

I needed to get all the rows in the table1 even if it is not existing in table2 and display it as zero. I got it using raw sql query but in django ORM i am getting the values existing only in table2. The only difference on my django orm is that iI am using inner join while in the raw sql query I am using left join. Is there any way to achieve this or should I use raw sql query? Thanks.
Django ORM:
total=ApplicantInfo.objects.select_related('source_type').values('source_type__source_type').annotate(total_count=Count('source_type'))
OUTPUT OF DJANGO ORM IN RAW SQL:
SELECT "applicant_sourcetype"."source_type", COUNT("applicant_applicantinfo"."source_type_id") AS "total_count" FROM "applicant_applicantinfo" INNER JOIN "applicant_sourcetype" ON ("applicant_applicantinfo"."source_type_id" = "applicant_sourcetype"."id") GROUP BY "applicant_sourcetype"."source_type"
RAW SQL:
SELECT source.source_type, count(info.source_type_id) as total_counts from applicant_sourcetype as source LEFT JOIN applicant_applicantinfo as info ON source.id = info.source_type_id GROUP BY source.id
You cannot query on ApplicantInfo if you want a left join with it. Query on SourceType instead:
qs = (SourceType.objects
.values('source_type')
.annotate(cnt=Count('applicantinfo'))
.values('source_type', 'cnt')
)
Since you haven't posted the models, you may need to adapt the field names to make it work.

Convert mysql query with date_format in doctrine dql

How can I convert this query in doctrine?
select distinct date_format(data_fine, '%d/%m/%Y')
from calendario
Thanks for your help.
Use Doctrine’s NativeSQL functions to write an SQL statement
Write a User-Defined Function so that Doctrine recognises DATE_FORMAT when written in DQL and can successfully transform it into
SQL.
From http://www.uvd.co.uk/blog/labs/using-mysqls-date_format-in-doctrine-2-0/
You need define a name for this row:
select distinct date_format(data_fine, '%d/%m/%Y') AS <name> from calendario
Example:
select distinct date_format(data_fine, '%d/%m/%Y') AS day from calendario

Birt Report Grouping

I have 2 columns: fltrte_P1_Plt_Per_Id_Fk (Pilot) and fltrte_P2_Plt_Per_Id_Fk (Co-Pilot).
When displaying data in the report I need to group based pilot name. He may be pilot or co-pilot.
It should come in same group. How can this grouping be achieved in a Birt report?
I suggest amending your query from:
select fltrte_P1_Plt_Per_Id_Fk, fltrte_P2_Plt_Per_Id_Fk, ... from flight_Log_Table
to:
select fltrte_P1_Plt_Per_Id_Fk as group_By, ... from flight_Log_Table
union all
select fltrte_P2_Plt_Per_Id_Fk as group_By, ... from flight_Log_Table
then amend your report to group on the new group_By field in the query.
Create a new column that combines the two in your SQL Query.
ISNULL ( fltrte_P1_Plt_Per_Id_Fk, fltrte_P2_Plt_Per_Id_Fk ) as 'Pilot'
If there is a value for P1 (Pilot) it will be in the new field 'Pilot', otherwise P2 (Co-Pilot) will the new field 'Pilot'
This solution works in BIRT 4.2 using a 2008 R2 database.
Add the GROUP BY clause Group by Pilot, Co-Pilot to the end of your SQL query.

query builder select the id from leftJoin

I have a select field that fetch from an entity
and I would like to customize completely my select by choosing the table the id is picked from
(here I would like to select t.id instead of tl.id as the select value)
return $er->createQueryBuilder('tl')
->addSelect('l')
->addSelect('t')
->leftJoin('tl.lang', 'l')
->leftJoin('tl.type', 't')
->where('l.isDefault = 1')
->orderBy('tl.name', 'ASC');
Due to my tables, I can't simply fetch the table t, I have to use tl
Your query is not according to the syntax defined in Doctrine 2 QueryBuilder: http://www.doctrine-project.org/docs/orm/2.0/en/reference/query-builder.html
Your query might work in Doctrine 1.2 but in Doctrine 2 you should build your query according to the syntax defined in the link I posted above.
For example ->addSelect('l') is not being used in Doctrine 2 anymore. It has become ->add('select', 'l').
You don't have to set different alias for your column. It'll be hydrated as column of the related entity.