使用这样的地理数据记录:
START | END CITY1 | STATE1 | CITY2 | STATE2 ---------------------------------------------- New York | NY | Boston | MA Newark | NJ | Albany | NY Cleveland| OH | Cambridge | MA
我想输出类似这样的内容,其中它以矩阵形式显示START / END配对:
| MA | NJ | NY | OH ------------------------------ MA | 0 | 0 | 1 | 0 NJ | 0 | 0 | 1 | 0 NY | 1 | 0 | 0 | 0 OH | 1 | 0 | 0 | 0
我可以看到如何GROUP BY以及COUNT将如何找到数据,但是我对如何显示为矩阵一无所知。有人有什么想法吗?
GROUP BY
COUNT
这似乎可以解决问题,并在PostgreSQL 9.1上进行了测试。几乎肯定会需要对它进行调整以使其适合SQL Server(任何人都可以随时更新我对此的回答)。
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;
但是请注意,我的输出与您的输出略有不同-我不确定这是因为您的示例输出是错误的,还是因为我误解了您的要求:
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)
此查询的工作方式是查询表两次,一次查询state1-> state2路由,第二次查询state2-> state1路由,然后将它们连接在一起UNION ALL。
UNION ALL
然后,对于每个目标状态,它SUM()为该行的原始状态运行一个。
SUM()
该策略应易于适应任何RDBMS。