-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW2.sql
38 lines (31 loc) · 1.08 KB
/
HW2.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
--1
DECLARE @pn nvarchar(50) = 'someName'
SELECT DISTINCT P.ParkingNo, P.ParkingName, CONVERT(date, PD.date_start)
FROM Parking P LEFT JOIN ParkingData PD ON P.ParkingNo = PD.ParkingNo
WHERE P.ParkingName = @pn
--2
SELECT PD.CarID, C.RegNo, COUNT(*), COUNT(DISTINCT PD.p_ID), SUM(DATEDIFF(s , PD.date_start, CASE WHEN PD.date_end IS NOT NULL THEN PD.date_end ELSE GETDATE() END))
FROM ParkingData PD LEFT JOIN Cars C ON PD.CarID = C.CarID
GROUP BY PD.CarID
--3
DECLARE @date date = '2017-01-01'
SELECT DISTINCT ParkingNo
FROM ParkingData
EXEPT
SELECT DISTINCT PD.ParkingNo
FROM ParkingData PD LEFT JOIN Cars C ON PD.CarID = C.CarID
WHERE C.RegNo like '%777' AND PD.date_start > @date
--4
DECLARE @Date date = '2017-01-01'
SELECT DISTINCT C.CarID, C.RegNo, COUNT(PD.ParkingNo)
FROM Cars C LEFT JOIN ParkingData PD ON PD.CarID = C.CarID
WHERE PD.date_start > @date
GROUP BY C.CarID, C.RegNo
HAVING COUNT(PD.ParkingNo) < 3
--5
ALTER TABLE Ñars ADD COLUMN Total_ent int
INSERT INTO Ñars(Total_ent) (
SELECT count(*)
FROM Cars INNER JOIN ParkingData USING Cars.CarID = ParkingData.CarID
GROUP BY CarID
) as T