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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:31:26+00:00 2026-06-07T18:31:26+00:00

With geographic data records like this: START | END CITY1 | STATE1 | CITY2

  • 0

With geographic data records like this:

START                  |  END

CITY1    |   STATE1    |   CITY2    |  STATE2
----------------------------------------------
New York |    NY       |  Boston    |   MA
Newark   |    NJ       |  Albany    |   NY
Cleveland|    OH       |  Cambridge |   MA

I would like to output something like this where it counts START/END pairings displayed as a matrix:

   |  MA  |  NJ  |  NY  |  OH
------------------------------
MA |  0   |  0   |  1   |  0
NJ |  0   |  0   |  1   |  0
NY |  1   |  0   |  0   |  0
OH |  1   |  0   |  0   |  0

I can see how GROUP BY and COUNT will find the data but I’m lost on how to display as a matrix. Does anyone have any ideas?

  • 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-07T18:31:28+00:00Added an answer on June 7, 2026 at 6:31 pm

    This seems to do the trick, tested on PostgreSQL 9.1. It will almost certainly need to be adapted for SQL Server (anyone feel free to update my answer to that effect).

    SELECT start AS state,
        SUM((dest = 'MA')::INT) AS MA,
        SUM((dest = 'NJ')::INT) AS NJ,
        SUM((dest = 'NY')::INT) AS NY,
        SUM((dest = 'OH')::INT) AS OH
    FROM (
        SELECT state1 AS start, state2 AS dest
            FROM routes
        UNION ALL
        SELECT state2 AS start, state1 AS dest
            FROM routes
    ) AS s
    GROUP BY start
    ORDER BY start;
    

    However note that my output is slightly different than yours–I’m not sure if that’s because your sample output is wrong, or because I misunderstood your requirements:

     state | ma | nj | ny | oh 
    -------+----+----+----+----
     MA    |  0 |  0 |  1 |  1
     NJ    |  0 |  0 |  1 |  0
     NY    |  1 |  1 |  0 |  0
     OH    |  1 |  0 |  0 |  0
    (4 rows)
    

    This query works by querying the table twice, once for the state1 -> state2 routes, and a second time for the state2 -> state1 routes, then joins them together with UNION ALL.

    Then for each destination state, it runs a SUM() for that row’s origin state.

    This strategy should be easy to adapt for any RDBMS.

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

Sidebar

Related Questions

I would like to read geographic data (logitude/latitude) from two sources into one Javascript
I have huge amont of geographic data represented in simple object structure consisting only
I need geography data for new website. Data required is States (With names and
I have geographic data that was loaded into the geography datatype. For very specific
I want to send geographic data (latitude & longitude) to a mySQL database with
I need to convert data points from one geographic projection (Lat Long, Mercator, UTM)
I have file with binary data. This is specification: The SRTM data files have
I have cube which contains geographic sales data. I want to pull sales of
I have the following JOINED inheritance root entity for geographic areas (like continents, countries,
If you have geographic data stored in ESRI shapes, you have at least three

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.