Doctrine2 - need some help modifying working dql - symfony

I have the following dql SELECT query, which works:
SELECT q AS question, AVG(r.score) AS average
FROM MyBundle:Question q
JOIN q.ratings r
WHERE q.deleted IS NULL
GROUP BY q.id
ORDER BY q.id ASC
What I'd like to do is make it return 0 if AVG(r.score) is either 0 or null. I'm just not sure how to do it. Is it possible to do something like:
SELECT q AS question, (AVG(r.score) OR 0) AS average FROM....

Since you can't use aggregate function in a WHERE clause, you must use HAVING instead. That should do the trick:
SELECT q AS question, AVG(r.score) AS average
FROM MyBundle:Question q
JOIN q.ratings r
WHERE q.deleted IS NULL
GROUP BY q.id
HAVING average IN (0, NULL)
ORDER BY q.id ASC

Are you looking for an alternative of mysql IFNULL to a doctrine query builder. Look at this question.

Changing my (INNER) JOIN to a LEFT OUTER JOIN gave me the results I was looking for.

Related

Two queries in one statment

I want in put two queries in one statement how can i do that in this state?
1
stmt = `SELECT Comments.*, Users.username,Users.avatar from Users
INNER JOIN Comments ON Comments.users_id =Users.users_id
WHERE Comments.post_id= 1`
2
`SELECT COUNT(*) comment FROM Comments WHERE Comments.post_id= 1`;
Cross join the 1st query to the 2nd:
SELECT c.*, u.username, u.avatar, t.counter
FROM Users u INNER JOIN Comments c
ON c.users_id = u.users_id
CROSS JOIN (SELECT COUNT(*) counter FROM Comments WHERE post_id = 1) t
WHERE c.post_id = 1
I don't know much about SQLite, but in SQL Server you can use ";" to use multiple queries.
Maybe it does work for SQLite.
You can use GROUP BY on comments table in a way:
'SELECT COUNT(Comments.<id>), Comments.*, Users.username,Users.avatar
from Users INNER JOIN Comments ON Comments.users_id =Users.users_id
WHERE Comments.post_id = 1 GROUP BY Comments.<id>';
*This syntax of GROUP BY clause follows PostgreSQL. You might need to tweak according to the syntax followed by sqlite.

Is it possible to combine fetch joins and COUNT in Doctrine?

I want to fetch join some entities to avoid a lot of additional queries and I also want to get count of related collection.
Something like this:
SELECT u, a, count(p) properties_count
FROM User u
JOIN u.address a
LEFT JOIN u.properties p
group by u.id
That is I want to get a collection like [[0 => User, 'properties_count' => 42], [0 => ...], ...].
It works without fetch join (SELECT u, count(p) properties_count) but with SELECT u, a, count(p) properties_count it seems to not include count in the
result.
Am I doing something wrong?
Looks like it was just a mistake in the original query.
It was using joins like this:
LEFT JOIN Address a WITH a.id = u.address
When I replaced this to
LEFT JOIN u.address a
it started working. (I thought they were equivalent)

Distinct Count expression ssas

I want to create a new calculated member in OLAP Cube to count the number of distinct clients, I'm trying to write this expression, but i don't know how to make it in MDX:
Distinct count ([DIM.Clients].[CuNumber], where Sum([Measures].[QQT - FACT Ventes] >=1)
Any help please !
Thanks!
Hi thank you for replies,
I spent couple of days trying to make the query work, but without big progress.
First, I ran an SQL query on my datawarehouse to know what result should I get on my OLAP Cube,
this is my SQL query:
use [Warehouse]
select count(*) as count_row
From
(Select F.FaCunumberX
from [dbo].[Dim_FaClients] F
inner join [dbo].[FACT_Ventes] V on F.[SK_FAClients] = V.SK_FaClients
inner join [dbo].[Dim_Date] D on D.SK_Date = V.SK_Date
where
D.Year = '2014'
Group by F.FaCunumberX
having SUM(V.QQT) >= 1) test
the result I got is : 26026
On my OLAP Cube I tried several queries, but I didn't get the same result
this is some of the expressions that I tried :
WITH SET MySet AS
(Filter({[DIM FA Clients].[FaCuNumberX].[FaCuNumberX]}*{([Dim Date].[Year].&[2014],[Measures].[QQT - Fact Ventes])},[Measures].[QQT - Fact Ventes]>1 or [Measures].[QQT - Fact Ventes]=1)
MEMBER MEASURES.SETDISTINCTCOUNT AS
DISTINCTCOUNT(MySet)
SELECT {MEASURES.SETDISTINCTCOUNT} ON 0
FROM [CubeAll]
the result I got with this one is : 31575
I tried also this expression :
DistinctCount(Filter([DIM.Clients].[CuNumber].[CuNumber].Members,
[Measures].[QQT - FACT Ventes] >= 1
)
)
the same result : 31575
sincerely, I don't see what I'm missing on my expressions.
Thank's for your help !
This would be something like
DistinctCount(Filter([DIM.Clients].[CuNumber].[CuNumber].Members,
[Measures].[QQT - FACT Ventes] >= 1
)
)
See the documentation of Filter and DistinctCount for details.

How to make JOIN with OR expression in DQL?

Here is the SQL equivalent of what I expect to get from Doctrine:
SELECT c.* FROM comments c
LEFT JOIN articles a
ON a.id = c.articles_id OR a.translation = c.articles_id
WHERE c.published = 1 AND c.language = a.language
The problem is that I cannot make Doctrine to generate the JOIN operation with OR as it is supposed to be. If we execute query from the following QueryBuilder object:
$qb->select('c')
->from('Project:Comment', 'c')
->leftJoin('c.article', 'a', 'WITH', 'a = c.article OR a.translation = c.article')
->where('c.published = true AND c.language = a.language');
we receive the following SQL statement:
SELECT
...
FROM comments c0_
LEFT JOIN articles a0_ ON c0_.articles_id = a0_.id
AND (
a0_.id = c0_.articles_id OR
a0_.translation = c0_.profiles_id
)
WHERE c0_.published = 1 AND c0_.language = a0_.language
which is obviously not the same as the initial query, as WITH operator seems to add additional conditions to the basic one instead of replacing the whole condition.
Is there any way to force Doctrine to output exactly what I need? I know that I may use native SQL but I doubt that it will be as convenient as QueryBuilder. Maybe there is a way to extend Doctrine with normal JOIN ON implementation instead of this odd JOIN WITH?
Doctrine doesn't implement this because it is (as far as I understand at least) not considered optimized enough for SQL.
See this SO post for precisions.
What you intend to do could appearantly be done using Union and other types of Join.

Doctrine2 QueryBuilder subquery with SUM?

I'm trying to get Hotels from my database that have enough beds (user specifies guest count parameter). The query should look like this:
SELECT h.* FROM Hotel AS h
WHERE
(SELECT SUM(r.guestCount * r.count)
FROM Room AS r
WHERE r.hotel_id = h.id) >= $questCount
The above query contains subquery in the where clause. I've read doctrine's QueryBuilder documentation and I don't know how to make subquery in QB.
All I have now is:
$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
->select('h')
->from('AAAHotelsBundle:Hotel', 'h')
->where(.........???...........);
Any ideas what to do next?
Of course I simplified the problem (the query itself is much more complex). I use Symfony2.
DQL is not a solution in my case. I do really need to use QueryBuilder.
I've asked the same question on Google Groups and here is the solution:
$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder()
->select('h')
->from('AAAHotelsBundle:Hotel', 'h')
->join('Room', 'r')
->groupBy('h')
->having('SUM(r.guestCount * r.count) >= :guestCount')
->setParameter("guestCount", $guestCount);
I think this DQL will help you
SELECT h, SUM(r.guestCount * r.count) as TSUM FROM Hotel h JOIN h.room r
WHERE TSUM >= :questCount

Resources