Doctrine2 Limit the rows to sum in a query - doctrine-orm

I have a query that return a sum, but I want to limit the number of rows to query, but is not working.
This is my query:
$qb = $this->createQueryBuilder('result')
->select('SUM(result.generalPoints) AS generalPoints, SUM(result.coefficient) AS coefficient')
->join('result.inscription', 'inscription', Join::WITH, 'inscription.user = :user')
->join('inscription.race', 'race')
->join('inscription.category', 'category', Join::WITH, 'category.generalRanking = true')
->join('race.event', 'event')
->join('event.competitionSeason', 'competitionSeason', Join::WITH, 'competitionSeason = :competitionSeason')
->orderBy('generalPoints', 'DESC')
->addOrderBy('coefficient', 'DESC')
->setParameter('competitionSeason', $competitionSeason)
->setParameter('user', $user);
if (isset($limit)) {
$qb->setMaxResults($limit);
}
return $qb->getQuery()->getOneOrNullResult();
Some idea ?
Thanks

As sum is an aggregation function its result is one row, the limit statement is applied afterwards. You need create subquery producing multiple rows with generalPoints and coefficient, limit number of rows in this subquery and use the aggregation function in the wrapping query.

Related

advance filter expression in dynamodb

I couldn't get any solution yet that's why questioning...
I have a table where two fields are categoryID_A, categoryID_B. I have some category ids like 'df23a5', '234za2', 'k0f9e3'. Need to get filtered out rows from that table including eighter the categoryID_A or categoryID_B matches with those ids.
...
FilterExpression: 'createdAt >= :time_within AND createdAt <= :from_time AND (categoryID_A IN (:m1,:m2,:m3) OR categoryID_B IN (:m1,:m2,:m3))',
ExpressionAttributeValues: {
':from_time': '2022-10-10T02:44:12.481Z',
':time_within': '2022-11-11T02:44:12.481Z',
':m1': 'df23a5',
':m2': '234za2',
':m3': 'k0f9e3'
}
...

Run SUM(*) on Supabase

I'm starting with Supabase and would like to understand how I can pass the SUM() aggregator in a SELECT.
I noticed that for COUNT we use:
const { data, error, range, count } = supabase
.from('table')
.select('*', { count: 'exact' })
Is there anything similar to SUM that I haven't noticed?
My query is this:
select
"GE_PRODUCTS",
sum("GE_QUANTITY") as Quantity,
sum("GE_SALEVALUE") as Revenue
from "GE_SELLS"
where "GE_ENTERPRISE" = 'G.E.'
and "DELETED" <> '*'
group by "GE_PRODUCTS"
order by Revenue desc
limit 3;
Your best bet is to put this into a PostgreSQL function and call it from Supabase using .rpc():
CREATE OR REPLACE FUNCTION get_my_sums()
RETURNS TABLE (
"GE_PRODUCTS" TEXT,
Quantity NUMBER,
Revenue NUMBER
) AS $$
DECLARE
var_r record;
BEGIN
RETURN QUERY
select
"GE_PRODUCTS",
sum("GE_QUANTITY") as Quantity,
sum("GE_SALEVALUE") as Revenue
from "GE_SELLS"
where "GE_ENTERPRISE" = 'G.E.'
and "DELETED" <> '*'
group by "GE_PRODUCTS"
order by Revenue desc
limit 3;
END; $$
LANGUAGE 'plpgsql';

How to remove null rows from MDX query results

How can I remove the null row from my MDX query results?
Here is the query I'm currently working with
select
non empty
{
[Measures].[Average Trips Per Day]
,[Measures].[Calories Burned]
,[Measures].[Carbon Offset]
,[Measures].[Median Distance]
,[Measures].[Median Duration]
,[Measures].[Rider Trips]
,[Measures].[Rides Per Bike Per Day]
,[Measures].[Total Distance]
,[Measures].[Total Riders]
,[Measures].[Total Trip Duration in Minutes]
,[Measures].[Total Members]
} on columns
,
non empty
{
(
[Promotion].[Promotion Code Name].children
)
} on rows
from [BCycle]
where ([Program].[Program Name].&[Madison B-cycle])
;results
This is not a null value however it is one of the children of [Promotion].[Promotion Code Name].Children.
You can exclude that particular value from children using the EXCEPT keyword of MDx.
Example query:
//This query shows the number of orders for all products,
//with the exception of Components, which are not
//sold.
SELECT
[Date].[Month of Year].Children ON COLUMNS,
Except
([Product].[Product Categories].[All].Children ,
{[Product].[Product Categories].[Components]}
) ON ROWS
FROM
[Adventure Works]
WHERE
([Measures].[Order Quantity])
Reference -> https://learn.microsoft.com/en-us/sql/mdx/except-mdx-function?view=sql-server-2017

Doctrine2 ODM Count() does not count integer values

i m using Zendframework2 and using count function to count the number of values, it fails when i try to count the integer values
$q = $dm->createQueryBuilder('Admin\Document\Institution')
->field('id')->notEqual($id)
->field('coreid')->equals($post['coreid']);
$coreid = $q->getQuery()->execute()->count();
does count function works for integer valeues in Doctrine2 ODM?
If you change your query slightly then possibly get your count value.
$q = $dm->createQueryBuilder('Admin\Document\Institution')
->select('count(id) as id')
->where('id !=:id AND coreid != :coreid')
->setParameters(array('id'=> $id,'coreid'=>$coreid);
$coreid = $q->getQuery()->execute();

CDaoRecordSet select all from column

How can i store all the records of a column in a CDaoRecordSet? I've tried this, but will only return the first record of that column:
rs.Open(dbOpenSnapshot, "SELECT Numar_inmatriculare FROM Masini");
short nFields = rs.GetFieldCount();//returns 1
If i make a "SELECT count(*) AS Numar_inmatriculare FROM Masini" and use rs.GetFieldValue(0) it returns me 13, the correct number of records.
GetFieldCount returns the number of columns in your resultset.
To iterate through the records (=rows), you have to call MoveNext until IsEOF() returns true.
rs.Open(dbOpenSnapshot, "SELECT Numar_inmatriculare FROM Masini");
while(!rs.IsEOF())
{
// do something
rs.MoveNext();
}