I have a query with several self joins: Here is a sample table where in yellow I marked the record that is within the boundaries of the where condition: Here is the expected result view: Basically I filter one table with several consecutive conditions and show the result as a table. Is there a way, using window function or subquery
Tag: tsql
Get Row based off Date and previous row
I’ve got the following sample data and what I’m trying to do is based off the DateChanged value is get the appropriate Amount from the linked table along with the previous row. The results would then display as How could this be done? What I have so far is this but wondering is there’s a better way Answer Just use
Column to see Completion
I’m trying to come up with a way to add an extra column to this result-set that is a bit value of 1/0if the particular owner has all the rows as not null. The column would be: Answer You can use window functions:
Select max value for each company in list, display longest value per company per day
I’ve looked at some of the other questions similar to this, but nothing I’ve found can quite get me there. I have a table with multiple columns, but 3 of the columns are Company, Date, and WaitTime. I want to select the longest WaitTime for each company and for each day. I’d like to display the results for the entire
Subtract values from one column based on values in another (TSQL)
I have table where I need to subtract values in one column based on conditions applied to the other. For example, I want to subtract values with code fst and trd, meaning (12 – 23). I don’t want to declare separate variables. Is it possible to make this with a query? Answer One method is conditional aggregation: Assuming you have
diffrent serial no for each ID in MSSQL
I have a table where I Have 2 ids table is like ID Date Shift(hr) serial no 54 11/10/2020 2 1 54 10/10/2020 5 2 …
SQL Date Filter: Return results when start date = end date
:startDate and :endDate are given value from html input and passed as parameters with AJAX. I want to return notifications BETWEEN :startDate and :endDate, but also return a notification if startDate …
Getting SUM of ‘x’ consecutive values where x is an integer from another row
I am trying to code in Microsoft SQL Server Management Studio. This is SQL. I have to get the sum of x previous consecutive rows where x is based on a number in another row. For Example: Example 1: …
Dynamically Insert into Table A Based on Row_Num from Table B
I’ve condensed some data into TableB which looks like the following: There are 2000 unique rows in total, Row_Num from 1 to 2000, and every AreaID in this table is naturally unique as well. I now want to insert into a blank table, TableA, which has the following columns: The insert statement I want to use is repeats everything except
TSQL – how can I sum values except absolutes
I would like to sum values in my table, except the ones that are absolute (field absolute, value = 1). If that’s the case, the summing should reset. Example: Regular SUM() would return (4+7+3=) 14. But in this example it should reset at value 7, which makes a sum of (7+3=) 10. How can I make this work? Answer You