Skip to content

Duplicates in pre-aggregated sub-query sql

I have two tables with many-to-many relationship. I am trying to get values from both of the table where UserId is unique (I’m joining these table on this value) I am rying to use pre aggregated query, but I get error Column ‘clv.ProbabilityAlive’ is invalid in the select list because it is …

Recursive SQL query for finding matches

I have 5 SQL Tables with the following columns: As input data I have employee_id and request_id. I need to figure out whether the employee has access to the request (whether he’s a manager or not) We cannot use ORM here since app’s responsiveness is our priority and the script might be called a lo…

Add hours/minutes to datetime

I’m having an issue on how to add the Offset to the EventDateTime. Is there a way to do this? I would convert it to a decimal number but cannot because 5:30 is not 5.3 hours, it’s 5 hours and 30 minutes. The data is shown below with the expected output. Please help if possible. Sample Data: Expect…

Finding last and second last date and corresponding values

Consider the following schema (fiddle): For each meter.id I need to find the latest reading date, value, and the value difference vs previous reading. For the sample data my output would look like this (plus some other columns from meters): meterid latest value delta value 1 20090403 399 99 2 20090403 288 44 …

MS SQL return single row case dataset without GROUP BY clause

I have a data set of ActivityDate which is a datetime field and EngineHours which is a decimal field. I want to return a single row of data that brings back the average number of EngineHours on each day of the week. The dataset is pretty big so the best performance is likely to be the most ideal, the script