PostgreSQL - неагрегированные поля, присутствующие в предложении GROUP BY -Альтернативный метод - PullRequest
0 голосов
/ 04 марта 2020

Мне нужно получить результат, объединив около 10 таблиц в Postgresql, но мне нужно сгруппировать результат по первичному ключу одной таблицы.

Есть ли способ построить запрос без включения этих нежелательных полей в условие group by, отличное от рекурсивного запроса (выполнение подзапроса)?

Мой запрос такой, как показано ниже:

select cd.closedroomid, cd.mongoid as _id, 'close' as type,
count(r.reviewid) as "reviewsCount", h.mongoid as "hospitalityId",
h.hospitalityname as name, 
fMain.mongoid as "mainImage",
fUnique.mongoid as "uniqueImage", 
string_agg(fFotovan.mongoid, ',') as "fotovanImage",
string_agg(fFoodAndDrinks.mongoid, ',') as "foodAndDrinksImage",
h.parkingPricePerHour, h.parkingPricePerDay, 0 as "flexWorkspaceCount",
'' AS favourites,
h.averagefoodprice, 
string_agg(fImages.mongoid, ',') as imageIds, 
case when bs.bookable = 'direct' then true else false end as isBookableDirect,
case when bd.bookingdiscountid IS NULL then false else true end as bookingDiscount
from table1 h
inner join table2 cd 
on cd.hospitalityid = h.hospitalityid
LEFT JOIN table3 r
on r.workspacetype = 'close' and r.closedroomid = cd.closedroomid
and r.hospitalityid = h.hospitalityid
LEFT JOIN files fMain
on fMain.filerelationfromid = h.hospitalityid and fMain.filerelationfrom = 'hospitality'
and fMain.ismainimage = true
LEFT JOIN files fUnique
on fUnique.filerelationfromid = h.hospitalityid and fUnique.filerelationfrom = 'hospitality'
and fUnique.isuniqueimage = true
LEFT JOIN files fFotovan
on fFotovan.filerelationfromid = h.hospitalityid and fFotovan.filerelationfrom = 'hospitalityOutside'
LEFT JOIN files fFoodAndDrinks
on fFoodAndDrinks.filerelationfromid = h.hospitalityid and fFoodAndDrinks.filerelationfrom = 'foodAndDrinks'
LEFT JOIN files fImages
on fImages.filerelationfromid = h.hospitalityid and fImages.filerelationfrom = 'hospitality'
and fImages.isuniqueimage != true and fImages.ismainimage != true
LEFT JOIN bookingslots bs 
on bs.closedroomid = cd.closedroomid
LEFT JOIN bookingdiscountclosedroomrel bdcr
on bdcr.closedroomid = cd.closedroomid
LEFT JOIN bookingdiscounts bd
on bd.bookingdiscountid = bdcr.bookingdiscountid AND bd.status = 'Valid'
where h.isdeleted = false  
group by cd.closedroomid, h.hospitalityid, fMain.fileid, fUnique.fileid, 
fFotovan.fileid, fFoodAndDrinks.fileid, fImages.fileid, bs.bookingslotid,
bd.bookingdiscountid;

Здесь На самом деле я хочу получить результат быть сгруппированы только по cd.closedroomid. Но postgresql не позволяет без добавления всех других неагрегированных полей указываться в группе с помощью предложения .

Сам простой запрос не выполняется без включения всех полей в сгруппировать по пунктам.

select cd.closedroomid, cd.mongoid as _id, 'close' as type,
h.mongoid as "hospitalityId", h.hospitalityname as name, 
h.parkingPricePerHour, h.parkingPricePerDay, 0 as "flexWorkspaceCount", '' AS favourites, h.averagefoodprice
from table1 cd 
inner join table2 h 
on cd.hospitalityid = h.hospitalityid
where cd.isactive = true and h.isdeleted = false   
group by cd.closedroomid

Есть ли какое-нибудь решение?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...