Other ATAN SQL function
1. Usage of ATAN to show directions in radians and degree
SQL Server Query 1
WITH CustomerCoordinates AS (
SELECT
CustomerID,
CompanyName,
-- Simulated Latitude/Longitude difference from the central point
(47.6062 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) * 0.5)) - 47.6062 AS LatitudeDifference,
(-122.3321 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) * 0.8)) - -122.3321 AS LongitudeDifference
FROM Customers
),
DirectionCalculation AS (
SELECT
CustomerID,
CompanyName,
LatitudeDifference,
LongitudeDifference,
-- Calculate a simplified direction using ATAN
ATAN(LongitudeDifference / LatitudeDifference) AS DirectionInRadians,
DEGREES(ATAN(LongitudeDifference / LatitudeDifference)) AS DirectionInDegrees
FROM CustomerCoordinates
)
SELECT
CustomerID,
CompanyName,
DirectionInRadians,
DirectionInDegrees
FROM DirectionCalculation
ORDER BY DirectionInDegrees;
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.WithCTETable(new Table("CustomerCoordinates"), new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "CompanyName")
.Column(new ColumnArithmatic()
.StartBracket()
.Value(47.6062).ADD()
.StartBracket()
.SqlFunction(new ROW_NUMBER().ORDER_BY(new OrderBy().SetSelectNull()))
.EndBracket().MULTIPLY().Value(0.5)
.EndBracket().SUBTRACT().Value(47.6062)
, "LatitudeDifference")
.Column(new ColumnArithmatic()
.StartBracket()
.Value(-122.3321).ADD()
.StartBracket()
.SqlFunction(new ROW_NUMBER().ORDER_BY(new OrderBy().SetSelectNull()))
.EndBracket().MULTIPLY().Value(0.8)
.EndBracket().SUBTRACT().Value(-122.3321)
, "LongitudeDifference")
.From("Customers")
)
.WithCTETable(new Table("DirectionCalculation"), new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "CompanyName", "LatitudeDifference", "LongitudeDifference")
.Column(new ATAN(new ColumnArithmatic().Column("LongitudeDifference").DIVIDE().Column("LatitudeDifference"))
, "DirectionInRadians")
.Column(new DEGREES(new ATAN(new ColumnArithmatic().Column("LongitudeDifference").DIVIDE().Column("LatitudeDifference")))
, "DirectionInDegrees")
.From("CustomerCoordinates")
)
.Select()
.Columns("CustomerID", "CompanyName", "DirectionInRadians", "DirectionInDegrees")
.From("DirectionCalculation")
.OrderBy(new OrderBy().Set(new Column("DirectionInDegrees")))
.Build();
Query build by SqlQueryBuilder 1
WITH CustomerCoordinates
AS (SELECT CustomerID,
CompanyName,
(@pMAIN_2507192103022644500 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) * @pMAIN_2507192103022644501) - @pMAIN_2507192103022644502 AS LatitudeDifference,
(@pMAIN_2507192103022644503 + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) * @pMAIN_2507192103022644504) - @pMAIN_2507192103022644505 AS LongitudeDifference
FROM Customers),
DirectionCalculation
AS (SELECT CustomerID,
CompanyName,
LatitudeDifference,
LongitudeDifference,
ATAN(LongitudeDifference / LatitudeDifference) AS DirectionInRadians,
DEGREES(ATAN(LongitudeDifference / LatitudeDifference)) AS DirectionInDegrees
FROM CustomerCoordinates)
SELECT CustomerID,
CompanyName,
DirectionInRadians,
DirectionInDegrees
FROM DirectionCalculation
ORDER BY DirectionInDegrees ASC;
Parameters (If used)
Name |
Value |
@pMAIN_2507192103022644500 |
47.6062 |
@pMAIN_2507192103022644501 |
0.5 |
@pMAIN_2507192103022644502 |
47.6062 |
@pMAIN_2507192103022644503 |
-122.3321 |
@pMAIN_2507192103022644504 |
0.8 |
@pMAIN_2507192103022644505 |
-122.3321 |
Query Results 1:
|
CustomerID |
CompanyName |
DirectionInRadians |
DirectionInDegrees |
1 |
ALFKI
|
Alfreds Futterkiste
|
1.0121970114513326
|
57.994616791916414
|
2 |
ANATR
|
Ana Trujillo Emparedados y helados
|
1.0121970114513326
|
57.994616791916414
|
3 |
BLONP
|
Blondesddsl père et fils
|
1.0121970114513337
|
57.99461679191648
|
4 |
BLAUS
|
Blauer See Delikatessen
|
1.012197011451334
|
57.99461679191649
|
5 |
CACTU
|
Cactus Comidas para llevar
|
1.012197011451334
|
57.99461679191649
|
6 |
BOTTM
|
Bottom-Dollar Markets
|
1.0121970114513341
|
57.9946167919165
|
7 |
BSBEV
|
B's Beverages
|
1.0121970114513341
|
57.9946167919165
|
8 |
COMMI
|
Comércio Mineiro
|
1.0121970114513341
|
57.9946167919165
|
9 |
CONSH
|
Consolidated Holdings
|
1.0121970114513341
|
57.9946167919165
|
10 |
WANDK
|
Die Wandernde Kuh
|
1.0121970114513341
|
57.9946167919165
|
11 |
BERGS
|
Berglunds snabbköp
|
1.0121970114513341
|
57.9946167919165
|
12 |
EASTC
|
Eastern Connection
|
1.0121970114513341
|
57.9946167919165
|
13 |
ERNSH
|
Ernst Handel
|
1.0121970114513341
|
57.9946167919165
|
14 |
FAMIA
|
Familia Arquibaldo
|
1.0121970114513341
|
57.9946167919165
|
15 |
FOLKO
|
Folk och fä HB
|
1.0121970114513341
|
57.9946167919165
|
16 |
FRANR
|
France restauration
|
1.0121970114513341
|
57.9946167919165
|
17 |
FRANS
|
Franchi S.p.A.
|
1.0121970114513341
|
57.9946167919165
|
18 |
FURIB
|
Furia Bacalhau e Frutos do Mar
|
1.0121970114513341
|
57.9946167919165
|
19 |
GALED
|
Galería del gastrónomo
|
1.0121970114513341
|
57.9946167919165
|
20 |
GODOS
|
Godos Cocina Típica
|
1.0121970114513341
|
57.9946167919165
|
21 |
GOURL
|
Gourmet Lanchonetes
|
1.0121970114513341
|
57.9946167919165
|
22 |
GROSR
|
GROSELLA-Restaurante
|
1.0121970114513341
|
57.9946167919165
|
23 |
HANAR
|
Hanari Carnes
|
1.0121970114513341
|
57.9946167919165
|
24 |
HILAA
|
HILARION-Abastos
|
1.0121970114513341
|
57.9946167919165
|
25 |
HUNGC
|
Hungry Coyote Import Store
|
1.0121970114513341
|
57.9946167919165
|
26 |
ISLAT
|
Island Trading
|
1.0121970114513341
|
57.9946167919165
|
27 |
KOENE
|
Königlich Essen
|
1.0121970114513341
|
57.9946167919165
|
28 |
LAMAI
|
La maison d'Asie
|
1.0121970114513341
|
57.9946167919165
|
29 |
LAZYK
|
Lazy K Kountry Store
|
1.0121970114513341
|
57.9946167919165
|
30 |
LEHMS
|
Lehmanns Marktstand
|
1.0121970114513341
|
57.9946167919165
|
31 |
LILAS
|
LILA-Supermercado
|
1.0121970114513341
|
57.9946167919165
|
32 |
LONEP
|
Lonesome Pine Restaurant
|
1.0121970114513341
|
57.9946167919165
|
33 |
MAGAA
|
Magazzini Alimentari Riuniti
|
1.0121970114513341
|
57.9946167919165
|
34 |
MEREP
|
Mère Paillarde
|
1.0121970114513341
|
57.9946167919165
|
35 |
OTTIK
|
Ottilies Käseladen
|
1.0121970114513341
|
57.9946167919165
|
36 |
PARIS
|
Paris spécialités
|
1.0121970114513341
|
57.9946167919165
|
37 |
PERIC
|
Pericles Comidas clásicas
|
1.0121970114513341
|
57.9946167919165
|
38 |
PICCO
|
Piccolo und mehr
|
1.0121970114513341
|
57.9946167919165
|
39 |
QUEDE
|
Que Delícia
|
1.0121970114513341
|
57.9946167919165
|
40 |
QUEEN
|
Queen Cozinha
|
1.0121970114513341
|
57.9946167919165
|
41 |
QUICK
|
QUICK-Stop
|
1.0121970114513341
|
57.9946167919165
|
42 |
RANCH
|
Rancho grande
|
1.0121970114513341
|
57.9946167919165
|
43 |
REGGC
|
Reggiani Caseifici
|
1.0121970114513341
|
57.9946167919165
|
44 |
RICAR
|
Ricardo Adocicados
|
1.0121970114513341
|
57.9946167919165
|
45 |
RICSU
|
Richter Supermarkt
|
1.0121970114513341
|
57.9946167919165
|
46 |
ROMEY
|
Romero y tomillo
|
1.0121970114513341
|
57.9946167919165
|
47 |
SAVEA
|
Save-a-lot Markets
|
1.0121970114513341
|
57.9946167919165
|
48 |
SEVES
|
Seven Seas Imports
|
1.0121970114513341
|
57.9946167919165
|
49 |
SIMOB
|
Simons bistro
|
1.0121970114513341
|
57.9946167919165
|
50 |
SPECD
|
Spécialités du monde
|
1.0121970114513341
|
57.9946167919165
|
51 |
SPLIR
|
Split Rail Beer & Ale
|
1.0121970114513341
|
57.9946167919165
|
52 |
SUPRD
|
Suprêmes délices
|
1.0121970114513341
|
57.9946167919165
|
53 |
THEBI
|
The Big Cheese
|
1.0121970114513341
|
57.9946167919165
|
54 |
THECR
|
The Cracker Box
|
1.0121970114513341
|
57.9946167919165
|
55 |
TOMSP
|
Toms Spezialitäten
|
1.0121970114513341
|
57.9946167919165
|
56 |
TORTU
|
Tortuga Restaurante
|
1.0121970114513341
|
57.9946167919165
|
57 |
TRAIH
|
Trail's Head Gourmet Provisioners
|
1.0121970114513341
|
57.9946167919165
|
58 |
VAFFE
|
Vaffeljernet
|
1.0121970114513341
|
57.9946167919165
|
59 |
VICTE
|
Victuailles en stock
|
1.0121970114513341
|
57.9946167919165
|
60 |
VINET
|
Vins et alcools Chevalier
|
1.0121970114513341
|
57.9946167919165
|
61 |
WARTH
|
Wartian Herkku
|
1.0121970114513341
|
57.9946167919165
|
62 |
WELLI
|
Wellington Importadora
|
1.0121970114513341
|
57.9946167919165
|
63 |
WHITC
|
White Clover Markets
|
1.0121970114513341
|
57.9946167919165
|
64 |
WILMK
|
Wilman Kala
|
1.0121970114513341
|
57.9946167919165
|
65 |
WOLZA
|
Wolski Zajazd
|
1.0121970114513341
|
57.9946167919165
|
66 |
NORTS
|
North/South
|
1.0121970114513341
|
57.9946167919165
|
67 |
OCEAN
|
Océano Atlántico Ltda.
|
1.0121970114513341
|
57.9946167919165
|
68 |
OLDWO
|
Old World Delicatessen
|
1.0121970114513343
|
57.99461679191651
|
69 |
CHOPS
|
Chop-suey Chinese
|
1.0121970114513343
|
57.99461679191651
|
70 |
TRADH
|
Tradição Hipermercados
|
1.0121970114513343
|
57.99461679191651
|
71 |
SANTG
|
Santé Gourmet
|
1.0121970114513343
|
57.99461679191651
|
72 |
RATTC
|
Rattlesnake Canyon Grocery
|
1.0121970114513343
|
57.99461679191651
|
73 |
PRINI
|
Princesa Isabel Vinhos
|
1.0121970114513343
|
57.99461679191651
|
74 |
MORGK
|
Morgenstern Gesundkost
|
1.0121970114513343
|
57.99461679191651
|
75 |
MAISD
|
Maison Dewey
|
1.0121970114513343
|
57.99461679191651
|
76 |
LINOD
|
LINO-Delicateses
|
1.0121970114513343
|
57.99461679191651
|
77 |
LETSS
|
Let's Stop N Shop
|
1.0121970114513343
|
57.99461679191651
|
78 |
LAUGB
|
Laughing Bacchus Wine Cellars
|
1.0121970114513343
|
57.99461679191651
|
79 |
LACOR
|
La corne d'abondance
|
1.0121970114513343
|
57.99461679191651
|
80 |
HUNGO
|
Hungry Owl All-Night Grocers
|
1.0121970114513343
|
57.99461679191651
|
81 |
GREAL
|
Great Lakes Food Market
|
1.0121970114513343
|
57.99461679191651
|
82 |
FRANK
|
Frankenversand
|
1.0121970114513343
|
57.99461679191651
|
83 |
FISSA
|
FISSA Fabrica Inter. Salchichas S.A.
|
1.0121970114513343
|
57.99461679191651
|
84 |
FOLIG
|
Folies gourmandes
|
1.0121970114513343
|
57.99461679191651
|
85 |
BONAP
|
Bon app'
|
1.0121970114513343
|
57.99461679191651
|
86 |
DRACD
|
Drachenblut Delikatessen
|
1.0121970114513343
|
57.99461679191651
|
87 |
DUMON
|
Du monde entier
|
1.0121970114513343
|
57.99461679191651
|
88 |
CENTC
|
Centro comercial Moctezuma
|
1.0121970114513346
|
57.99461679191653
|
89 |
BOLID
|
Bólido Comidas preparadas
|
1.0121970114513346
|
57.99461679191653
|
90 |
AROUT
|
Around the Horn
|
1.0121970114513346
|
57.99461679191653
|
91 |
ANTON
|
Antonio Moreno Taquería
|
1.0121970114513352
|
57.99461679191656
|