-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_03_update.sql
189 lines (152 loc) · 4.98 KB
/
03_03_update.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
select PhoneNumber,FaxNumber,*
from [Application].People
WHERE PersonId = 3
--simple
Update [Application].People
SET
PhoneNumber = '(495) 555-0102',
FaxNumber = '(495) 555-0103'
WHERE PersonId = 3
Update [Application].People
SET
PhoneNumber = '(415) 555-0102',
FaxNumber = '(415) 555-0103'
OUTPUT inserted.*, deleted.*
WHERE PersonId = 3
--PhoneNumber FaxNumber
--(415) 555-0102 (415) 555-0103
ALTER TABLE [Application].People ADD FirstSale DATETIME;
ALTER TABLE [Application].People ADD FirstSale2 DATETIME;
select firstSale, FullName From [Application].People;
UPDATE [Application].People
SET FirstSale = (SELECT MIN(InvoiceDate)
FROM Sales.Invoices AS I
WHERE [Application].People.PersonID = I.SalespersonPersonID);
SELECT FirstSale,FirstSale2,*
FROM [Application].People
WHERE FirstSale2 IS NOT NULL;
UPDATE P
SET FirstSale2 = I.MinInvoiceDate
FROM [Application].People AS P
JOIN
(SELECT SalespersonPersonID, MIN(InvoiceDate) AS MinInvoiceDate
FROM Sales.Invoices
GROUP BY SalespersonPersonID) AS I
ON P.PersonID = I.SalespersonPersonID;
Select P.FirstSale2, I.MinInvoiceDate
FROM [Application].People AS P
JOIN
(SELECT SalespersonPersonID, MIN(InvoiceDate) AS MinInvoiceDate
FROM Sales.Invoices
GROUP BY SalespersonPersonID) AS I
ON P.PersonID = I.SalespersonPersonID;
UPDATE [Application].People
SET FirstSale = NULL,
FirstSale2 = NULL;
ALTER TABLE [Application].People DROP COLUMN FirstSale;
ALTER TABLE [Application].People DROP COLUMN FirstSale2;
ALTER TABLE [Application].People ADD TotalSaleCount INT NOT NULL Default 0;
UPDATE P
SET TotalSaleCount = TotalSaleCount + I.SalesCount
FROM [Application].People AS P
JOIN
(SELECT SalespersonPersonID, Count(InvoiceId) AS SalesCount
FROM Sales.Invoices
WHERE InvoiceDate < '20140101'
GROUP BY SalespersonPersonID) AS I
ON P.PersonID = I.SalespersonPersonID;
SELECT TotalSaleCount,*
FROM [Application].People
WHERE TotalSaleCount > 0
UPDATE P
SET TotalSaleCount += I.SalesCount
OUTPUT inserted.PersonId, inserted.FullName,inserted.TotalSaleCount
FROM [Application].People AS P
JOIN
(SELECT SalespersonPersonID, Count(InvoiceId) AS SalesCount
FROM Sales.Invoices
WHERE InvoiceDate >= '20140101'
AND InvoiceDate < '20150101'
GROUP BY SalespersonPersonID) AS I
ON P.PersonID = I.SalespersonPersonID;
UPDATE TOP (5) P
SET TotalSaleCount += I.SalesCount
OUTPUT inserted.PersonId, inserted.FullName,inserted.TotalSaleCount
FROM [Application].People AS P
JOIN
(SELECT SalespersonPersonID, Count(InvoiceId) AS SalesCount
FROM Sales.Invoices
WHERE InvoiceDate >= '20150401'
AND InvoiceDate < '20150801'
GROUP BY SalespersonPersonID) AS I
ON P.PersonID = I.SalespersonPersonID;
ALTER TABLE [Application].[People] DROP CONSTRAINT [DF__People__TotalSal__79C80F94];--[DF__People__TotalSal__77DFC722]
ALTER TABLE [Application].People DROP COLUMN TotalSaleCount;
UPDATE TOP (100) Website.Customers
SET CustomerName = 'New Great Shop for something'--Biju Deb
WHERE CustomerID = 842;
SELECT *
FROM Website.Customers
WHERE CustomerID = 842
SELECT *
FROM Sales.Customers
WHERE CustomerID = 842
ALTER TABLE Sales.Customers ADD CustomerCategoryName NVARCHAR(50);
WITH Customers AS
(
SELECT s.CustomerID,
s.CustomerName,
sc.CustomerCategoryName AS sourceCustomerCategoryName,
s.CustomerCategoryName
FROM Sales.Customers AS s
LEFT JOIN Sales.CustomerCategories AS sc
ON s.CustomerCategoryID = sc.CustomerCategoryID
)
UPDATE Customers
SET CustomerCategoryName = sourceCustomerCategoryName;
SELECT CustomerCategoryName,*
FROM Sales.Customers;
ALTER TABLE Sales.Customers DROP COLUMN CustomerCategoryName;
create table Sales.TotalSalesForDay (SalesDate DATE, SalesSum DECIMAL(18,2));
create table Sales.InvoiceBucket (InvoiceDate DATE, UnitPrice DECIMAL(18,2));
INSERT INTO Sales.InvoiceBucket
(InvoiceDate, UnitPrice)
SELECT I.InvoiceDate, IL.UnitPrice
FROM Sales.InvoiceLines AS IL
join Sales.Invoices AS I
ON I.InvoiceID = IL.InvoiceID
WHERE I.InvoiceDate >= '20130103'
AND I.InvoiceDate < '20130105'
ORDER BY InvoiceDate ---!!!!!!! Ѕудет ли соблюден пор¤док?
SELECT InvoiceDate, Sum(UnitPrice)
FROM Sales.InvoiceBucket
GROUP BY InvoiceDate
ORDER BY InvoiceDate
select *
from Sales.InvoiceBucket
select *
from Sales.TotalSalesForDay;
SELECT InvoiceDate, Sum(UnitPrice) AS TotalSumm
FROM Sales.InvoiceBucket
GROUP BY InvoiceDate
--insert into Sales.TotalSalesForDay (SalesDate, SalesSum) VALUES (DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0),0)
select * from Sales.TotalSalesForDay
UPDATE T
SET SalesSum = B.TotalSumm
FROM Sales.TotalSalesForDay AS T
JOIN
(SELECT InvoiceDate, Sum(UnitPrice) AS TotalSumm
FROM Sales.InvoiceBucket
GROUP BY InvoiceDate) AS B
ON T.SalesDate = DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
select *
from Sales.TotalSalesForDay;
UPDATE T
SET SalesSum = B.TotalSumm
FROM Sales.TotalSalesForDay AS T
JOIN
(SELECT InvoiceDate, Sum(UnitPrice) AS TotalSumm
FROM Sales.InvoiceBucket
GROUP BY CustomerId, InvoiceDate
) AS B
ON T.CustomerId = B.CustomerId