-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL
193 lines (140 loc) · 3.12 KB
/
MySQL
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
create table t1(
data_vihoda datetime NOT NULL,
tutorial_author varchar(40) ,
PRIMARY KEY ( data_vihoda )
);
insert into t1
values ('11-02-19', 'asdasda'),
('11-02-20', 'asda')
;
drop table t2;
select * from t1;
create table t2(
data_oper datetime NOT NULL,
fl_sotr int ,
PRIMARY KEY ( data_oper )
);
insert into t2
values ('11-02-19', 0),
('11-05-23', 1),
('11-03-23', 3),
('11-07-23', 4)
;
select * from t2;
select *
from t1 join t2 on 1=1
where fl_sotr > 0;
select sum(fl_sotr) over()
from t2;
drop table count_sovp;
create table count_sovp(
data_vihoda datetime NOT NULL,
tutorial_author varchar(40)
);
insert into count_sovp
values ('11-02-19', 'asdasda'),
('11-02-20', 'asda')
;
insert into count_sovp
values ('11-02-19', 'asdasda')
;
select * from count_sovp;
WITH my_col AS (
SELECT COLUMN_NAME
FROM information_schema.columns
WHERE table_schema='d' AND table_name='count_sovp'
)
select sum(NoOfDuplicates) from (
SELECT count(*)-1 as NoOfDuplicates
FROM count_sovp
group by (select * from my_col)
) a
;
SELECT COUNT(*) AS NoOfOccurrences
FROM count_sovp
GROUP BY (SELECT column_name
FROM user_tab_cols
WHERE table_name = 'count_sovp'
)
HAVING COUNT(*) > 1
;
SELECT *, row_number() over (partition by *) rownum
FROM count_sovp
;
SELECT COLUMN_NAME
FROM information_schema.columns
WHERE table_schema='d' AND table_name='count_sovp'
;
WITH my_col AS (
SELECT COLUMN_NAME
FROM information_schema.columns
WHERE table_schema='d' AND table_name='count_sovp'
)
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (
a INT,
b INT
);
INSERT INTO
t1(a,b)
VALUES
(1,1),
(1,2),
(1,3),
(2,1),
(1,2),
(1,3),
(2,1),
(2,2);
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY a,b) rownum
FROM
t1
)
SELECT
*
FROM
cte
WHERE
rownum > 1;
SELECT column_name
FROM user_tab_cols
WHERE table_name = 'count_sovp';
create table users_hits (
user_id INT,
url VARCHAR(30) NOT NULL,
timestamp DATETIME
);
INSERT INTO
users_hits(user_id, url, timestamp)
VALUES
(1, 'aa','20-02-01'),
(1, 'https://music.yandex.ru/feed','20-02-07 11:49:04'),
(1, 'https://music.yandex.ru/feed','20-02-01 16:49:04'),
(2, 'bb','20-02-03'),
(3, 'https://music.yandex.ru/feed','20-02-01'),
(3, 'https://music.yandex.ru/feed','20-02-01'),
(3, 'cc','20-02-19'),
(5, 'aaa','20-02-07'),
(5, 'ada','20-02-01'),
(5, 'https://music.yandex.ru/feed','20-02-01'),
(5, 'zaa','20-02-03 11:49:04'),
(6, 'bbccbb','20-01-03')
;
drop table users_hits;
select distinct user_id
from users_hits
where user_id in
(select distinct user_id
from users_hits
where TIMESTAMPDIFF(second, timestamp, NOW() )/(60*60*24) <=7
)
and user_id not in
(select distinct user_id
from users_hits
where TIMESTAMPDIFF(minute, timestamp, NOW() )/(60*24) <=7
and url = 'https://music.yandex.ru/feed'
)
;