Merge not inserting new values

I’m trying to use MERGE to insert new values to a table only if they don’t already exists in the same table. This is the query I am using: MERGE [dbo].[TARGET_TABLE] AS Target USING (SELECT [NAME] …

How to filter the record based on two columns in sql

i have a table with 2 columns and i wanted to filter the records where column1 in (Hierarchy,Single) and column2 in (‘Relate’, ‘NoRelate’). but not return the record when Column1 = Single and Column2 =…

Update script for a column with (max+1 for the column group value) on basis of another id column

As shown in the image. I have a table with Id, OrderId, SubId. For which the some row values in SubId column is zero as shown in the left table in the image. I have to update the SubId rows which are …

Delete rows from table using JOIN – SQL Server

I have a table_A – id | name | is_active | —-+———+————+ 1 | jon | 1 | 2 | ham | 0 | 3 | gary | null | I have a table_B – id | name | -…

Normalize dates to periods starting at same origin

I’m attempting to track the evolution of customer spending over time. Essentially, I need to identify the first instance of customer purchase, assign it a rank of 1, and assign all of their purchases …

UNION two SELECT queries but result set is smaller than one of them

In a SQL Server statement there is SELECT id, book, acnt, prod, category from Table1 UNION SELECT id, book, acnt, prod, category from Table2 The first …

Need to match a field with another that has commas on it’ s value

I would like to match the field values of “FORMULATION” from TABLE 1 to “C_TEST_ARTICLE” from table 2, that has mutiple of these formulation sepparated by commas. Table 1: +——–…

Arithmetic overflow error converting nvarchar to data type numeric in case query

I am trying to show the number of quotes won as a percentage of the total number of quotes in a time frame. For example. I have a total of 294 quotes and 173 of them are won. The result I am looking …

How can I get the percentage of time per user in sql?

I am making an app where people can select a song and listen to it. Whenever someone plays a song it should display the percentage of the how much youve listened in an overview. I have the following …

Sql Server Global Blocked List and Item specific Allow List

This seems easy at first but after thinking about it for the day I’m stuck. Imagine you have a global block list for veggies. create table blocked_veggie(veggieid int primary key, veggiename varchar(…