Skip to content

SQL deadlock on concurrent delete

I have an API endpoint to delete children of a parent, unless a child is specified. The SQL is: (This query is sanitised, so ignore the fact that I’m not doing ChildID != @ChildIDToKeep) The executed SQL is also wrapped in a transaction. My web application attempts to send hundreds of requests to this e…

sql query for finding ID numbers on date range

I want to get the ID numbers for the last 24 hour range. Say I run a task at 4:00AM each morning and want to get the previous 24 hours of data going back to 4:00AM the previous day. I need to get the id codes to search the correct tables. If the data is like this what would be

Arithmetic Overflow Error if Varchar is over 999, WHY?

I discovered a weird issue in my database, I was able to fix it, but I do not understand WHY the error occurred in the first place. I’m using Microsoft SQL Server 2017. The following code returns an Arithmetic Overflow error: Returns Error: Arithmetic overflow error converting varchar to data type numer…

How to write a loop for such sql query

I need to write such query: SELECT CAST (date_time_column AS DATE) as ‘Date’, AVG(CASE WHEN FORMAT(date_time_column, ‘HH:mm’) = ’00:00′ then my_values ELSE NULL end) as ’00:00′, …….. AVG(CASE …

Delete all but the newest with specific filename

I migrated a file logger to log to the database instead of to disk. This caused many duplicates in the database, whereas on disk the file would have just gotten overwritten. I want to delete all the “not newest” rows given a specific filename, what SQL would do this given the following table: The …

How to sum the minutes of each activity in Postgresql?

The column “activitie_time_enter” has the times. The column “activitie_still” indicates the type of activity. The column “activitie_walking” indicates the other type of activity. Table example: What I need is to sum up the total minutes for each activity separately. Any sug…