Skip to content

Tag: sql

SQL efficient way to match ANY in a large table

I am joining a small table and a very large table and want to return a distinct item if ANY items match. The table is so large that it takes hours for something that I think should take seconds. The problem is that I am “iterating” over every single entry in the second table. I want to be able to

First row of results should never be displayed

DB-Fiddle Expected Result In the results I want to display the quantity for all dates except for the first row. I know in the example above I can do this with a simple WHERE-condition like this: However, it is neither guaranteed that the first time_stamp will be always 2020-01-14 nor that the MIN(2020-01-14) …

SQL – Ordering table with subquery for select

I am trying to do ordering on query with subquery for a name from uuid. Let’s have those two tables in MySQL: bans: id uuid time reason 1 c6b8eade-7db9-345b-b838-8f8361552cf5 1642369382 Test 2 0138c279-c5fa-3acd-adaa-8edb7b4569d8 1642384565 Spam 3 3c01262c-a3c3-3133-ba43-92a9ded01c27 1631876477 Hax user…

Calculate total difference of values between two timestamps

DB-Fiddle Expected Result: I want to calculate the total difference of the quantity between two timestamps. Therefore, I tried to use the query from this question: However, I am not able to get the expected result. What do I need to change to make it work? Answer The partition by clause in the lag call of you…