So this is to show how many connection my platform had in a period in range of hour.
Where I comment "variable" it's what will be received from the front end, this query will run in the backend to populate a chart.
SELECT
count(server_events.event_type),
server_events.time_stamp,
hotspots.partner_id,
hotspots.partner,
hotspots.operator_id
FROM
`msp-data`.server_events
INNER JOIN
`adserver`.hotspots, `adserver`.operator
WHERE
server_events.time_stamp between "1591930800" and "1592017199" -- variable
and server_events.event_type = "auth_final"
and server_events.nas_id = adserver.hotspots.code
and hotspots.partner_id = "1" -- variable
and hotspots.operator_id = "2" -- variable
GROUP BY
server_events.time_stamp div 3600
ORDER BY
server_events.time_stamp
This is my current output
I posted the full query, but in this output I didn't get partner and filters of partner_id or operator_id
count time_stamp partner_id operator_id
6 1591944931 1 1
12 1591945711 1 5
6 1591952103 1 1
36 1591952621 1 1
18 1591956063 1 1
12 1591962118 1 4
6 1591966538 1 1
6 1591968554 1 1
12 1591973267 1 5
18 1591976918 1 1
18 1591978620 1 5
12 1591983139 1 5
12 1591984830 1 1
24 1591989873 1 1
12 1591993080 1 1
30 1591995612 1 1
output
Expected output would be
10 1591930800-1591934399
15 1591934400-1591937999
Try executing below. And try considering Strawberry's advice.
SELECT SUM(CNT), TM FROM(SELECT DISTINCT
count(server_events.event_type) OVER(PARTITION BY hotspots.partner_id,hotspots.partner,hotspots.operator_id,server_events.time_stamp/3600) CNT
server_events.time_stamp/3600 TM
FROM
`msp-data`.server_events
INNER JOIN
`adserver`.hotspots, `adserver`.operator
WHERE
server_events.time_stamp between "1591930800" and "1592017199" -- variable
and server_events.event_type = "auth_final"
and server_events.nas_id = adserver.hotspots.code
and hotspots.partner_id = "1" -- variable
and hotspots.operator_id = "2" -- variable
)
GROUP BY TM
ORDER BY
2
Under Mysql Version 12
SELECT SUM(CNT), TM FROM(SELECT
count(A.event_type) cnt,
hotspots.partner_id,hotspots.partner,hotspots.operator_id,tm
FROM
(SELECT A.time_stamp/3600 AS TM,A.* FROM `msp-data`.server_events A ) A
INNER JOIN
`adserver`.hotspots, `adserver`.operator
WHERE
A.time_stamp between "1591930800" and "1592017199" -- variable
and A.event_type = "auth_final"
and A.nas_id = adserver.hotspots.code
and hotspots.partner_id = "1" -- variable
and hotspots.operator_id = "2" -- variable
group by hotspots.partner_id,hotspots.partner,hotspots.operator_id,TM
) aliaz
GROUP BY TM
ORDER BY
2
#ismetguzelgun
SELECT SUM(CNT), TM FROM(SELECT
count(A.event_type) cnt,
hotspots.partner_id,
hotspots.partner,
hotspots.operator_id,
TM
FROM
(SELECT
A.time_stamp/3600 AS TM,
A.* FROM `msp-data`.server_events A
) A
INNER JOIN
`adserver`.hotspots,
`adserver`.operator
WHERE
A.time_stamp between "1591930800" and "1592017199" -- variable
and A.event_type = "auth_final"
and A.nas_id = adserver.hotspots.code
-- and hotspots.partner_id = "1" -- variable
-- and hotspots.operator_id = "2" -- variable
GROUP BY
hotspots.partner_id,
hotspots.partner,
hotspots.operator_id,
TM
) B
GROUP BY TM
ORDER BY
2
Gave this output now:
SUM(CNT) TM
6 442206.9253
6 442207.1419
6 442207.9306
6 442208.9175
6 442209.0614
6 442209.2531
6 442209.3533
6 442209.4050
6 442209.4150
6 442209.4969
6 442210.0175
6 442210.1089
6 442210.4747
6 442211.6994
6 442211.7619
6 442212.9272
6 442213.4872
6 442214.0711
6 442214.7964
6 442215.4461
6 442215.5736
6 442215.8106
6 442216.2833
6 442216.2908
6 442216.7419
6 442217.5386
6 442217.6267
6 442218.0083
6 442218.3764
6 442219.4092
6 442219.6553
6 442219.8953
6 442219.9933
6 442220.3000
6 442220.9611
6 442221.0033
6 442221.0297
6 442221.6656
6 442221.7533
6 442221.7897
Related
I have 3 tables that I have joined in the MySQL query below. All works well EXCEPT I would like the timeadj value with a 1 in column countingtime
to show from table 'data', not the first timeadj value the query finds.
I know this needs to be a query within a query but I am going around in circles and getting no where.
SELECT ttt_entries.tttid, ttt_teams.teamname, data.RacersInTeam,
ttt_entries.CoffeeClass, SEC_TO_TIME(data.timeadj),
COUNT(IF(data.division=5,1,NULL)) 'A+',
COUNT(IF(data.division=10,1,NULL)) A,
COUNT(IF(data.division=20,1,NULL)) B,
COUNT(IF(data.division=30,1,NULL)) C,
COUNT(IF(data.division=40,1,NULL)) D
FROM ttt_entries
INNER JOIN ttt_teams
ON ttt_entries.tttid = ttt_teams.tttid
INNER JOIN (SELECT * FROM data ORDER BY data.countingtime DESC) as data
ON ttt_entries.tttid = data.teamid
WHERE ttt_entries.eventDate = DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE()) >4, -5, 2) + DAYOFWEEK(CURDATE())) * -1 DAY) -- last Thursday
AND data.wtrlid = '22'
GROUP BY ttt_teams.tttid
ORDER BY data.timeadj ASC
For example.... In Team 1 (data.teamid=1) there are 8 time values in timeadj. In the adjacent column (countingtime) is a value either 1 or 0 but only ever 1x 1 per team.
Table ttt_entries
ID tttid CoffeeClass
1 23 Mocha
2 52 Espresso
3 6 Frappe
Table ttt_teams
tttid Name
6 Team A
23 Team 1
52 Team 2
Table 'data'
id wtrlid teamid timeadj countingtime division
1 22 23 3467.123 0 10
2 22 23 3467.125 0 20
3 22 23 3467.432 0 10
4 22 23 3469.000 1 10
5 22 23 3469.112 0 10
6 22 23 3468.987 0 5
My code brings back
tttid teamname RacersInTeam CoffeeClass Time A+ A B C D
23 Team 1 6 Mocha 3467.123 1 4 1 0 0
I need it to bring back the same data but a different time:
tttid teamname RacersInTeam CoffeeClass Time A+ A B C D
23 Team 1 6 Mocha 3469.000 1 4 1 0 0
You can try below way -
SELECT ttt_entries.tttid, ttt_teams.teamname, data.RacersInTeam,
ttt_entries.CoffeeClass, SEC_TO_TIME(max(case when countingtime=1 then data.timeadj end)),
COUNT(IF(data.division=5,1,NULL)) 'A+',
COUNT(IF(data.division=10,1,NULL)) A,
COUNT(IF(data.division=20,1,NULL)) B,
COUNT(IF(data.division=30,1,NULL)) C,
COUNT(IF(data.division=40,1,NULL)) D
FROM ttt_entries
INNER JOIN ttt_teams
ON ttt_entries.tttid = ttt_teams.tttid
INNER JOIN (SELECT * FROM data ORDER BY data.countingtime DESC) as data
ON ttt_entries.tttid = data.teamid
WHERE ttt_entries.eventDate = DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE()) >4, -5, 2) + DAYOFWEEK(CURDATE())) * -1 DAY) -- last Thursday
AND data.wtrlid = '22'
GROUP BY ttt_teams.tttid
ORDER BY data.timeadj ASC
I would like to select the first certain number of rows, by groups of a certain column. For example :
Original data:
index type value
0 1 a 0.716430
1 2 a 0.223650
2 3 a 0.375417
3 4 a 0.773874
4 5 a 0.802127
5 6 a 0.956563
6 7 b 0.377718
7 8 b 0.487772
8 9 b 0.672767
9 10 b 0.275895
10 11 b 0.981751
11 12 b 0.914780
12 13 b 0.940582
13 14 c 0.347563
14 15 c 0.101106
15 16 c 0.390205
16 17 c 0.235941
17 18 c 0.593234
18 19 c 0.904659
I would like to select the first 4 rows for each unique value of type, and the order is by index.
So the ideal result would be:
index type value
0 1.0 a 0.716430
1 2.0 a 0.223650
2 3.0 a 0.375417
3 4.0 a 0.773874
4 7.0 b 0.377718
5 8.0 b 0.487772
6 9.0 b 0.672767
7 10.0 b 0.275895
8 14.0 c 0.347563
9 15.0 c 0.101106
10 16.0 c 0.390205
11 17.0 c 0.235941
row_number() is the typical solution to this:
select t.*
from (select t.*,
row_number() over (partition by type order by index) as seqnum
from t
) t
where seqnum <= 4;
In older versions of MySQL, you can do:
select tm.*
from telegram_message tm
where tm.index <= coalesce( (select tm2.index
from telegram_message tm2
where tm2.type = tm.type
order by tm2.index asc
limit 1 offset 3
), tm.index
);
The coalesce() is so all rows are taken if there are not 4 rows for the type.
You can get the result you want by self joining your table on index, where the value of index in the joined table is less than that in the first, and selecting only those rows which have < 4 rows with lower index values:
SELECT t1.id, t1.index, t1.type, t1.value
FROM test t1
LEFT JOIN test t2 ON t2.index < t1.index AND t2.type = t1.type
GROUP BY t1.id, t1.index, t1.type, t1.value
HAVING COUNT(t2.index) < 4
Output:
id index type value
0 1 a 0.71643
1 2 a 0.22365
2 3 a 0.375417
3 4 a 0.773874
6 7 b 0.377718
7 8 b 0.487772
8 9 b 0.672767
9 10 b 0.275895
13 14 c 0.347563
14 15 c 0.101106
15 16 c 0.390205
16 17 c 0.235941
Demo on dbfiddle
Currently I am honestly at loss what I am doing wrong. It is a rather simple query I think.
Tables:
operations:
id processedon clientid
1 2018-01-01 9
2 2018-03-16 9
3 2018-04-21 9
4 2018-04-20 9
5 2018-05-09 9
items:
id operation_id quantity unitprice
1 1 10 2
2 1 5 3
3 2 20 4
4 3 10 2
5 4 8 4
6 4 10 4
7 5 2 2
The expected result of the operation/query is:
month total_value
1 35
3 80
4 92
5 4
That is quantity * unitprice based. For some reason, it only returns month=4
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON (`items`.`operation_id` = `operations`.`id`)
GROUP BY 'month'
ORDER BY 'month'
According to the info provided the join should be
INNER JOIN operations ON items.operation_id = operations.id
Eg
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON `items`.`operation_id` = `operations`.`id`
GROUP BY month(`operations`.`processedon`)
ORDER BY `month`
There is no efficiency gain by using a column alias in the group by clause, I prefer to avoid using them except perhaps in the order by clause.
The following query will give you the required answer
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM items
INNER JOIN operations ON (items.operation_id = operations.id)
GROUP BY month(operations.processedon)
ORDER BY month(operations.processedon)
You need to specify month correctly since it is not an existing column.
You'll get the following result
month total_value
1 35
3 80
4 92
5 4
My table is similar to the one below. I would like to select records where city and town matches and code or area has repetition. In this case rows the result should be all rows except the ones with id 3 and 5. Thanks for looking at this
city town id code1 code2 code3 code4 area1 area2 area3 area4
----------------------------------------------------------------------------------
dublin town1 1 1 2 3 5 1 2 3 4
dublin town1 2 2 8 10 6 7 8 9
dublin town1 3 12 13 15 11 12 13 14
dublin town2 4 1 2 3 5 1 2 3 4
dublin town2 5 6 7 8 10 6 7 8 9
dublin town2 6 11 12 13 15 1 12 13 14
http://sqlfiddle.com/#!2/0bbe7/1/0
Using INNER JOIN,
select a.*
from bigcities a inner join bigcities b
on a.city = b.city
and a.town = b.town
and a.id != b.id
and (a.code1 = b.code1
or a.code2 = b.code2
or a.code3 = b.code3
or a.code4 = b.code4
or a.area1 = b.area1
or a.area2 = b.area2
or a.area3 = b.area3
or a.area4 = b.area4
);
Demo.
This is pretty much what the exists clause can do. Here is a solution for your conditions:
select t.*
from <table> t
where exists (select 1
from <table> t2
where t2.city = t.city and
t2.town = t.town and
t2.id <> t.id and
(t2.code1 = t.code1 or t2.code2 = t.code2 or t2.code3 = t.code3 or t2.code4 = t.code4 or
t2.area1 = t.area1 or t2.area2 = t.area2 or t2.area3 = t.area3 or t2.area4 = t.area4
)
You can do it with the following query
--We do a query to the table to get the rows and then we do a subquery
--to get all the rows with the same criteria. We know it is repeated
--because the counter of rows > 1.
--If you want to get only where it is repeated 2 o 3, etc, you only need to change
--the COUNT() > 1
SELECT *
FROM tableName t
WHERE (
SELECT COUNT(*) FROM tableName tRep WHERE
t.city = tRep.city AND
t.town = tRep.town AND
((t.code1 = tRep.code1 AND t.code2 = tRep.code2 AND t.code3 = tRep.code3 AND t.code4 = tRep.code4) OR
(t.area1 = tRep.area1 AND t.area2 = tRep.area2 AND t.area3 = tRep.area3 AND t.area4 = tRep.area4))
) > 1
I need a column from row value.
I have two table.
Table 1 : working_day Contains list of all working day date.
date
--------
2013-03-30
2013-03-29
2013-03-28
Table 2 : entry contains each employee in and out time.
id In Out Date
1 9 0 2013-03-30
2 8 0 2013-03-30
3 7 0 2013-03-30
1 8 18 2013-03-29
2 9 16 2013-03-29
3 6 20 2013-03-29
4 12 15 2013-03-29
Expected Output :
ID 29-03-2013_IN 29-03-2013_Out 30-03-2013_In
1 8 18 9
2 9 16 8
3 6 20 7
4 12 15 0
Tried :
SELECT id,
Case condition1 for 29_in, // I don't know which condition suite here.
Case condition1 for 29_out,
Case condition1 for 30_in
FROM entry
WHERE DATE
IN (
SELECT *
FROM (
SELECT DATE
FROM working_day
ORDER BY DATE DESC
LIMIT 0 , 2
)a
)
You could try something like that:
select
e.id,
(SELECT `in` FROM entry WHERE id = e.id AND date = '2013-03-30') as '2013-03-30_in',
(SELECT `in` FROM entry WHERE id = e.id AND date = '2013-03-29') as '2013-03-29_in',
(SELECT `out` FROM entry WHERE id = e.id AND date = '2013-03-29') as '2013-03-29_out'
from entry e
group by e.id;
Here is Demo
IMO you should do this in application instead of SQL