简体   繁体   English

SQL Server 2008:基于ID的ST联合

[英]SQL Server 2008: ST Union based on ID

I have a table which contains spatial data (geography type). 我有一个包含空间数据(地理类型)的表。 Let's call this table A. The table is structured as below: 我们将此表称为A。该表的结构如下:

AreaID  RegionID    RegionName  SP_Geography
---------------------------------------------
   1       1        EAST        *Geogstring*
   2       1        EAST        *Geogstring*
   3       2        NORTH       *Geogstring*
   4       2        NORTH       *Geogstring*
   5       3        WEST        *Geogstring*

From this table I want to update another table called table B. This table is structured as below: 我想从该表更新另一个称为表B的表。该表的结构如下:

RegionID    RegionNAme  SP_Geograph
-------------------------------------
1   EAST    *NULL*
2   NORTH   *NULL*
3   WEST    *NULL*

I want to use the STUnion command to combine the geography strings, and then update the empty SP_Geography column in Table B where the RegionID matches the RegionID in Table A. 我想使用STUnion命令合并地理字符串,然后更新表B中的空SP_Geography列,其中RegionID与表A中的RegionID匹配。

I have managed to get the STUnion command to work between two individual rows (which successfully combines the two), but am not sure on how to use this to populate another table using a where clause or cursor. 我设法使STUnion命令在两个单独的行之间工作(成功地将两者合并),但是不确定如何使用where子句或游标填充另一个表。

Any help would be much appreciated! 任何帮助将非常感激!

Danny 丹尼

PS I'm aware of the Union Aggregate function in SQL Server 2012, but I cannot use this as our databases have to run in 2008 PS我知道SQL Server 2012中的Union Aggregate函数,但是我不能使用它,因为我们的数据库必须在2008年运行

What a quandary you find yourself in..... 您真陷入困境.....

I hope this little snippet of SQL will do the trick. 我希望这小段SQL可以解决问题。 Just make sure you change the regionID each time. 只要确保您每次都更改regionID。 If you want to make it dynamic simply put a cursor round the regionID and let the fun commence. 如果要使其动态,只需将光标放在regionID周围,就可以开始乐趣了。

DECLARE @MergedGeog Geography
DECLARE @RegionID INt

SET @RegionID = 1

SELECT TOP(1) @MergedGeog=SP_GEOMETRY  FROM dbo.[SMA_Singapore_WGS84] WHERE RegionID = @RegionID

--perform the merge
SELECT @MergedGeog=@MergedGeog.STUnion(ISNULL(SP_GEOMETRY,@MergedGeog))
FROM (SELECT SP_GEOMETRY FROM  [SMA_Singapore_WGS84] WHERE RegionID = @RegionID) k

--return merged polygon

select @MergedGeog
UPDATE table B
SET SP_Geograph = CONCAT(a.SP_Geography1, isnull(a.SP_Geography2,''))
from
(SELECT a.RegionID, a.RegionName, a.SP_Geography as SP_Geography1, b.SP_Geography as SP_Geography2 from table a as a
left join table a as b on a.RegionID = b.RegionID and a.RegionName = b.RegionName) aa
where [table b].RegionID = aa.RegionID

Would this work? 这行得通吗? I haven't tested it. 我还没有测试。

UPDATE table B
SET SP_Geograph = SP_Geography1.STUnion(isnull(SP_Geography2,''))
from
(SELECT a.RegionID, a.RegionName, a.SP_Geography as SP_Geography1, b.SP_Geography as SP_Geography2 from table a as a
left join table a as b on a.RegionID = b.RegionID and a.RegionName = b.RegionName) aa
where [table b].RegionID = aa.RegionID

Or this... 或这个...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM