-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab3_2.sql
131 lines (119 loc) · 2.97 KB
/
Lab3_2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Variant #9
group 651003
Kornienko Anastasia
*/
USE AdventureWorks2012;
GO
SELECT * FROM dbo.StateProvince;
GO
--pont a)
ALTER TABLE dbo.StateProvince
ADD TaxRate SMALLMONEY,
CurrencyCode NCHAR(3),
AvarageRate MONEY,
IntTaxRate AS CEILING(TaxRate);
SELECT * FROM dbo.StateProvince;
GO
--point b)
CREATE TABLE dbo.#StateProvince (
StateProvinceID INT,
StateProvinceCode NCHAR(3),
CountryRegionCode NVARCHAR(3),
IsOnlyStateProvinceFlag SMALLINT,
Name NVARCHAR(50),
TerritoryID INT,
ModifiedDate DATETIME,
TaxRate SMALLMONEY,
CurrencyCode NCHAR(3),
AvarageRate MONEY,
PRIMARY KEY (StateProvinceID)
);
SELECT * FROM dbo.#StateProvince;
GO
--point c)
WITH RATE_CTE AS (
SELECT MAX(CurrencyRate.AverageRate) MaxRate,
ToCurrencyCode
FROM Sales.CurrencyRate CurrencyRate
GROUP BY ToCurrencyCode
)
INSERT INTO dbo.#StateProvince(
StateProvinceID,
StateProvinceCode,
CountryRegionCode,
IsOnlyStateProvinceFlag,
Name,
TerritoryID,
ModifiedDate,
TaxRate,
CurrencyCode,
AvarageRate
)
SELECT StateProvince.StateProvinceID,
StateProvinceCode,
StateProvince.CountryRegionCode,
IsOnlyStateProvinceFlag,
StateProvince.Name,
TerritoryID,
StateProvince.ModifiedDate,
ISNULL(SalesTaxRate.TaxRate, 0),
Currency.CurrencyCode,
rate.MaxRate
FROM dbo.StateProvince StateProvince
JOIN Sales.CountryRegionCurrency CountryRegionCurrency
ON CountryRegionCurrency.CountryRegionCode = StateProvince.CountryRegionCode
JOIN Sales.Currency Currency
ON Currency.CurrencyCode = CountryRegionCurrency.CurrencyCode
JOIN RATE_CTE rate
ON CountryRegionCurrency.CurrencyCode = rate.ToCurrencyCode
LEFT JOIN Sales.SalesTaxRate SalesTaxRate
ON SalesTaxRate.StateProvinceID = StateProvince.StateProvinceID
WHERE SalesTaxRate.TaxType = 1 OR SalesTaxRate.TaxType IS NULL;
GO
SELECT * FROM dbo.#StateProvince;
SELECT * FROM dbo.StateProvince;
--point d)
DELETE FROM dbo.StateProvince WHERE CountryRegionCode = 'CA';
SELECT * FROM dbo.StateProvince;
SELECT * FROM dbo.#StateProvince;
--point e)
SET IDENTITY_INSERT dbo.StateProvince ON
MERGE INTO dbo.StateProvince targ
USING dbo.#StateProvince src
ON targ.StateProvinceID = src.StateProvinceID
WHEN MATCHED THEN UPDATE SET
targ.TaxRate = src.TaxRate,
targ.CurrencyCode = src.CurrencyCode,
targ.AvarageRate = src.AvarageRate
WHEN NOT MATCHED BY TARGET THEN
INSERT (
StateProvinceID,
StateProvinceCode,
CountryRegionCode,
IsOnlyStateProvinceFlag,
Name,
TerritoryID,
ModifiedDate,
TaxRate,
CurrencyCode,
AvarageRate
)
VALUES (
src.StateProvinceID,
src.StateProvinceCode,
src.CountryRegionCode,
src.IsOnlyStateProvinceFlag,
src.Name,
src.TerritoryID,
src.ModifiedDate,
src.TaxRate,
src.CurrencyCode,
src.AvarageRate
)
WHEN NOT MATCHED BY SOURCE THEN DELETE;
SET IDENTITY_INSERT dbo.StateProvince OFF
GO
SELECT * FROM dbo.StateProvince;
SELECT * FROM dbo.#StateProvince;
GO