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 8123699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:15:32+00:00 2026-06-06T06:15:32+00:00

I have two tables with a linking table describing their many-to-many relationship. TABLE buildings

  • 0

I have two tables with a linking table describing their many-to-many relationship.

TABLE buildings
    building_id
    building_name

TABLE facility_types
    facility_type_id
    facility_type

TABLE buildings_2_facility_types
    building_id
    facility_type_id

Now I need a report including the facility types as columns per each building.

REPORT:

  NAME        golf    pool    garage
Building A     Y        N        Y
Building B     N        Y        Y
Building C     N        N        N

How can I do this in SQL?

N.B. This will be part of a much larger query involving lots of other tables. I can do all that part, but converting rows to columns has got me vexed!

  • 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-06-06T06:15:34+00:00Added an answer on June 6, 2026 at 6:15 am

    Converting rows to columns is a real pain and you have to understand that SQL is really not meant for this kind of operation.

    It’s still possible, providing that the set of values to be transposed is small, known and limited. If not, you’ll have to rely on you application layer.

    An exemple on how to do it should help you understand the 3 conditions:

    Assuming a table buildings_2_facility_types:

    building_id facility_type_id
        1               1
        1               2
        2               3
        3               1
        3               3
    

    You can get sthg out of it this way:

    SELECT
        building_id,
        IF(SUM(facility_type_id = 1) > 0, "Y", "N") as facility_type_1,
        IF(SUM(facility_type_id = 2) > 0, "Y", "N") as facility_type_2,
        IF(SUM(facility_type_id = 3) > 0, "Y", "N") as facility_type_3
    FROM buildings_2_facility_types
    GROUP BY building_id;
    

    See ? small, known and limited :/

    Of course, you can also do it with a proc stock but it’s usually not the prefered way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables(entries and tags) with a many-to-many linking table. Right now, I'm
I have a database two tables and a linking table that I need a
I have a typical N-M relationship structure like this: two independent tables: Table Client:
Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|
I have two tables: tags and linking table photos_tags. I want to add a
I have many-to-many linking table between two entities, datacenters and projects, in legacy code.
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username

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.