Skip to content

Tag: sql-server

Join many to many tables optionally

I have below tables User Intrest Hobby User_Intrest User_Hobby Now to find user ids who have both interests Eating and Sleeping I have written Output Same as above I also can find user ids with hobbies Smoking, Hiking, Browsing as below Output Now I want to mix these two optionally in a way that if only inter…

A problem with a simple join taking too long to finish

I´m having trouble identifying who to fix this simple join. My problem is, after adding the column B.CIDPRODUCT IS NULL this query takes hours to finish. table BO_PRICER001 rows 286537 table BO_PRODUCTCONFIG rows 7934844 on the table BO_PRICER001 exists 15329 rows with null with B.CIDPRODUCT IS NULL the live …

SQLGroup with Distinct count

I have query Outcome Not sure why not group by range and expect results. Thank you. Answer I would suggest cross apply to define the alias: SQL Server doesn’t allow aliases as GROUP BY keys. I think defining the alias in the FROM clause is the simplest method; you could also use a CTE or subquery or rep…

Find a word from a string in SQL Server

I have some values into a table like below :- Bank Of America testBank Of America State Bank Of India ICICI Bank Test @Bank* I want a query to get all the values which contains Bank only. Proper word should match. Result should be like :- Bank Of America State Bank Of India ICICI Bank Answer One method is to

SQL Server : assistance with INSERT INTO

I have a problem where I need to create new entries in the SpecialOfferProduct table. I am applying my created discount (SpecialOfferID = 20) to all products that have an inventory greater than 1800 but I can’t figure out what I am doing wrong. this is my code. It’s from the AdventureWorks2012 dat…