I am trying find out how many leads are generated per listing per day.
I have this query:
SELECT
vl.listing_id,
vl.created_at::date as dt,
(
SELECT count(*)
FROM voice_leads vl2
WHERE vl2.listing_id = vl.listing_id
AND vl.created_at::date = vl2.created_at::date
) as cnt
FROM voice_leads vl
GROUP BY listing_id, vl.created_at::date
ORDER BY listing_id
but when executing I get "ERROR: subquery uses ungrouped column "vl.created_at" from outer query LINE 8: AND vl.created_at::date = vl2.created_at::date"
Any idea on what I could do to fix it?
SELECT
vl.listing_id,
vl.created_at::date as dt,
count(cnt.*)
FROM voice_leads vl, lateral (SELECT *
from voice_leads vl2
WHERE vl2.listing_id = vl.listing_id
AND vl.created_at::date = vl2.created_at::date) cnt
GROUP BY vl.listing_id, vl.created_at::date
ORDER BY listing_id
You don't need the subquery:
SELECT
vl.listing_id,
vl.created_at::date as dt,
count( vl.listing_id ) as cnt
FROM voice_leads vl
GROUP BY listing_id, vl.created_at::date
ORDER BY listing_id
should do the same.
count(field)
will count the number of rows in each group.
count(*)
will count the total number of rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.