I have a reminder table (ParentId) – Foreign key to ProductId from Product and also OrderId from Order table. Is it possible? When I try to insert the data for the Reminder table belongs to OrderId, I’m getting Foreign-Key constraint error. Answer You can do this with a bit of work, using foreign …
Tag: sql-server
How do I generate random number with a range X times in SQL SERVER
I want to generate a random number X times with a range. I’ve tried to create a function: CREATE FUNCTION Random_Number (@Times INT, @Upper BIGINT,@Lower BIGINT, @randomvalue numeric(18,10)) RETURNS …
The INSERT statement conflicted with the FOREIGN KEY constraint in Entity framework core, 2 tables in single statement
I have following 2 classes. public partial class Query { public int Id { get; set; } public virtual QueryGroup QueryGroup { get; set; } } public partial class QueryGroup { public int Id {…
How to do an UPDATE with several SELECT in value to update in MSSQL
I’m trying to update a value which is the result of 2 SELECT UPDATE table2 SET value = (SELECT value FROM table1 WHERE table1.id = (SELECT value2 FROM table3 WHERE table3 = Value3 )), …
SQL – Aggregates data with dates interval
i’m facing a problem joining two table with date interval that represent user status change. ID is the same in both table. Table_A ID StatusA FromA ToA 1 Active 01/01/2020 10:…
Query for Exam Score calculation
Problem Statement: I have an EXAM table which has following fields and sample data I want to write a SQL query to output the following fields My SQL Query: Can I achieve my result by writing a better SQL query? Answer You could indeed join the table with several aggregate subqueries that compute the relevant …
Performance improvement Outer Apply
I have a query which runs slower. Order By inside the Outer Apply is very costly, is there any better way to write this query ? Answer From what I can tell in the code, you seem to be implementing a variation of lag(ignore nulls). Here is a better way: Also note that in many databases the “v” in v…
Writing a Single Query w/ Multiple CTE Subqueries SQL/R
I have some data I would like to pull from a database, I’m using RStudio for my query. What I intend to do is write: The first CTE statement to pull all my necessary information. The second CTE statement will add two new columns for two row numbers, which are partitioned by different groups. Two additio…
TSQL – Union records based on partial uniqueness
Sample Data Currently inserted data: Goal: To insert record into the table variable (or cte/temp table) based on uniqueness ONLY ON [Id] field. “UNION” would not do that because, at minimum, [Group] is going to make duplicate records based on just [Id]… unique. Oh, and [Group] value basicall…
Flag the date when they return
Story: For each id , they have a join date to a subscription and when they get rebilled monthly, they have a returning date. The first part of the exercise was to flag consecutive months of returned dates from the join date. Here’s an example: Objective: What I would like to add is to flag those who ret…