I'm having a problem with a query I'm running. Here's a dbfiddle: https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=8dc0b4f201e7d25c8817dcecd35b47f0
Basically, I'm trying to keep the rows intact, what I mean is: You can see in the 1st query that playerID 147 set a score of 10450 on the 2018-03-24 13:37:02. However, sadly in the 2nd query the row breaks, I still get the min score I need, but the date is wrong. it sets 2018-03-05 16:24:28 even though it should be 2018-03-24 13:37:02.
I tried doing what's described here: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL? but I can't get it to work. I've no idea how to rewrite my query in order for it to work. I'd appreciate any help in altering my query. Thanks for any help.
With this SELECT Statement
SELECT MIN(t.ID),t.Score,t.ChallengeId,t.playerID,MIN( from_unixtime(date) )
FROM `times` t inner Join
(SELECT MIN(Score) score,challengeID,playerID
from `times`
group by challengeID,playerID
Order by Score ASC) s
ON t.challengeID = s.challengeID
and t.playerID = s.playerID
and t.Score = s.score
GROUP BY t.Score,t.ChallengeId,t.playerID
order by t.Score
You get this result(Only the first are here Displayed
ID Score ChallengeId playerID from_unixtime(date)
90488 10450 466 28 2018-03-20 02:16:29
92155 10450 466 8 2018-03-24 02:05:18
92448 10450 466 147 2018-03-24 13:37:02
92763 10450 466 97 2018-03-24 19:15:42
92787 10450 466 410 2018-03-24 19:23:24
85201 10460 466 255 2018-03-06 05:13:46
86256 10460 466 66 2018-03-09 10:48:16
92778 10460 466 1846 2018-03-24 19:21:07
84801 10470 466 47 2018-03-05 16:29:41
84804 10470 466 944 2018-03-05 16:30:34
85724 10470 466 599 2018-03-07 08:07:30
88139 10470 466 1651 2018-03-15 11:16:54
DBfiddle new sample https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=de581e5a5b6fa9a184cc2e235337b2d5
Related
I have table 1:
historial_id
timestamp
address
value
insertion_time
1
2022-01-29
1
84
2022-01-31
2
2022-01-29
2
40
2022-01-31
3
2022-01-30
1
84
2022-01-31
4
2022-01-30
2
41
2022-01-31
5
2022-01-30
2
41
2022-01-31
(sometimes it has repeated rows)
...
I need a Query to get:
timestamp
value(address 1)
value(address 2)
2022-01-29
84
40
2022-01-30
84
41
......
I tried with:
SELECT timestamp, ( SELECT value
FROM historical
WHERE register_type=11
AND address=2
AND timestamp=t1.timestamp
GROUP BY value
) AS CORRIENTE_mA,
( SELECT value
FROM historical
WHERE register_type=11
AND address=1
AND timestamp=t1.timestamp
GROUP BY value ) AS Q_M3pH
FROM historical AS t1
GROUP BY timestamp;
But it's too slow, it even stops because of exceeded time.
I tried with distinct too instead of group by
I think you need dynamic pivot.
Please try and avoid MySQL reserved words like timestamp.
Below query return only the max value for address 1 and 2 grouping by timestamp.
This is a simplified version of your query :
select
`timestamp`
, max(case when address=1 then value end) as value_address1
, max(case when address=2 then value end) as value_address2
from historical
group by `timestamp`;
Result:
timestamp value_address1 value_address2
2022-01-29 84 40
2022-01-30 84 41
Demo
I need to extract the required fields from a table along with relevant time stamp
SELECT * FROM Glm_Test.LicenseUsage where FeatureId='2';
Output :
VendorId,FeatureId,Total_Lic_Installed,Total_Lic_Used,Reserved,CurrentTime
1 2 106 19 67 2015-12-15 15:00:05
1 2 106 19 67 2015-12-15 15:02:02
1 2 106 19 69 2015-12-15 15:04:02
1 2 106 19 67 2015-12-15 15:06:01
1 2 106 20 67 2015-12-15 15:08:02
select VendorId,FeatureId,Total_Lic_Installed,Max(Total_Lic_Used),Reserved,CurrentTime from Glm_Test.LicenseUsage where FeatureId= '2' group by VendorId,FeatureId;
output:
1 2 106 20 69 2015-12-15 15:00:05
In the above 2 queries
1st query lists all entries from the table
and i want second query to return time stamp for the MAX value of column Total_Lic_Used but somehow it is returning me only timestamp of the first entry.
Help is much appreciated.
Selecting the columns which are not part of an aggregation function like count/max/min/sum... or not in group by clause will give unexpected results:
Other RBBMS wont allow these statements(gives error like):
sql server ==> the select list because it is not contained in either
an aggregate function or the GROUP BY clause
Oracle ==>not a GROUP BY expression
You can do this by a sub query and join
select
a.VendorId,
a.FeatureId,
a.Total_Lic_Installed,
b.max_Total_Lic_Used,
a.Reserved,
a.CurrentTime
from Glm_Test.LicenseUsage a
join (
select
VendorId,
FeatureId,
Max(Total_Lic_Used) max_Total_Lic_Used
from Glm_Test.LicenseUsage
where FeatureId = '2'
group by VendorId, FeatureId
) b
on a.VendorId = b.VendorId and
a.FeatureId = b.FeatureId and
a.Total_Lic_Used = b.max_Total_Lic_Used
sql fiddle demo
You can try this also;
select
`VendorId`,
`FeatureId`,
`Total_Lic_Installed`,
`Total_Lic_Used`,
`Reserved`,
`CurrentTime`
from Glm_Test.LicenseUsage
order by Total_Lic_Used desc
limit 1
demo
I have the 2 following select in Mysql:
1st select:
(SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date As FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) as Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player`<>'Player'
ORDER BY `Online_playerdatabase_v2`.`Balance` DESC;
2d select:
SELECT DISTINCT(Online_customer_activity_v2.Customers) as Player,
max(Online_customer_activity_v2.Date) as LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets`>0
Group by Online_customer_activity_v2.Customers
Output Select 1
Player FirstDep Balance
Ray 2014-10-19 9100.00
Ramzi 2014-11-02 9.61
tareq 2014-11-06 805.00
STAN 2014-10-17 7.50
Bill 2014-03-25 68.40
karam 2014-11-16 676.50
Abdul 2014-11-13 650.00
Renaud 2014-03-12 507.00
John 2014-11-22 500.00
Output select 2
Player LastAction
John 2015-11-13
Bill 2014-12-14
Renaud 2015-03-14
Abdul 2015-11-16
Ray 2015-11-22
STAN 2015-10-29
Ramzi 2015-11-10
Tarek 2015-05-10
karam 2014-12-10
Abdul 2015-02-10
Desired Output, a join on both Select that adds following calculations:
active days (FirstDep-LastAction) and Days_last_Visit (CurrentDate - Last Action)
Summarized in following table:
Player FirstDep Balance LastAction Active_days Days_last_Visit
Ray 2014-10-19 9100.00 2015-11-22 399 1
Ramzi 2014-11-02 9.61 2015-11-10 373 13
tareq 2014-11-06 805.00 2015-05-10 185 197
STAN 2014-10-17 7.50 2015-10-29 377 25
Bill 2014-03-25 68.40 2014-12-14 264 344
karam 2014-11-16 676.50 2014-12-10 24 348
Abdul 2014-11-13 650.00 2015-02-10 89 286
Renaud 2014-03-12 507.00 2015-03-14 367 254
John 2014-11-22 500.00 2015-11-13 356 10
Your help is greatly appreciated!
Thanks
The following query should give the result you want. I will add that I joined the two tables from your intermediate queries above using the Player field. This is not a very robust way to join, because the name may not be unique among all players in the table. A better way to join would be to use a unique identifier of some sort.
SELECT t1.Player, t1.FirstDep, t1.Balance, t2.LastAction,
DATEDIFF(t2.LastAction, t1.FirstDep) AS Active_days,
DATEDIFF(NOW(), t2.LastAction) AS Days_last_Visit
FROM
(
SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date AS FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) AS Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player` <> 'Player'
) t1
INNER JOIN
(
SELECT DISTINCT(Online_customer_activity_v2.Customers) AS Player,
MAX(Online_customer_activity_v2.Date) AS LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets` > 0
GROUP BY Online_customer_activity_v2.Customers
) t2
ON t1.`Player` = t2.`Player`
You need to join the 2 selects as subqueries in a 3rd select using the player field. The Active_days and Days_last_Visit fields can be calculated using the DateDiff() function.
SELECT *
,DateDiff(t2.LastAction,t1.FirstDep) AS Active_days
,DateDiff(CURDATE(), t2.LastAction) AS Days_last_Visit
FROM
(SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date As FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) as Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player`<>'Player'
ORDER BY `Online_playerdatabase_v2`.`Balance` DESC) t1
LEFT JOIN
(SELECT DISTINCT(Online_customer_activity_v2.Customers) as Player,
max(Online_customer_activity_v2.Date) as LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets`>0
Group by Online_customer_activity_v2.Customers) t2
ON t1.Player=t2.Player
You have to consider, however, how you join the 2 datasets. I used left join, since the players table will probably hold all players, but you may want to go for inner join or simulate a full outer join depending your requirements and your data.
I have a query like that:
SELECT id, title
FROM tbl_item
WHERE id!=477
AND (projectid=172 OR styleid=66)
ORDER BY timestamp DESC LIMIT 4;
How can I order the item by same projectid first (list all projectid 172), then same styleid (66) even if the timestamp of styleid is greater.
id projectid styleid timestamp
475 172 66 1401660000
459 172 66 1398981600
458 167 66 1398636000
456 172 64 1397685600
449 63 66 1394665200
444 167 66 1391727600
411 113 66 1386630000
408 62 66 1385938800
407 159 66 1385938800
387 159 66 1375826400
With my snapshot above, my desire result is merge of two queries:
Query 1: SELECT id FROM tbl_item WHERE projectid=172;
Query 2: SELECT id FROM tbl_item WHERE styleid=66;
Desire result ids: 475,459,456,458
The first 3 ids (475,459,456) is result of #1, and the 458 is result of #2 (LIMIT 4 results)
Thank you!
You can add multiple columns to the ORDER BY, they are prioritised from left to right see docs:
SELECT id, projectid, styleid, timestamp
FROM tbl_item
WHERE ...
ORDER BY projectid, styleid, timestamp DESC;
N.B. You do not need to SELECT the columns used by the ORDER BY, I have just included them for clarity.
UPDATE
From your update to the question, it can be done bespokely like this
SELECT id, projectid, styleid, timestamp
FROM tbl_item
WHERE projectid=172
OR styleid=66
ORDER BY projectid=172 DESC,
projectid != 172 AND styleid=66 DESC,
timestamp DESC
LIMIT 4;
Here is my table structure with data
id actor_id created_at updated_at
729 80 2012-09-10 17:05:59 2012-09-10 17:05:59
731 80 2012-09-10 17:04:02 2012-09-10 17:04:02
725 139 2012-09-06 13:59:08 2012-09-06 13:59:08
724 76 2012-09-06 11:31:30 2012-09-06 11:31:30
723 29 2012-09-06 09:40:22 2012-09-06 09:40:22
719 29 2012-09-06 09:24:02 2012-09-06 09:24:02
811 80 2012-09-02 17:05:59 2012-09-10 17:05:59
812 80 2012-09-01 17:04:02 2012-09-10 17:04:02
This is the result of
SELECT `te`.`id`, te.actor_id, te.created_at, te.created_at
FROM `timeline_events` AS te
ORDER BY
te.created_at DESC
LIMIT 10
I need group it by actor_id and created_at
Here is what i need in the end
id actor_id created_at updated_at count
729 80 2012-09-10 17:05:59 2012-09-10 17:05:59 2
725 139 2012-09-06 13:59:08 2012-09-06 13:59:08 1
724 76 2012-09-06 11:31:30 2012-09-06 11:31:30 1
723 29 2012-09-06 09:40:22 2012-09-06 09:40:22 2
812 80 2012-09-10 17:04:02 2012-09-10 17:04:02 2
Can someone guide me how to do this?
Many thanks in advance
UPD To simplify i will put another example
So say i have next rows
1 1 (count: 2)
1
3 3 (count: 1)
4
4 => after magic function it should be 4 (count: 2)
1
1 1 (count: 3)
1
6 6 (count: 2)
6
4 4 (count 3)
4
4
So it should split by groups.
UPD 2
I need this query for rendering timeline. Right now it show all info what user did, but i need group it.
Before
user1 upload photo
user1 changed information
user2 updated bio
user3 uploaded photo
user3 updated bio
user1 update bio
After
user1 uploadd photo and changed infomarion
user2 updated bio
user3 uploaded photo and updated bio
user1 updated bio
I think this might be what you are trying to do:
select t1.id,
t1.actor_id,
max(created_at) created_at,
updated_at,
t2.total
from yourtable t1
left join
(
select count(*) total, date(created_at) dt, actor_id
from yourtable
group by actor_id, date(created_at)
) t2
on t1.actor_id = t2.actor_id
and date(t1.created_at) = t2.dt
group by t1.actor_id, date(t1.created_at)
order by t1.created_at desc
see SQL Fiddle with demo
I am grouping by actor_id and the date using the DATE() function
I find solution by myself
Here is the query
SET #i = 0;
SELECT
COUNT(`wrapper`.`id`) AS 'count',
GROUP_CONCAT(`wrapper`.`type` SEPARATOR ',') as 'types'
FROM (
SELECT
#prev := (
SELECT prev_te.actor_id
FROM `timeline_events` AS prev_te
WHERE prev_te.created_at > te.created_at
ORDER BY prev_te.created_at ASC
LIMIT 1
) AS 'prev_actor_id',
IF(
#prev = te.`actor_id` OR #prev IS NULL,
#i,
#i := #i + 1
) AS 'actor_id_group',
`te`.*
FROM `timeline_events` AS te
ORDER BY
te.created_at DESC,
te.id DESC
) AS `wrapper`
GROUP BY `wrapper`.`actor_id_group`
LIMIT 10
And here is the proof link ;-)
This website really helped me
I am using wrapper for grouping purpose, because mysql didn't group by variables, if someone know better solution, please let me know
SELECT te.id, te.actor_id, te.created_at, te.updated_at ,
COUNT(*) FROM timeline_events AS te GROUP BY te.actor_id,
te.created_at ORDER BY
te.created_at DESC
LIMIT 10
Add GROUP BY actor_id before order by clause.
Latest edit:
select distinct actor_id, count(actor_id), created_at from actors group by actor_id
order by created_at desc;