--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
---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
--Teiseldamise funktsioonid
SELECT CAST(GETDATE() as varchar(50) ) as tekst,
CONVERT(varchar(50), getdate()) as tekst
--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
--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
--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
--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
--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