Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5938435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:41:06+00:00 2026-05-22T15:41:06+00:00

I have the following tables in a PostgreSQL: Categories | Locations | Checkins |

  • 0

I have the following tables in a PostgreSQL:

Categories | Locations   | Checkins     | Users  | Friendships
id         | id          | id           | id     | user_id
name       | category_id | location_id  | gender | friend_id
icon       | name        | user_id      |        |

Now, i want to retrieve the following information about a venue

  • How many female and male users a location has
  • Category name and icon
  • Location name
  • How many friends have checked in at a location (from a given user id)

Except the last point, I solved it. But I have troubles to count the friends from a given user id. I tried it with this query:

SELECT distinct locations.id,
 max(locations.name) as name,
 max(locations.location) as location,
 max(categories.name) as cat,
 max(categories.icon) as caticon,
 SUM(CASE WHEN users.gender = 'm' THEN 1 ELSE 0 END) AS male,
 SUM(CASE WHEN users.gender = 'f' THEN 1 ELSE 0 END) AS female,
 SUM(CASE WHEN friendships.user_id = 1 OR friendships.friend_id=1 THEN 1 ELSE 0 END) AS friends
 FROM locations
 INNER JOIN checkins ON checkins.location_id = locations.id 
 INNER JOIN users ON users.id = checkins.user_id 
 INNER JOIN categories ON categories.id = locations.category_id
 LEFT JOIN friendships ON friendships.user_id = users.id OR friendships.friend_id = users.id
 WHERE locations.id=7
 GROUP BY locations.id

But I get a wrong number of the count for female users. Any idea what I’m doing wrong? I think I need a left join for the friendships table, because if a user has no friends (or no user is given) it should only return 0 for the friend count.

Hope I made myself clear,
thx, tux

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:41:07+00:00Added an answer on May 22, 2026 at 3:41 pm
    SELECT
      L.id,
      L.name,
      c.name AS cat,
      c.icon AS caticon,
      COUNT(CASE u.gender WHEN 'm' THEN 1 END) AS male,
      COUNT(CASE u.gender WHEN 'f' THEN 1 END) AS female,
      COUNT(f.user_id) AS friends
    FROM Locations L
      INNER JOIN Categories c ON c.id = L.category_id
      INNER JOIN Checkins ch ON ch.location_id = L.id
      INNER JOIN Users u ON u.id = ch.user_id
      LEFT JOIN Friendships f ON f.user_id = @user_id AND f.friend_id = ch.user_id
                              OR f.user_id = ch.user_id AND f.friend_id = @user_id
    WHERE L.id = @location_id
    GROUP BY L.id, L.name, c.name, c.icon
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following tables questions -> id, question_data, user_id users -> id, fname, lname
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01
I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
In PostgreSQL 8.3, let's say I have a table called widgets with the following:
I have the following tables. I wanted it so when I make more boxes
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have the following tables, with these keys in my database: bookings session_id sessions
I have the following tables in a database (i'll only list the important attributes):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.