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

  • Home
  • SEARCH
  • 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
  • 0 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 the following tables in MySQL: team: id, name, [more stuff] person: id,
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have the following tables in my database that have a many-to-many relationship, which
I have the following five tables: ISP Product Connection AddOn AddOn/Product (pivot table for
I have following situation. A main table and many other tables linked together with
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
I have 2 tables with the following structure.. category { sub_cat_id - numeric sub_cat_name
I have the following 2 tables: items: id int primary key bla text events:
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I have the following database tables: party, with a pk pty_id connected to a

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.