0

On our postgresql database we currently have 2 tables called Users and Bookings.

We are currently trying to know on the users that made a booking yesterday how many bookings they did over time.

Here is the query we have for the moment:

SELECT "domain".users.email, COUNT("domain".bookings."id")
FROM "domain".bookings
INNER JOIN "domain".users ON "domain".users."id" = "domain".bookings.user_id
GROUP BY "domain".users.email

If we have the date the booking was created (field "domain.bookings.created_at) with filter yesterday we only get the data from yesterday.

Is there a way to see on who bought yesterday how many bookings they did overtime?

Thank you! Luca

2
  • Add count(case when bookingdate = yesterday then 1 end) (pseudo SQL) to the select list.
    – jarlh
    May 30, 2016 at 9:47
  • Please add some sample data and the expected output. I don't really understand what output you expect
    – user330315
    May 30, 2016 at 9:57

3 Answers 3

0

TRY THIS

SELECT "domain".users.email, COUNT(CASE WHEN bookingdate = current_date - 1 THEN 1 END ) AS TOTAL_BOOKING
FROM "domain".bookings
INNER JOIN "domain".users ON "domain".users."id" = "domain".bookings.user_id
GROUP BY "domain".users.email
0
0

This is what you are looking for:

SELECT u.id, u.email, count(b.id) AS "Total Bookings" 
FROM "domain".bookings as b
  JOIN "domain".users as U ON u.id = b.user_id
WHERE EXISTS (SELECT 1 
              FROM "domain".bookings b2
              WHERE b2.created_at = current_date - 1
                AND b2.user_id = b.user_id)
GROUP BY u.email;

The exists condition will only return rows from the bookings table for users that booked something yesterday.


Unrelated, but: using a keyword like domain that requires double quotes for identifiers is not such a good idea. It would save you some trouble in the long run if you found a different name

1
  • Thank you! I tested but I am still getting the numbers for all users, it looks like the filter is not working... Query is: SELECT "domain".users."id", "domain".users.email, count("domain".bookings."id") AS "Total Bookings" FROM "domain".bookings JOIN "domain".users ON "domain".users."id" = "domain".bookings.user_id WHERE EXISTS (SELECT 1 FROM "domain".bookings WHERE "domain".bookings.created_at < current_date - 1 AND "domain".bookings.user_id = "domain".bookings.user_id) GROUP BY "domain".users.email, "domain".users."id" May 30, 2016 at 11:22
0

Thank you for answering. I was probably not precise enough!

Basically if I use the following query:

SELECT
"domain".users.email,
COUNT( "domain".bookings."id") AS "Total Bookings"
FROM
"domain".bookings
INNER JOIN "domain".users ON "domain".users."id" = "domain".bookings.user_id    
GROUP BY
"domain".users.email

I get the number of bookings made by a user.

BUT

I want to get the following: the number of bookings made all time by the users that bought yesterday.

I tried this

SELECT
"domain".users.email,
COUNT( "domain".bookings."id") AS "Total Bookings"
FROM
"domain".bookings
INNER JOIN "domain".users ON "domain".users."id" = "domain".bookings.user_id    
WHERE
"domain".bookings.created_at = 'yesterday'
GROUP BY
"domain".users.email

But I am getting no responses...

Thank you for help!

Luca

2
  • Don't add additional information as an answer edit your question instead.
    – user330315
    May 30, 2016 at 10:40
  • Ok sorry... Quite new with all this! May 30, 2016 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.