2017-07-28 29 views
0

我試圖創建一個使用最大一個DQL請求(案例...) 我使用PostgreSQL無法使用SELECT MAX CASE教義

這裏有一個表爲例:(密鑰值是隨機的)

[------TABLE------] 
[TYPE |KEY|VALUE ] 
[SWEET|843|WAFFLE ] 
[SWEET|258|NUTELLA] 

這裏還有想什麼我:

[TYPE |DESSERT|SAUCE ] 
[SWEET|GAUFRE |NUTELLA] 

而這裏的(工作)SQL我所做的:

select 
    type, 
    max(case when key=843 then valeur end) as dessert, 
    max(case when key=258 then valeur end) as sauce 
from 
    TABLE 
group by type 

但是,我不能轉換這個SQL查詢來DQL,這裏是我綁什麼(不工作):

$qb = $this->createQueryBuilder('table') 
->select('table.type') 
->addSelect('max(case when table.key=843 then table.value end) as dessert') 
->addSelect('max(case when table.key=258 then table.value end) as sauce') 
->addGroupBy('table.type') 

這裏是我的錯誤:我嘗試

[Syntax Error] Error: Unexpected ')' 

有和沒有最大值,並且生成的DQL是正確的。 你能幫忙嗎?非常感謝你們!

回答

0

的解決方案是增加的ELSE的情況下子句:

$qb = $this->createQueryBuilder('table') 
->select('table.type') 
->addSelect('max(case when table.key=843 then table.value else null end) as dessert') 
->addSelect('max(case when table.key=258 then table.value else null end) as sauce') 
->addGroupBy('table.type') 

有一個愉快的一天

+0

可能不相關,但你有兩次甜點在你的榜樣。 – JulHaus

+0

這似乎是在教條上的一個常見問題:)你ou試過以下內容:'$ qb = $ this-> createQueryBuilder('table') - > select('table.type,max(case when table.key = 843 then table.value end)as dessert,max(case when table.key = 258 then table.value end)as sauce') - > addGroupBy('table.type')' – JulHaus

+0

感謝您的回答,我確切地得到同樣的錯誤..哈哈 – TheMackou