Skip to main content

delete all the values with in a group keeping only the topmost entry in each group in mysql

sometimes you're caught up in a situation where you have multiple entries against an id and you want to keep only single entry against each.

Table structure would be something like this:

Table `temp`

Entries:
user_id tstamp source
100 4/26/2015 app
100 4/26/2014 web
100 4/25/2015 app
101 4/26/2015 app
100 4/23/2015 app
101 4/20/2015 web
100 4/1/2015 app
200 4/26/2015 app
200 4/2/2015 app

Desired Output:
user_id tstamp source
100 4/26/2015 app
100 4/26/2014 web
101 4/26/2015 app
200 4/26/2015 app


Now, I want to delete the rows where source is `app` and keep the entry with max tstamp only with in a group.
Here's the sample query to achieve the same:

DELETE tmp FROM `temp` tmp join (select user_id, max(tstamp) as ts from `temp` where user_id in (select user_id from `temp` where source = 'app' group by user_id having count(user_id)>2) and source = 'app' group by user_id )t on tmp.user_id = t.user_id and tmp.tstamp != t.ts and tmp.source = 'app'


Exercise for you: Replace that inner query with join and check the performance benefits ;)

Comments

Popular posts from this blog

VBA MAcro to generate "table of contents with hyperlinks" automatically in a ppt

VBA MAcro to generate "table of contents with hyperlinks" automatically in a ppt: Function TableOfContent(count As Integer) 'count is the no. of slides in ppt Dim var As Integer Dim i As Integer, scount As Integer Dim strSel As String, strTitle As String, strb As String, strtemp As String, str As String Dim arr() As String Dim index As Integer, indexcount As Integer, slidecount As Integer Dim summary As Slide Dim para As Integer Dim slideOrder() As Integer 'To generate the Table of contents slide ReDim slideOrder(count - 2) 'Collect all the IDs of the selected slides For i = 1 To count - 2 slideOrder(i) = i + 2 Next 'Iterate over the slides in Index order slidecount = UBound(slideOrder) For scount = 1 To slidecount If ActivePresentation.Slides(slideOrder(scount)).Shapes.HasTitle Then 'Build up the ToC Text strTitle = ActivePresentation.Slides(slideOrder(scount)).Shapes("UseCase").TextFrame.TextRange.Text + ": ...

Filter packets through jpcap

//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms) JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20); captor.setFilter("tcp && src port 6000", true); It'll filter all the TCP packets with source port number 6000. So, all we need to do is pass the parameters and use logical and, or operators.

Upload a multi-part/image/file using curl

This post is about uploading a file to server using curl. Why I'm writing this post is because of a possible bug in Postman which forced me to look for an alternative to test my upload api. Test if curl is present in system : $ which curl /usr/bin/curl If curl is not present then install it : yum (centos), apt-get (ubuntu), brew (mac) Syntax of the command : curl -F "param1=value" -F "param2=value2" -F "filecomment=file_comment" -F "image=@image_path" server_api Here's an example of the same : curl -F "user_id=12" -F "description": "desc" -F "filecomment=This is an image file" -F "image=@/Users/Desktop/testim.png" localhost:80/myblog/create If using nodeJs, you can fetch the values in fields & files parameters.