Assign Positions MS Access

huangapple go评论69阅读模式
英文:

Assign Positions MS Access

问题

我在MS Access中遇到了一个问题(尽管我是R程序员)。
我有一个包含每个网格方格的纬度(最小,最大)和经度(最小,最大)的参考表。在MS Access中,我想使用查询为第二个表的每个位置分配正确的网格方格,例如使用查询。

我已经在R中解决了这个问题,这就是为什么我提供这个示例:
我有一个包含Gridsquare位置的表。

  GS= c('OOOZ','WYAG','WYAH','WZAG','WZAH','WZAJ','WZAK','WZAL','WZAM','WZAN','XAAG','XAAH','XAAJ','XAAK','XAAL','XAAM','XAAN','XAAP','XAAQ'),
                    纬度= c(0,-47.633,-47.633,-47.883,-47.883,-47.883,-47.883,-47.883,-47.883,-47.883,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133),
                    经度= c(0,-60.75,-60.25,-60.75,-60.25,-59.75,-59.25,-58.75,-58.25,-57.75,-60.75,-60.25,-59.75,-59.25,-58.75,-58.25,-57.75,-57.25,-56.75)
                    )

和我有一个包含实际位置的第二个表,例如。
Data < - data.frame(纬度= rnorm(100,-50,3),经度= rnorm(100,-60,3))

在R中,我会像这样使用:(首先组合纬度,然后是Lon部分)

case_when(
  纬度<= -47.00&amp; 纬度> -47.25〜&quot; WW&quot;,
  纬度<= -47.25&amp; 纬度> -47.50〜&quot; WX&quot;,
  纬度<= -47.50&amp; 纬度> -47.75〜&quot; WY&quot;,
  纬度<= -47.75&amp; 纬度> -48.00〜&quot; WZ&quot;,
  纬度<= -48.00&amp; 纬度> -48.25〜&quot; XA&quot;,
  TRUE〜&quot; OZ&quot;)`

  case_when(
  经度>= -64.00&amp; 经度< -63.50〜&quot; AA&quot;,
  经度>= -63.50&amp; 经度< -63.00〜&quot; AB&quot;,
  经度>= -63.00&amp; 经度< -62.50〜&quot; AC&quot;,
  经度>= -62.50&amp; 经度< -62.00〜&quot; AD&quot;,
  经度>= -62.00&amp; 经度< -61.50〜&quot; AE&quot;,
  经度>= -61.50&amp; 经度< -61.00〜&quot; AF&quot;,
  经度>= -61.00&amp; 经度< -60.50〜&quot; AG&quot;,
  经度>= -60.50&amp; 经度< -60.00〜&quot; AH&quot;,
  经度>= -60.00&amp; 经度< -59.50〜&quot; AJ&quot;,
  经度>= -59.50&amp; 经度< -59.00〜&quot; AK&quot;,
  经度>= -59.00&amp; 经度< -58.50〜&quot; AL&quot;,
  经度>= -58.50&amp; 经度< -58.00〜&quot; AM&quot;,
  经度>= -58.00&amp; 经度< -57.50〜&quot; AN&quot;,
  经度>= -57.50&amp; 经度< -57.00〜&quot; AP&quot;,
  经度>= -57.00&amp; 经度< -56.50〜&quot; AQ&quot;,
  TRUE〜&quot; OZ&quot;)

然后使用 paste(GS_Lat(Data$Latitude), GS_Long(Data$Longitude)) 将它们放在一起。

在MS Access中,我尝试在查询中构建一个表达式,例如使用开关


<details>
<summary>英文:</summary>

I have a problem with MS Access (although I am a R programmer). 
I have one reference table containing Latitude(min, max) and Longitude(min, max) for each grid square. In MS Access I want to assign the correct grid square for each position of a second table using for example a query.

I solved the problem already in **R**, that&#39;s why I provide this example:
I have a table containing Gridsquare positions. 

data2 <- data.frame(
GS= c('OOOZ','WYAG','WYAH','WZAG','WZAH','WZAJ','WZAK','WZAL','WZAM','WZAN','XAAG','XAAH','XAAJ','XAAK','XAAL','XAAM','XAAN','XAAP','XAAQ'),
Latitude= c(0,-47.633,-47.633,-47.883,-47.883,-47.883,-47.883,-47.883,-47.883,-47.883,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133,-48.133),
Longitude= c(0,-60.75,-60.25,-60.75,-60.25,-59.75,-59.25,-58.75,-58.25,-57.75,-60.75,-60.25,-59.75,-59.25,-58.75,-58.25,-57.75,-57.25,-56.75)
)

And I have a second Table containing actual positions e.g.

`Data &lt;- data.frame(Latitude = rnorm(100, -50, 3), Longitude = rnorm(100, -60, 3))`

In R, I would use something like this: (putting together first the Lat and then the Lon part)

GS_Lat <-function(Latitude){
case_when(
Latitude <= -47.00 & Latitude > -47.25 ~ "WW",
Latitude <= -47.25 & Latitude > -47.50 ~ "WX",
Latitude <= -47.50 & Latitude > -47.75 ~ "WY",
Latitude <= -47.75 & Latitude > -48.00 ~ "WZ",
Latitude <= -48.00 & Latitude > -48.25 ~ "XA",
TRUE ~ "OZ")`

And

GS_Lon <-function(Longitude){
case_when(
Longitude >= -64.00 & Longitude < -63.50 ~ "AA",
Longitude >= -63.50 & Longitude < -63.00 ~ "AB",
Longitude >= -63.00 & Longitude < -62.50 ~ "AC",
Longitude >= -62.50 & Longitude < -62.00 ~ "AD",
Longitude >= -62.00 & Longitude < -61.50 ~ "AE",
Longitude >= -61.50 & Longitude < -61.00 ~ "AF",
Longitude >= -61.00 & Longitude < -60.50 ~ "AG",
Longitude >= -60.50 & Longitude < -60.00 ~ "AH",
Longitude >= -60.00 & Longitude < -59.50 ~ "AJ",
Longitude >= -59.50 & Longitude < -59.00 ~ "AK",
Longitude >= -59.00 & Longitude < -58.50 ~ "AL",
Longitude >= -58.50 & Longitude < -58.00 ~ "AM",
Longitude >= -58.00 & Longitude < -57.50 ~ "AN",
Longitude >= -57.50 & Longitude < -57.00 ~ "AP",
Longitude >= -57.00 & Longitude < -56.50 ~ "AQ",
TRUE ~ "OZ")


And then put them together with `paste(GS_Lat(Data$Latitude), GS_Long(Data$Longitude))`

In **MS Access** I tried to build an expression within a query, for example with switch

Longitude:=Switch([Longitude]<-63.5,"AA",[Longitude]<-63,"AB",[Longitude]<-62.5,"AC",[Longitude]<-62,"AD",[Longitude]<-61.5,"A",[Longitude]<-61,"A",[Longitude]<-60.5,"AG",[Longitude]<-60,"AH",[Longitude]<-59.5,"AJ",[Longitude]<-59,"AK",[Longitude]<-58.5,"AL",[Longitude]<-58,"AM",[Longitude]<-57.5,"AN",[Longitude]<-57,"AP",[Longitude]<-56.5,"AQ")



But the problem is, that the expression seem to complicated to compute for MS Access. Is there another way, to create a query column assigning the correct gridsquare for each position?

I really wouldn&#39;t know how to use SQL for this, maybe something like

SELECT P.*,
CASE
WHEN P.Latitude <= -47.00 AND P.Latitude > -47.25 THEN 'WW'
WHEN P.Latitude <= -47.25 AND P.Latitude > -47.50 THEN 'WX'
WHEN P.Latitude <= -47.50 AND P.Latitude > -47.75 THEN 'WY'
...
ELSE 'OZ'
END AS Gridsquare
FROM Positions P
JOIN Gridsquares G ON P.Position = G.Position;



EDIT:
That was my Query Design in the end
[Query Design][1]

And I modified to code thanks to your answers to:

SELECT Positions.Latitude, Northing.Grid, Easting.Grid, Positions.Longitude
FROM (Positions INNER JOIN Northing ON (Positions.Latitude < Northing.To) AND (Positions.Latitude >= Northing.From)) INNER JOIN Easting ON (Positions.Longitude < Easting.To) AND (Positions.Longitude >= Easting.From);


  [1]: https://i.stack.imgur.com/zOxr1.png

</details>


# 答案1
**得分**: 0

当使用数据库时,我更愿意将配置放入表格中。然后,您可以简单地使用JOIN来获取所需的配置值(网格)。

纬度的示例:**GridLat**
请注意,您需要涵盖两种边缘情况。

| LatID | LatMin | LatMax | LatGrid |
|-------|--------|--------|---------|
|     1 | -47,00 |   0,00 | OZ      |
|     2 | -47,25 | -47,00 | WW      |
|     3 | -47,50 | -47,25 | WX      |
|     4 | -47,75 | -47,50 | WY      |
|     5 | -48,00 | -47,75 | WZ      |
|     6 | -48,25 | -48,00 | XA      |
|     7 | -90,00 | -48,25 | OZ      |

测试数据:**Position**

| PosID | Latitude |
|-------|----------|
|     1 |   -47,50 |
|     2 |   -47,49 |
|     3 |   -50,00 |
|     4 |   -20,00 |

带JOIN的SQL:

```sql
SELECT P.PosID, P.Latitude, GL.LatGrid
FROM Position AS P 
INNER JOIN 
GridLat AS GL ON P.Latitude > GL.LatMin AND P.Latitude <= GL.LatMax

结果:

PosID Latitude LatGrid
1 -47,50 WY
2 -47,49 WX
3 -50,00 OZ
4 -20,00 OZ
英文:

When using a database, I'd rather put the configuration into tables. Then you can simply use JOIN to get the desired config value (grid).

Example for latitude: GridLat
Note that you need to cover both edge cases.

LatID LatMin LatMax LatGrid
1 -47,00 0,00 OZ
2 -47,25 -47,00 WW
3 -47,50 -47,25 WX
4 -47,75 -47,50 WY
5 -48,00 -47,75 WZ
6 -48,25 -48,00 XA
7 -90,00 -48,25 OZ

Testdata: Position

PosID Latitude
1 -47,50
2 -47,49
3 -50,00
4 -20,00

SQL with JOIN:

SELECT P.PosID, P.Latitude, GL.LatGrid
FROM Position AS P 
INNER JOIN 
GridLat AS GL ON P.Latitude &gt; GL.LatMin AND P.Latitude &lt;= GL.LatMax

Result:

PosID Latitude LatGrid
1 -47,50 WY
2 -47,49 WX
3 -50,00 OZ
4 -20,00 OZ

huangapple
  • 本文由 发表于 2023年7月7日 02:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定