Other ATN2 SQL function
1. Usage of ATN2
SQL Server Query 1
WITH CustomerCoordinates AS (
SELECT
CustomerID,
CompanyName,
-- Simulate customer coordinates. In a real scenario, these would come from your table.
-- Using row_number() to generate different coordinates for each customer for the example
47.6062 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) * 0.1) AS Latitude,
-122.3321 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) * 0.15) AS Longitude
FROM Customers
),
CustomerBearing AS (
SELECT
CustomerID,
CompanyName,
Latitude,
Longitude,
-- Calculate the bearing using ATN2
DEGREES(ATN2(
SIN(RADIANS(Longitude - -122.3321)) * COS(RADIANS(Latitude)),
COS(RADIANS(47.6062)) * SIN(RADIANS(Latitude)) -
SIN(RADIANS(47.6062)) * COS(RADIANS(Latitude)) * COS(RADIANS(Longitude - -122.3321))
)) AS BearingInDegrees
FROM CustomerCoordinates
)
SELECT
CustomerID,
CompanyName,
BearingInDegrees
FROM CustomerBearing
ORDER BY BearingInDegrees;
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.WithCTETable(new Table("CustomerCoordinates"), new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "CompanyName")
.Column(new ColumnArithmatic().Value(47.6062).ADD()
.StartBracket()
.SqlFunction(new ROW_NUMBER().ORDER_BY(new OrderBy().SetSelectNull()))
.MULTIPLY().Value(0.1)
.EndBracket()
, "Latitude")
.Column(new ColumnArithmatic().Value(-122.3321).ADD()
.StartBracket()
.SqlFunction(new ROW_NUMBER().ORDER_BY(new OrderBy().SetSelectNull()))
.MULTIPLY().Value(0.15)
.EndBracket()
, "Longitude")
.From("Customers")
)
.WithCTETable(new Table("CustomerBearing"), new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "CompanyName", "Latitude", "Longitude")
.Column(new DEGREES(new ATN2(
new ColumnArithmatic().SqlFunction(new SIN(new RADIANS(new ColumnArithmatic().Column("Longitude").SUBTRACT().Value(-122.3321))))
.MULTIPLY().SqlFunction(new COS(new RADIANS(new Column("Latitude")))),
new ColumnArithmatic().SqlFunction(new COS(new RADIANS(47.6062))).MULTIPLY()
.SqlFunction(new SIN(new RADIANS(new Column("Latitude")))).SUBTRACT()
.SqlFunction(new SIN(new RADIANS(47.6062))).MULTIPLY()
.SqlFunction(new COS(new RADIANS(new Column("Latitude")))).MULTIPLY()
.SqlFunction(new COS(new RADIANS(new ColumnArithmatic().Column("Longitude").SUBTRACT().Value(-122.3321))))
))
, "BearingInDegrees")
.From("CustomerCoordinates")
)
.Select()
.Columns("CustomerID", "CompanyName", "BearingInDegrees")
.From("CustomerBearing")
.OrderBy(new OrderBy().Set(new Column("BearingInDegrees")))
.Build();
Query build by SqlQueryBuilder 1
WITH CustomerCoordinates
AS (SELECT CustomerID,
CompanyName,
@pMAIN_2507192055433499830 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) * @pMAIN_2507192055433499831) AS Latitude,
@pMAIN_2507192055433499832 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) * @pMAIN_2507192055433499833) AS Longitude
FROM Customers),
CustomerBearing
AS (SELECT CustomerID,
CompanyName,
Latitude,
Longitude,
DEGREES(ATN2(SIN(RADIANS(Longitude - @pMAIN_2507192055433499834)) * COS(RADIANS(Latitude)), COS(RADIANS(@pMAIN_2507192055433499835)) * SIN(RADIANS(Latitude)) - SIN(RADIANS(@pMAIN_2507192055433499836)) * COS(RADIANS(Latitude)) * COS(RADIANS(Longitude - @pMAIN_2507192055433499837)))) AS BearingInDegrees
FROM CustomerCoordinates)
SELECT CustomerID,
CompanyName,
BearingInDegrees
FROM CustomerBearing
ORDER BY BearingInDegrees ASC;
Parameters (If used)
Name |
Value |
@pMAIN_2507192055433499830 |
47.6062 |
@pMAIN_2507192055433499831 |
0.1 |
@pMAIN_2507192055433499832 |
-122.3321 |
@pMAIN_2507192055433499833 |
0.15 |
@pMAIN_2507192055433499834 |
-122.3321 |
@pMAIN_2507192055433499835 |
47.6062 |
@pMAIN_2507192055433499836 |
47.6062 |
@pMAIN_2507192055433499837 |
-122.3321 |
Query Results 1:
|
CustomerID |
CompanyName |
BearingInDegrees |
1 |
WOLZA
|
Wolski Zajazd
|
37.37167751997869
|
2 |
WILMK
|
Wilman Kala
|
37.463808573482616
|
3 |
WHITC
|
White Clover Markets
|
37.55582959793371
|
4 |
WELLI
|
Wellington Importadora
|
37.647740735030936
|
5 |
WARTH
|
Wartian Herkku
|
37.73954212676835
|
6 |
VINET
|
Vins et alcools Chevalier
|
37.83123391543123
|
7 |
VICTE
|
Victuailles en stock
|
37.922816243591605
|
8 |
VAFFE
|
Vaffeljernet
|
38.01428925410445
|
9 |
TRAIH
|
Trail's Head Gourmet Provisioners
|
38.105653090103566
|
10 |
TRADH
|
Tradição Hipermercados
|
38.19690789499714
|
11 |
TORTU
|
Tortuga Restaurante
|
38.28805381246427
|
12 |
TOMSP
|
Toms Spezialitäten
|
38.379090986450265
|
13 |
THECR
|
The Cracker Box
|
38.470019561163035
|
14 |
THEBI
|
The Big Cheese
|
38.56083968106896
|
15 |
SUPRD
|
Suprêmes délices
|
38.65155149088873
|
16 |
SPLIR
|
Split Rail Beer & Ale
|
38.742155135593535
|
17 |
SPECD
|
Spécialités du monde
|
38.83265076040081
|
18 |
SIMOB
|
Simons bistro
|
38.92303851077054
|
19 |
SEVES
|
Seven Seas Imports
|
39.013318532401115
|
20 |
SAVEA
|
Save-a-lot Markets
|
39.103490971225305
|
21 |
SANTG
|
Santé Gourmet
|
39.1935559734066
|
22 |
ROMEY
|
Romero y tomillo
|
39.28351368533487
|
23 |
RICSU
|
Richter Supermarkt
|
39.37336425362274
|
24 |
RICAR
|
Ricardo Adocicados
|
39.463107825101716
|
25 |
REGGC
|
Reggiani Caseifici
|
39.552744546817955
|
26 |
RATTC
|
Rattlesnake Canyon Grocery
|
39.64227456602885
|
27 |
RANCH
|
Rancho grande
|
39.731698030198665
|
28 |
QUICK
|
QUICK-Stop
|
39.82101508699513
|
29 |
QUEEN
|
Queen Cozinha
|
39.910225884285275
|
30 |
QUEDE
|
Que Delícia
|
39.99933057013172
|
31 |
PRINI
|
Princesa Isabel Vinhos
|
40.088329292789105
|
32 |
PICCO
|
Piccolo und mehr
|
40.1772222006997
|
33 |
PERIC
|
Pericles Comidas clásicas
|
40.2660094424902
|
34 |
PARIS
|
Paris spécialités
|
40.35469116696769
|
35 |
OTTIK
|
Ottilies Käseladen
|
40.44326752311585
|
36 |
OLDWO
|
Old World Delicatessen
|
40.531738660091456
|
37 |
OCEAN
|
Océano Atlántico Ltda.
|
40.620104727220166
|
38 |
NORTS
|
North/South
|
40.70836587399342
|
39 |
MORGK
|
Morgenstern Gesundkost
|
40.79652225006443
|
40 |
MEREP
|
Mère Paillarde
|
40.88457400524406
|
41 |
MAISD
|
Maison Dewey
|
40.97252128949825
|
42 |
MAGAA
|
Magazzini Alimentari Riuniti
|
41.060364252943025
|
43 |
LONEP
|
Lonesome Pine Restaurant
|
41.14810304584199
|
44 |
LINOD
|
LINO-Delicateses
|
41.235737818602125
|
45 |
LILAS
|
LILA-Supermercado
|
41.323268721769914
|
46 |
LETSS
|
Let's Stop N Shop
|
41.41069590602867
|
47 |
LEHMS
|
Lehmanns Marktstand
|
41.49801952219388
|
48 |
LAZYK
|
Lazy K Kountry Store
|
41.58523972121036
|
49 |
LAUGB
|
Laughing Bacchus Wine Cellars
|
41.67235665414839
|
50 |
LAMAI
|
La maison d'Asie
|
41.75937047220008
|
51 |
LACOR
|
La corne d'abondance
|
41.846281326676376
|
52 |
KOENE
|
Königlich Essen
|
41.93308936900272
|
53 |
ISLAT
|
Island Trading
|
42.01979475071619
|
54 |
HUNGO
|
Hungry Owl All-Night Grocers
|
42.10639762346177
|
55 |
HUNGC
|
Hungry Coyote Import Store
|
42.192898138989
|
56 |
HILAA
|
HILARION-Abastos
|
42.27929644914833
|
57 |
HANAR
|
Hanari Carnes
|
42.36559270588759
|
58 |
GROSR
|
GROSELLA-Restaurante
|
42.451787061249036
|
59 |
GREAL
|
Great Lakes Food Market
|
42.53787966736557
|
60 |
GOURL
|
Gourmet Lanchonetes
|
42.62387067645722
|
61 |
GODOS
|
Godos Cocina Típica
|
42.70976024082825
|
62 |
GALED
|
Galería del gastrónomo
|
42.79554851286302
|
63 |
FURIB
|
Furia Bacalhau e Frutos do Mar
|
42.88123564502348
|
64 |
FRANK
|
Frankenversand
|
42.966821789845255
|
65 |
FRANS
|
Franchi S.p.A.
|
43.05230709993453
|
66 |
FRANR
|
France restauration
|
43.13769172796471
|
67 |
FOLKO
|
Folk och fä HB
|
43.22297582667305
|
68 |
FOLIG
|
Folies gourmandes
|
43.308159548857155
|
69 |
FISSA
|
FISSA Fabrica Inter. Salchichas S.A.
|
43.39324304737284
|
70 |
FAMIA
|
Familia Arquibaldo
|
43.478226475128665
|
71 |
ERNSH
|
Ernst Handel
|
43.5631099850855
|
72 |
EASTC
|
Eastern Connection
|
43.64789373025069
|
73 |
DUMON
|
Du monde entier
|
43.73257786367631
|
74 |
DRACD
|
Drachenblut Delikatessen
|
43.81716253845612
|
75 |
WANDK
|
Die Wandernde Kuh
|
43.901647907720914
|
76 |
CONSH
|
Consolidated Holdings
|
43.98603412463761
|
77 |
COMMI
|
Comércio Mineiro
|
44.07032134240359
|
78 |
CHOPS
|
Chop-suey Chinese
|
44.154509714245464
|
79 |
CENTC
|
Centro comercial Moctezuma
|
44.238599393415214
|
80 |
CACTU
|
Cactus Comidas para llevar
|
44.32259053318647
|
81 |
BSBEV
|
B's Beverages
|
44.40648328685333
|
82 |
BOTTM
|
Bottom-Dollar Markets
|
44.490277807724965
|
83 |
BONAP
|
Bon app'
|
44.57397424912381
|
84 |
BOLID
|
Bólido Comidas preparadas
|
44.65757276438276
|
85 |
BLONP
|
Blondesddsl père et fils
|
44.74107350684044
|
86 |
BLAUS
|
Blauer See Delikatessen
|
44.82447662984122
|
87 |
BERGS
|
Berglunds snabbköp
|
44.90778228672868
|
88 |
AROUT
|
Around the Horn
|
44.99099063084514
|
89 |
ANTON
|
Antonio Moreno Taquería
|
45.07410181552965
|
90 |
ANATR
|
Ana Trujillo Emparedados y helados
|
45.157115994106555
|
91 |
ALFKI
|
Alfreds Futterkiste
|
45.24003331990159
|