Doctrine GROUPBY with SELECT MAX - group-by

Here is my table :
id | place | date | valid
1 | ONE | 10/12/2019 | true
2 | ONE | 11/12/2019 | true
3 | ONE | 12/12/2019 | true
4 | ONE | 13/12/2019 | false
5 | ONE | 14/12/2019 | false
6 | TWO | 10/12/2019 | true
7 | TWO | 11/12/2019 | true
8 | TWO | 12/12/2019 | true
9 | TWO | 13/12/2019 | true
10 | TWO | 14/12/2019 | false
11 | OTH | 10/12/2019 | true
12 | OTH | 11/12/2019 | true
13 | OTH | 12/12/2019 | true
14 | OTH | 13/12/2019 | true
15 | OTH | 14/12/2019 | true
I'm trying with doctrine and the QueryBuilder to get the id's of my table.
I need to get the id's of the MAX date by place where valid is equal to "true".
With the data's displayed, I want to get this result :
3 | ONE | 12/12/2019 | true
9 | TWO | 13/12/2019 | true
15 | OTH | 14/12/2019 | true
I tried some query builder but I can not get the results I wanted.
$qb = $this->createQueryBuilder("o");
$qb->select('o.id, o.place');
$qb->addSelect($qb->expr()->max('o.date').' AS maxDate');
$qb->andWhere($qb->expr()->eq('o.valid', true));
$qb->groupBy('o.place');
I also tried with a subquery but I have a problem to group the place, get the max date and return the ID 3 / 9 and 15.
How can I get the max date and group my lines by place please ?
Thanks

Related

Listing duplicated entities based on two fields with django orm

My objective is getting all orders with duplicated order_number by location, that has checkout = true at least one time. But I'm having issues to translate this to Django ORM.
example of data:
| id | order_number | location_id | checkout |
|----|--------------|-------------|----------|
| 1 | 1 | 1 | true |
| 2 | 1 | 1 | true |
| 3 | 1 | 1 | false |
| 4 | 2 | 1 | true |
| 5 | 1 | 2 | true |
| 6 | 2 | 2 | false |
select count(*), order_number, location_id from orders where checkout = true group by location_id, order_number having count(*) > 1;
the expectation
| count | order_number | location_id |
|-------|--------------|-------------|
| 2 | 1 | 1 |
I already tried this, but it's not working as expected
>>> Order.objects.filter(checkout=True).values_list('order_number', 'location_id').annotate(count_order_number=Count("order_number")).filter(count_order_number__gt=1)
<QuerySet []>
I'm using
Django=3.2
postgresql

View Rows as columns in postgres SQL without using cross Tab as Cross Tab is provioing the excepted results

I am using Postgres 9.6. and i have a result set like this:
employee Name|collegeName | Date |attendance
-------------|------------|----------|-----------
employee1 |college1 |2020-05-01| true
employee1 |college2 |2020-05-01| false
employee2 |college3 |2020-05-01| true
employee3 |college4 |2020-05-02| true
employee4 |college5 |2020-05-02| false
employee5 |college1 |2020-05-03| true
employee6 |college3 |2020-05-03| false
My desired result is as follows:
employee Name|collegeName | 2020-05-01 | 2020-05-02 | 2020-05-03
-------------|------------|------------|------------|-----------
employee1 |college1 | true | |
employee1 | college2 | false | |
employee2 | college3 | true | |
employee3 | college4 | | true |
employee4 |college5 | | false |
employee5 | college1 | | | true
employee6 | college3 | | |false
tried using cross tab but couldn't get the desired Result. please help.

Group rows where there is at least one column with true value

I have a table like this
+-----+-------------------+------+-------+-------+-------+---+
| Row | email | year | month | flag1 | flag2 | |
+-----+-------------------+------+-------+-------+-------+---+
| 1 | user1#example.com | 2018 | 1 | true | true | |
| 2 | user1#example.com | 2018 | 1 | false | true | |
| 3 | user1#example.com | 2018 | 1 | true | true | |
| 4 | user2#example.com | 2018 | 1 | false | false | |
| 5 | user2#example.com | 2018 | 1 | false | false | |
| 6 | user2#example.com | 2018 | 1 | false | false | |
| 7 | user3#example.com | 2018 | 1 | true | false | |
| 8 | user3#example.com | 2018 | 1 | true | false | |
| 9 | user3#example.com | 2018 | 1 | false | false | |
+-----+-------------------+------+-------+-------+-------+---+
which can be generated with this statement
#standardSQL
WITH table AS (
SELECT "user1#example.com" as email, 2018 as year, 1 as month, TRUE AS flag1, TRUE as flag2
UNION ALL
SELECT "user1#example.com",2018,1,FALSE,TRUE
UNION ALL
SELECT "user1#example.com",2018,1,TRUE,TRUE
UNION ALL
SELECT "user2#example.com",2018,1,FALSE,FALSE
UNION ALL
SELECT "user2#example.com",2018,1,FALSE,FALSE
UNION ALL
SELECT "user2#example.com",2018,1,FALSE,FALSE
UNION ALL
SELECT "user3#example.com",2018,1,TRUE,FALSE
UNION ALL
SELECT "user3#example.com",2018,1,TRUE,FALSE
UNION ALL
SELECT "user3#example.com",2018,1,FALSE,FALSE
)
Grouping by email,year,month, the output table require to have true value (for each of the two flag columns), if in the grouped data there is at least ONE row with true value
The resulting table should be this one
+-----+-------------------+------+-------+-------+-------+---+
| Row | email | year | month | flag1 | flag2 | |
+-----+-------------------+------+-------+-------+-------+---+
| 1 | user1#example.com | 2018 | 1 | true | true | |
| 2 | user2#example.com | 2018 | 1 | false | false | |
| 3 | user3#example.com | 2018 | 1 | true | false | |
+-----+-------------------+------+-------+-------+-------+---+
I started grouping all the flags by the first 3 column, but now I'm now stuck to determine if there is at least one true value inside each array
SELECT email,
year,
month,
ARRAY_AGG(flag1) as flag1,
ARRAY_AGG(flag2) as flag2
FROM table
GROUP BY 1,2,3
#standardSQL
SELECT email,
year,
month,
LOGICAL_OR(flag1) AS flag1,
LOGICAL_OR(flag2) AS flag2
FROM table
GROUP BY 1,2,3

Spotfire - Calculate average only if there are minimum 3 values

I want to create a cross table in Spotfire where in which Average is calculated only when there are at least 3 values. If there are no values or less than 3 values the average should be blank.
+-------+-----+---------+
| Month | Age | Average |
+-------+-----+---------+
| 1 | 10 | |
| 2 | 11 | |
| 3 | 2 | 7.7 |
| 4 | | |
| 5 | 13 | |
| 6 | 14 | |
| 7 | | |
| 8 | 19 | |
| 9 | 20 | |
| 10 | 21 | 20 |
+-------+-----+---------+
If I'm understanding you correctly, you want to group by Month, and then have something like this as your aggregation:
If(Count()>2,Avg([Age]),null) as [AverageAge_3Min]

Conditional sum formula in Crystal reports

I need a help to do sum in Crystal reports:
Here is the sample data:
Seq | Name | Score | used |Multiply|
1 | Name 1 | 60 | True | Yes |
2 | Name 2 | 70 | False | No |
3 | Name 3 | 56 | False | No |
4 | Name 4 | 45 | True | No |
5 | Name 5 | 55 | True | Yes |
6 | Name 6 | 20 | True | No |
7 | Name 7 | 80 | False | No |
8 | Name 8 | 75 | False | No |
9 | Name 9 | 90 | False | No |
10 | Name 10| 40 | True | No |
11 | Name 11| 60 | False | No |
12 | Name 12| 50 | True | No |
I need to calculate sum of TOP 4 rows (Where score is higher) and "Used"= true and if "multiply" = yes then score multiply by 2. and display in group header.
(Row1 x 2) + Row4 + (Row5 x 2)+ Row 12
Like (60 x 2 ) + 45 + (55 x 2 ) + 50
Please help
I have added global variable at top group and set to zero. Then sum and conditions in details area. It works well but I need to show result in Group header 1. Sum works fine and show correct values in details section and footer but I want to show total on top. I found that whilereadingrecords or sub report may work here, but dont know how to use either