Assign Positions MS Access

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

Assign Positions MS Access

问题

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

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

  1. GS= c('OOOZ','WYAG','WYAH','WZAG','WZAH','WZAJ','WZAK','WZAL','WZAM','WZAN','XAAG','XAAH','XAAJ','XAAK','XAAL','XAAM','XAAN','XAAP','XAAQ'),
  2. 纬度= c0,-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),
  3. 经度= c0,-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部分)

  1. case_when(
  2. 纬度<= -47.00amp; 纬度> -47.25〜&quot; WWquot;,
  3. 纬度<= -47.25amp; 纬度> -47.50〜&quot; WXquot;,
  4. 纬度<= -47.50amp; 纬度> -47.75〜&quot; WYquot;,
  5. 纬度<= -47.75amp; 纬度> -48.00〜&quot; WZquot;,
  6. 纬度<= -48.00amp; 纬度> -48.25〜&quot; XAquot;,
  7. TRUE〜&quot; OZquot;)`

  1. case_when(
  2. 经度>= -64.00amp; 经度< -63.50〜&quot; AAquot;,
  3. 经度>= -63.50amp; 经度< -63.00〜&quot; ABquot;,
  4. 经度>= -63.00amp; 经度< -62.50〜&quot; ACquot;,
  5. 经度>= -62.50amp; 经度< -62.00〜&quot; ADquot;,
  6. 经度>= -62.00amp; 经度< -61.50〜&quot; AEquot;,
  7. 经度>= -61.50amp; 经度< -61.00〜&quot; AFquot;,
  8. 经度>= -61.00amp; 经度< -60.50〜&quot; AGquot;,
  9. 经度>= -60.50amp; 经度< -60.00〜&quot; AHquot;,
  10. 经度>= -60.00amp; 经度< -59.50〜&quot; AJquot;,
  11. 经度>= -59.50amp; 经度< -59.00〜&quot; AKquot;,
  12. 经度>= -59.00amp; 经度< -58.50〜&quot; ALquot;,
  13. 经度>= -58.50amp; 经度< -58.00〜&quot; AMquot;,
  14. 经度>= -58.00amp; 经度< -57.50〜&quot; ANquot;,
  15. 经度>= -57.50amp; 经度< -57.00〜&quot; APquot;,
  16. 经度>= -57.00amp; 经度< -56.50〜&quot; AQquot;,
  17. TRUE〜&quot; OZquot;)

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. I have a problem with MS Access (although I am a R programmer).
  4. 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.
  5. I solved the problem already in **R**, that&#39;s why I provide this example:
  6. 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)
)

  1. And I have a second Table containing actual positions e.g.
  2. `Data &lt;- data.frame(Latitude = rnorm(100, -50, 3), Longitude = rnorm(100, -60, 3))`
  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")`

  1. 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")

  1. And then put them together with `paste(GS_Lat(Data$Latitude), GS_Long(Data$Longitude))`
  2. 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")

  1. 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?
  2. 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;

  1. EDIT:
  2. That was my Query Design in the end
  3. [Query Design][1]
  4. 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. [1]: https://i.stack.imgur.com/zOxr1.png
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 当使用数据库时,我更愿意将配置放入表格中。然后,您可以简单地使用JOIN来获取所需的配置值(网格)。
  6. 纬度的示例:**GridLat**
  7. 请注意,您需要涵盖两种边缘情况。
  8. | LatID | LatMin | LatMax | LatGrid |
  9. |-------|--------|--------|---------|
  10. | 1 | -47,00 | 0,00 | OZ |
  11. | 2 | -47,25 | -47,00 | WW |
  12. | 3 | -47,50 | -47,25 | WX |
  13. | 4 | -47,75 | -47,50 | WY |
  14. | 5 | -48,00 | -47,75 | WZ |
  15. | 6 | -48,25 | -48,00 | XA |
  16. | 7 | -90,00 | -48,25 | OZ |
  17. 测试数据:**Position**
  18. | PosID | Latitude |
  19. |-------|----------|
  20. | 1 | -47,50 |
  21. | 2 | -47,49 |
  22. | 3 | -50,00 |
  23. | 4 | -20,00 |
  24. JOINSQL
  25. ```sql
  26. SELECT P.PosID, P.Latitude, GL.LatGrid
  27. FROM Position AS P
  28. INNER JOIN
  29. 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:

  1. SELECT P.PosID, P.Latitude, GL.LatGrid
  2. FROM Position AS P
  3. INNER JOIN
  4. 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:

确定