[Solved] SQL Group by and intersect between internal columns [closed]

SELECT Category, Tag1Value FROM table_name t1 WHERE EXISTS (SELECT 1 FROM table_name WHERE Tag2Value = t1.Tag1Value) UPDATE Try this : SELECT res.Category, res.tag, COUNT(res.tag) FROM (SELECT DISTINCT Category, Tag1Value tag FROM table_name UNION ALL SELECT DISTINCT Category, Tag2Value tag FROM table_name) res GROUP BY res.Category, res.tag HAVING COUNT(res.tag)>1 It return : category | tag ———————————– … Read more

[Solved] Can I know the issue of this SQL query

It’s because a CASE WHEN can only return 1 value. And a STRING_SPLIT returns a resultset. I assume something like this is what you want. SELECT * FROM Facility f WHERE (@Facility IS NULL OR f.facilityCode IN (SELECT value FROM string_split(@Facility,’,’))) This will get all records if the variable is null. 2 solved Can I … Read more

[Solved] How to perform complex join on three tables [closed]

This should do it. If CategoryNAME coming back as null, then you’re doing a LEFT OUTER join and have referential integrity issues. CREATE PROCEDURE usp_OrderDetails_SelectByOrderId @OrderID INT AS BEGIN SELECT P.ProductId, OD.Qty, P.Price, C.NAME AS CategoryNAME FROM tblProducts P INNER JOIN tblCategories C ON P.CategoryId = C.CategoryId INNER JOIN tblOrderDetails OD ON P.ProductId = OD.ProductId … Read more

[Solved] Find contact in location tree

You could use a recursive common-table expression to walk up until you find a parent with a contact. For example: ; with CTE as ( select ID , ContactID , ParentID , ID BaseID , 1 as Level from Location where ID in (4,5) union all select parent.ID , parent.ContactID , parent.ParentID , child.BaseID , … Read more

[Solved] SQL query to remove data duplication when join is used

ORDER BY clause is used here as per desired output ordering. It’ll meet expectation. — SQL Server SELECT f.Primary_Brand_Key , t.Market , f.Food , t.TV_Spends , t.Print_Spends , COALESCE(t.TV_Spends, 0) + COALESCE(t.Print_Spends, 0) “Total_Spends” FROM Food f INNER JOIN ( SELECT Primary_Brand_Key , Market , SUM(CASE WHEN Medium = ‘TV’ THEN Spent END) “TV_Spends” , … Read more

[Solved] Need Query – Row count with loop

declare @timediff table(id int identity(1,1),Name varchar(50),logtime datetime) insert into @timediff Select Name,logtime from timediff declare @datetimeDiff int declare @i int=1 while(@i<=(Select count(*) from @timediff)) Begin Select @datetimeDiff=datediff(hour,logtime, (Select logtime from @timediff where id=@i+1)) from @timediff where id=@i if(@datetimeDiff>=1) BEGIN Select @datetimeDiff –You can write your update code here END Set @i=@i+2 END 0 solved Need … Read more

[Solved] How to handle this scenario in single SSIS package?

This will help. how-to-read-data-from-an-excel-file-starting-from-the-nth-row-with-sql-server-integration-services Copying the solution here in case the link is unavailable Solution 1 – Using the OpenRowset Function Solution 2 – Query Excel Sheet Solution 3 – Google It Google it, The information above is from the first search result 3 solved How to handle this scenario in single SSIS package?

[Solved] Query to get table name by searching data in SQL Server 20008 [closed]

You can search for a table name in a single database like follows: USE YourDatabaseName SELECT * FROM sys.Tables WHERE name LIKE ‘%SearchText%’ You can also search for a table name in all databases like Pinal Dave describes here: CREATE PROCEDURE usp_FindTableNameInAllDatabase @TableName VARCHAR(256) AS DECLARE @DBName VARCHAR(256) DECLARE @varSQL VARCHAR(512) DECLARE @getDBName CURSOR SET … Read more