I’m using SQL Server 2014. I have a table: I have some data: If I perform a LAG and LEAD using the below for one Job, I get the desired result: Result: Notice the NULL for the first FromActionCode and LastToCode, which is correct. However, If I look at all records, it messes it up: What am I missing? An…
Tag: sql-server
SQL between date range returning a wrong result
I’m writing a SQL query to get rows between a date range format: mm/dd/yyyy I used the between operator but the result shows out of the range dates The expected result : dates of year 2020 only The actual result is: Answer I assume that you use SQL Server. Maybe you think that % is a wildcard char in th…
Update one column only when any other column change
I am trying to create an update statement on SQL Server to set one of the cards as default and all the others as not, however, if I have 3 cards and card 1 set to default and now I want to set card 2 …
Azure LogicApp cannot get rowid from SQL Server
I have an Azure logicapp that connects to a SQL Server database in Azure. The table has a uniqueidentifier column as the primary key. In the logic app the delete or update requires the rowid in order to perform update, the primary key does not work here and throws an error when running, stating that the table…
Query to update duplicates
I have the following query that returns duplicates that i am running on sql server. So I need to write a query that will update second instance of the email address based on the select query that retrieves dulicates to for example imports@rohnis.com to imports@rohnis.com.duplicate. If it is info@eps.ws then i…
How to calculate a group average in SQL?
I have a sql data set which look something like: I would like to be able to select the data in such a way that the query will return: With the average result calculated over the 3 years shown for each person. How could I achieve this in SQL Server? Answer I think you want a window function:
Look up in same table with different value reference sql server [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago. Improve this question I’d like some help with a code. I have TableA where Btime is sometim…
Extract substring address SQL
I have a table with column Address which is like city. City_Name, citysector. CitySector, Street. Street_Name, StreetNumber. Street_Number EXAMPLE Address c. Paris, s. ParisDowntown, str. Rue Étienne Marcel, nr. 50 How can I substract 4 different columns like city, sector, street, streetNumber from Address co…
SQL count column from another table
I have two tables in a database The first being people which has the columns id: int, name: varchar(10) and another being relationships which represents a one way following me: int following: int …
How to get every nth row value in sql
I have a select statement in sql: SELECT DISTINCT [Year] FROM [data_list] ORDER BY [YEAR] ASC; Result: Year —- 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 I want list of year in 3 …