Skip to main content

All Questions

1 vote
3 answers
2k views

SQL Server : Using LAG() with calculated previous value

I have data like this in SQL Server 2016 database table: PERIODE PERIODE_FORECAST VALUE 2021-08-01 2021-01-01 51384.673 2021-08-01 2021-02-01 44118.129 2021-08-01 2021-03-01 43164.446 2021-08-01 2021-...
eman kun's user avatar
0 votes
1 answer
373 views

How to select rows based on a rolling 30 day window SQL

My question involves how to identify an index discharge. The index discharge is the earliest discharge. On that date, the 30 day window starts. Any admissions during that time period are considered ...
oke_8992's user avatar
0 votes
1 answer
140 views

SQL DATEADD Function Removing Week Ends [duplicate]

I need to add five days to the "2020-12-01" date. But here the scenario is while adding five days to "2020-12-01" date we should not include the Saturday (2020-12-05) and Sunday (...
venugopal's user avatar
1 vote
2 answers
973 views

Detect if a month is missing and insert them automatically with a select statement (MSSQL)

I am trying to write a select statement which detects if a month is not existent and automatically inserts that month with a value 0. It should insert all missing months from the first entry to the ...
Taccoman's user avatar
-2 votes
3 answers
90 views

SQL with Date LOOP [closed]

I've been trying to write a query that returns the penultimate day of each month for 36 months, and this query should automatically return records for 36 months. For example; such as 20201230 for ...
Zack Mary's user avatar
1 vote
1 answer
63 views

Adding the total number of distinct customers to a cte table in Microsoft SQL Server

I am using Microsoft SQL Server and am trying to achieve the following Date Distinct Customers last 30Days 2020-12-01 20000 2020-12-02 23000 What I am trying to get is that between 2020-11-01 and 2020-...
Annalise Azzopardi's user avatar
1 vote
1 answer
316 views

Generate date range from start date and end date SQL Azure

I need to generate a date range that comes from a table like this: id | start_date | end_date ---+-------------+------------ 1 | 2020-01-01 | 2020-01-03 2 | 2020-01-01 | 2020-01-03 The result ...
Rodrigo's user avatar
  • 23
2 votes
1 answer
215 views

Using SQL to group consecutive items that share a common status (dummy data included)

Given a table that has sometimes repeated statuses within a group (in this case "vehicles"), I want to consolidate those statuses into a single row and aggregate status_seconds. The data ...
Andrew T.'s user avatar
2 votes
3 answers
141 views

Conceptualizing SQL Query for data that isn't there

SQL Server 2017. Having been running simple-to-intermediate SQL queries for many years, I'm having trouble wrapping my head around this one, as it's querying for information that doesn't actually ...
Stpete111's user avatar
  • 3,487
0 votes
2 answers
359 views

Split date into month and year based on number of months passed in stored procedure into a temp table

I have a stored procedure, where takes number of numbers as a parameter. I do my query with where clause like this select salesrepid, month(salesdate), year(salesdate), salespercentage from ...
Karthik Dooty's user avatar
-1 votes
1 answer
38 views

Create two record across hour end with same information

I want to split a single row into multiple rows based on time. Below is the example data. Status MachineId StartDate EndDate ---------------------------------------------------...
Alberto's user avatar
  • 77
0 votes
2 answers
222 views

How can I create a dates-table inside a query in SQL Server?

Say I want to match records in table_a that have a startdate and an enddate to individual days and see if on, for instance March 13, one or more records in table_a match. I'd like to solve this by ...
steenbergh's user avatar
  • 1,781
0 votes
1 answer
637 views

SQL - Create multiple records from 1 record based on days between 2 dates

I have a table that holds an employee's leave. If an employee takes more than 1 day off in a row for example 22-05-2020 to the 26-05-2020 this will be displayed as one record. I am trying to get this ...
Amy's user avatar
  • 641
1 vote
2 answers
38 views

SQL Flag Rows Occurring 2 Hours After Previous Flag

I have some messy RFID data due to over sensitive antenna's. There is a physical process that tracks an RFID tag moving through different stations in a cycle. An item with an RFID tag can move through ...
Hunter Duckworth's user avatar
1 vote
1 answer
390 views

Grouping shift data by 7-day windows in SQL Server 2012

What I want to do is to calculate the number of shifts and hours worked by each employee in any given 7-day period. In order to achieve this, I need to identify and group 'islands' of shifts. Note ...
ashvin10's user avatar

15 30 50 per page