I am looking to find the sum of the current + the last X Nth Rows. I am able to do this with the following query, however it is not very scalable. If this example, I’m finding the current value of “amount”, plus the last 3 “amounts” split 6 apart: X = 3 N = 6 I will be using
Tag: sql-server
Calculate years based on months in SQL Server
I want to write a function that takes the number of months as a parameter and does the following: I can hardcode all of this using case statements but I wanted to know if there is a dynamic way of doing it. Thanks! Answer Try this:
Connect to SQL Server in Python with ReadOnly
I am trying to connect to the SQL server in python using pyodbc. I am unable to connect because my database is ReadOnly. I tried the solutions provided at other link: But, I am still getting the following error: ProgrammingError: (‘42000’, “[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The target database (‘PRMOperationalDB’) is in an availability group and is currently accessible for
How can I Insert multiple rows with one query
I made a code allowing me to GET data from an API and insert it into a database. But I encounter a problem my api contains about 20 million data and to insert everything in my database it will take me 43 days 🙂 I think the problem comes from the fact that I insert the data one by one.
How to apply max function in a join query?
I am new to SQL Server, I have 3 tables consider an employee table empid name location 1 abc USA 2 efg UK Another table named location-table location holidaycode uk uk1 usa usa1 And also holidaytable: holiday-code date type uk1 2022-01-01 LM uk1 2022-01-01 RMC Expected result is: empid location holidaycode date type 1 2 uk uk1 2022-01-01 RMC suppose
How to use not in / not like to compare values from different tables in SQL
I have SQL 2019 with these two tables – Orders and Customers with respective columns in Orders and in Customers. Order_comment is ntext type. I need to update the order_comment with customer_name value. The update should be something like this: If the customer_name is already part of the order_comment then the update should not happen. The above query works ok
How to add additional XML node on top of my SQL generated XML
I have generated XML from a SQL Server FOR XML PATH statement as shown here: And this is the result: Now, I’d like to encapsulate the above with these nodes: How will I achieve this in a SQL Query I created above? I am new to SQL Server and XML. I am trying to generate this XML directly from my
Split strings in a column based on text values and numerical values such as patindex
I have a column that displays stock market options data like below: I want to be able to split these up so they show up like: I was able to split the first portion with the ticker name by using the code below, but I don’t know how to split the rest of the strings. edit: for anyone who is
SQL Server – Counting total number of days user had active contracts
I want to count the number of days while user had active contract based on table with start and end dates for each service contract. I want to count the time of any activity, no matter if the customer had 1 or 5 contracts active at same time. In result I want a table: Where 1477 stands by 1053 (days
Same ID with multiple records to single row
I have two tables. Table A look like this. source ID Type_ID Error_info ABC 100 1 country column missing ABC 100 2 conversion factor missing decimals BCA 200 1 error value in height column BCA 200 2 convertion factor should be 0.001 Table B look like this. source ID Type_1_ID Error_info_1 Type_2_ID Error_info_2 ABC 100 BCA 200 I want to