In [45]:
--Mõned tekstfunktsioonid
SELECT TOP 5 Code +', '+ CountryName as Koos, Concat(Code, ', ',CountryName) Koos_Funktsiooniga,
    LEFT(CountryName,1) as _1_taht, Upper(RIGHT(CountryName,1)) as Viimane_taht,
    SUBSTRING(CountryName,1,3) as Kolm_tahti, LEN(CountryName) as Pikkus
FROM tCountry
(5 rows affected)
Total execution time: 00:00:00.009
Out[45]:
KoosKoos_Funktsiooniga_1_tahtViimane_tahtKolm_tahtiPikkus
ABW, ArubaABW, ArubaAAAru5
AFG, AfghanistanAFG, AfghanistanANAfg11
AGO, AngolaAGO, AngolaAAAng6
AIA, AnguillaAIA, AnguillaAAAng8
ALB, AlbaniaALB, AlbaniaAAAlb7
In [46]:
---Kuupäeva funktsioonid
SELECT getdate() as Aeg, CURRENT_TIMESTAMP as aeg2,
    YEAR(getdate()) as Aasta, MONTH(getdate()) as Kuu, DATEPART(year, getdate()) as Aasta,
    DATEDIFF(day,getdate(),'2022-01-01') Vahe_paevades
(1 row affected)
Total execution time: 00:00:00.010
Out[46]:
Aegaeg2AastaKuuAastaVahe_paevades
2021-12-06 18:06:04.4402021-12-06 18:06:04.440202112202126
In [23]:
--Teiseldamise funktsioonid
SELECT CAST(GETDATE() as varchar(50) ) as tekst,
        CONVERT(varchar(50), getdate()) as tekst
(1 row affected)
Total execution time: 00:00:00.007
Out[23]:
teksttekst
Dec 6 2021 2:25PMDec 6 2021 2:25PM
In [24]:
--Suurte linnade arv kontinetdi ja regiooni kaupa
SELECT R.Continent, R.Region, COUNT(L.ID) as COuntCities
FROM tCountry R JOIN tCity AS L ON L.CountryCode=R.Code
WHERE L.Population>=100000
GROUP BY  R.Continent, R.Region
(21 rows affected)
Total execution time: 00:00:00.020
Out[24]:
ContinentRegionCOuntCities
AfricaNorthern Africa90
South AmericaSouth America416
EuropeEastern Europe325
OceaniaMelanesia1
North AmericaCentral America168
OceaniaAustralia and New Zealand21
EuropeBaltic Countries15
North AmericaNorth America290
AsiaSoutheast Asia254
EuropeWestern Europe162
AfricaEastern Africa55
North AmericaCaribbean32
EuropeBritish Islands66
EuropeNordic Countries27
AsiaMiddle East156
AfricaSouthern Africa44
AfricaCentral Africa35
AsiaEastern Asia689
AfricaWestern Africa100
EuropeSouthern Europe124
AsiaSouthern and Central Asia498
In [50]:
--With CUBE
SELECT ISNULL(R.Continent,'Region Total') as Continent , ISNULL(R.Region,'Continent Total') AS Region, COUNT(L.ID) as CountCities
FROM tCountry R JOIN tCity AS L ON L.CountryCode=R.Code
WHERE L.Population>=100000
GROUP BY  R.Continent, R.Region WITH CUBE
ORDER BY Continent
(49 rows affected)
Total execution time: 00:00:00.016
Out[50]:
ContinentRegionCountCities
AfricaCentral Africa35
AfricaEastern Africa55
AfricaNorthern Africa90
AfricaSouthern Africa44
AfricaWestern Africa100
AfricaContinent Total324
AsiaContinent Total1597
AsiaSouthern and Central Asia498
AsiaSoutheast Asia254
AsiaMiddle East156
AsiaEastern Asia689
EuropeBaltic Countries15
EuropeBritish Islands66
EuropeNordic Countries27
EuropeEastern Europe325
EuropeSouthern Europe124
EuropeWestern Europe162
EuropeContinent Total719
North AmericaContinent Total490
North AmericaNorth America290
North AmericaCaribbean32
North AmericaCentral America168
OceaniaAustralia and New Zealand21
OceaniaMelanesia1
OceaniaContinent Total22
Region TotalSouth America416
Region TotalWestern Europe162
Region TotalContinent Total3568
Region TotalWestern Africa100
Region TotalSouthern Europe124
Region TotalSouthern and Central Asia498
Region TotalMelanesia1
Region TotalEastern Europe325
Region TotalNordic Countries27
Region TotalMiddle East156
Region TotalNorth America290
Region TotalNorthern Africa90
Region TotalSoutheast Asia254
Region TotalSouthern Africa44
Region TotalAustralia and New Zealand21
Region TotalBaltic Countries15
Region TotalCaribbean32
Region TotalBritish Islands66
Region TotalCentral America168
Region TotalCentral Africa35
Region TotalEastern Asia689
Region TotalEastern Africa55
South AmericaSouth America416
South AmericaContinent Total416
In [51]:
--With ROLLUP
SELECT ISNULL(R.Continent,'Region Total')AS Continent, ISNULL(R.Region,'Continent Total') AS Region, COUNT(L.ID) as CountCities
FROM tCountry R JOIN tCity AS L ON L.CountryCode=R.Code
WHERE L.Population>=100000
GROUP BY  R.Continent, R.Region WITH ROLLUP
ORDER BY Continent
(28 rows affected)
Total execution time: 00:00:00.019
Out[51]:
ContinentRegionCountCities
AfricaCentral Africa35
AfricaEastern Africa55
AfricaNorthern Africa90
AfricaSouthern Africa44
AfricaWestern Africa100
AfricaContinent Total324
AsiaEastern Asia689
AsiaMiddle East156
AsiaSoutheast Asia254
AsiaSouthern and Central Asia498
AsiaContinent Total1597
EuropeBaltic Countries15
EuropeBritish Islands66
EuropeEastern Europe325
EuropeNordic Countries27
EuropeSouthern Europe124
EuropeWestern Europe162
EuropeContinent Total719
North AmericaCaribbean32
North AmericaCentral America168
North AmericaNorth America290
North AmericaContinent Total490
OceaniaAustralia and New Zealand21
OceaniaMelanesia1
OceaniaContinent Total22
Region TotalContinent Total3568
South AmericaSouth America416
South AmericaContinent Total416
In [39]:
--PIVOT TABLE - Kontinendi lõikes
SELECT  'Linnade Arv' as Continent,[Asia], [Europe], [Africa], [Oceania], [North America],[South America] --pais
FROM (SELECT Continent, L.ID 
        FROM tCountry as R INNER JOIN tCIty AS L ON L.COuntryCode=R.Code
        WHere L.Population >100000) AS Andmed --andmed
PIVOT (COUNT(ID) FOR 
    Continent IN ([Asia], [Europe], [Africa], [Oceania], [North America],[South America])) as Tabel
(1 row affected)
Total execution time: 00:00:00.058
Out[39]:
ContinentAsiaEuropeAfricaOceaniaNorth AmericaSouth America
Linnade Arv159571132422490416
In [42]:
--PIVOT TABLE - Kontinendi ja regioni lõikes
SELECT  Region 'Linnade Arv' ,[Asia], [Europe], [Africa], [Oceania], [North America],[South America] --pais
FROM (SELECT Region, Continent, L.ID 
        FROM tCountry as R INNER JOIN tCIty AS L ON L.COuntryCode=R.Code
        WHere L.Population >100000) AS Andmed --andmed
PIVOT (COUNT(ID) FOR 
    Continent IN ([Asia], [Europe], [Africa], [Oceania], [North America],[South America])) as Tabel
(21 rows affected)
Total execution time: 00:00:00.058
Out[42]:
Linnade ArvAsiaEuropeAfricaOceaniaNorth AmericaSouth America
Australia and New Zealand0002100
Baltic Countries090000
British Islands0650000
Caribbean0000320
Central Africa0035000
Central America00001680
Eastern Africa0055000
Eastern Asia68900000
Eastern Europe03240000
Melanesia000100
Middle East15600000
Nordic Countries0270000
North America00002900
Northern Africa0090000
South America00000416
Southeast Asia25300000
Southern Africa0044000
Southern and Central Asia49700000
Southern Europe01240000
Western Africa00100000
Western Europe01620000