I have a Projects table with ID and Responsible manager. The Responsible manager columns has values as John,Jim for Project 1 and Jim,Julie for Project 2.
But if I pass Jim to my stored procedure I should get 2 projects (1,2). This returns no rows because the column is John,Jim but SQL Server is looking for ='Jim':
select distinct ID,Manager from Projects where Manager=#Manager
WHERE ',' + Manager + ',' LIKE '%,Jim,%'
Or I suppose to match your actual code:
WHERE ',' + Manager + ',' LIKE '%,' + #Manager + ',%'
Note that your design is extremely flawed. There is no reason you should be storing names in this table at all, never mind a comma-separated list of any data points. These facts are important on their own, so treat them that way!
CREATE TABLE dbo.Managers
(
ManagerID INT PRIMARY KEY,
Name NVARCHAR(64) NOT NULL UNIQUE, ...
);
CREATE TABLE dbo.Projects
(
ProjectID INT PRIMARY KEY,
Name NVARCHAR(64) NOT NULL UNIQUE, ...
);
CREATE TABLE dbo.ProjectManagers
(
ProjectID INT NOT NULL FOREIGN KEY REFERENCES dbo.Projects(ProjectID),
ManagerID INT NOT NULL FOREIGN KEY REFERENCES dbo.Managers(ManagerID)
);
Now to set up the sample data you mentioned:
INSERT dbo.Managers(ManagerID, Name)
VALUES(1,N'John'),(2,N'Jim'),(3,N'Julie');
INSERT dbo.Projects(ProjectID, Name)
VALUES(1,N'Project 1'),(2,N'Project 2');
INSERT dbo.ProjectManagers(ProjectID,ManagerID)
VALUES(1,1),(1,2),(2,2),(2,3);
Now to find all the projects Jim is managing:
DECLARE #Manager NVARCHAR(32) = N'Jim';
SELECT p.ProjectID, p.Name
FROM dbo.Projects AS p
INNER JOIN dbo.ProjectManagers AS pm
ON p.ProjectID = pm.ProjectID
INNER JOIN dbo.Managers AS m
ON pm.ManagerID = m.ManagerID
WHERE m.name = #Manager;
Or you can even manually short circuit a bit:
DECLARE #Manager NVARCHAR(32) = N'Jim';
DECLARE #ManagerID INT;
SELECT #ManagerID = ManagerID
FROM dbo.Managers
WHERE Name = #Manager;
SELECT p.ProjectID, p.Name
FROM dbo.Projects AS p
INNER JOIN dbo.ProjectManagers AS pm
ON p.ProjectID = pm.ProjectID
WHERE pm.ManagerID = #ManagerID;
Or even more:
DECLARE #Manager NVARCHAR(32) = N'Jim';
DECLARE #ManagerID INT;
SELECT #ManagerID = ManagerID
FROM dbo.Managers
WHERE Name = #Manager;
SELECT ProjectID, Name
FROM dbo.Projects AS p
WHERE EXISTS
(
SELECT 1
FROM dbo.ProjectManagers AS pm
WHERE pm.ProjectID = p.ProjectID
AND pm.ManagerID = #ManagerID
);
As an aside, I really, really, really hope the DISTINCT in your original query is unnecessary. Do you really have more than one project with the same name and ID?
In a WHERE clasue a = operator looks for an exact match. You can use a LIKE with wildcards for a partial match.
where Manager LIKE '%Jim%'
You may try the following:
SELECT DISTINCT
ID,
Manager
FROM
Projects
WHERE
(
(Manager LIKE #Manager + ',*') OR
(Manager LIKE '*,' + #Manager) OR
(Manager = #Manager)
)
That should cover both names and surnames, while still searching for literal values. Performance can be a problem however, depending on table
please tried with below query
select distinct ID,Manager from Projects where replace('#$#' + Manager + '#$#', ',', '#$#') like '%Jim%'
Related
hopefully someone can help with this. I have recieved a table of data which I need to restructure and build a Denorm table out of. The table structure is as follows
UserID Logon ParentID
2344 Test1 2000
2345 Test2 2000
The issue I have is the ParentID is also a UserID of its own and in the same table.
SELECT * FROM tbl where ParentID=2000 gives the below output
UserID Logon ParentID
2000 Test Team 2500
Again, the ParentID of this is also stored as a UserID..
SELECT * FROM tbl where ParentID=2500 gives the below output
UserID Logon ParentID
2500 Test Division NULL
I want a query that will pull all of these relationships and the logons into one row, with my output looking like the below.
UserID Username Parent1 Parent2 Parent3 Parent4
2344 Test1 Test Team Test Division NULL NULL
2345 Test2 Test Team Test Division NULL NULL
The maximum number of parents a user can have is 4, in this case there is only 2. Can someone help me with the query needed to build this?
Appreciate any help
Thanks
Jess
You can use basicly LEFT JOIN. If you have static 4 parent it should work. If you have unknown parents you should do dynamic query.
SELECT U1.UserId
,U1.UserName
,U2.UserName AS Parent1
,U3.UserName AS Parent2
,U4.UserName AS Parent3
,U5.UserName AS Parent4
FROM Users U1
LEFT JOIN Users U2 ON U1.ParentId = U2.UserId
LEFT JOIN Users U3 ON U2.ParentId = U3.UserId
LEFT JOIN Users U4 ON U3.ParentId = U4.UserId
LEFT JOIN Users U5 ON U4.ParentId = U5.UserId
EDIT : Additional(to exclude parent users from the list) :
WHERE NOT EXISTS (SELECT 1 FROM Users UC WHERE U1.UserId = UC.ParentId)
select
tb1.UserId as UserId,
tb1.UserName as UserName,
tb2.UserName as Parent1,
tb3.UserName as Parent2,
tb4.UserName as Parent3,
tb5.UserName as Parent4
from tbl t1
left join tbl t2 on t2.UserId=t1.ParentID
left join tbl t3 on t3.UserId=t2.ParentID
left join tbl t4 on t4.UserId=t3.ParentID
left join tbl t5 on t5.UserId=t4.ParentID;
you need to do 4 left joins in order to fetch 4 parent details
Use a recursive CTE to get the levels then pivot to put them in columns:
WITH cte(UserID, Logon, ParentID, ParentLogon, ParentLevel) AS
(
SELECT UserID, Logon, ParentID, Logon, 0
FROM users
UNION ALL
SELECT u.UserID, u.Logon, u.ParentID, cte.ParentLogon, ParentLevel + 1
FROM users u
JOIN cte ON cte.UserID = u.ParentID
)
SELECT UserId, Logon, Parent1, Parent2, Parent3, Parent4 FROM cte
PIVOT (
MAX(ParentLogon)
FOR ParentLevel
IN (
1 AS Parent1,
2 AS Parent2,
3 AS Parent3,
4 AS Parent4
)
)
See SQL Fiddle example
In order to get all parent or child, it's efficient to use a recursive function which would fetch the whole hierarchy.
Sample Table:
CREATE TABLE #TEST
(
[Name] varchar(100),
ManagerName Varchar(100),
Number int
)
Insert some values
Insert into Test values
('a','b'), ('b','c'), ('c','d'), ('d','e'), ('e','f'), ('f','g')
Create recursive function as below
CREATE FUNCTION [dbo].[fnRecursive] (#EmpName Varchar(100), #incremental int)
RETURNS #ret TABLE
(
ManagerName varchar(100),
Number int
)
AS
BEGIN
Declare #MgrName varchar(100)
SET #MgrName = (Select ManagerName from test where [name] = #EmpName)
Insert into #ret values (#MgrName, #incremental)
if(#MgrName is not null)
BEGIN
SET #incremental = #incremental + 1;
Insert into #ret
Select ManagerName, Number from [fnRecursive](#MgrName, #incremental)
END
RETURN;
END
If this function is joined with table, it should list the hierarchy for all employees
CREATE TABLE #TEST
(
[Name] varchar(100),
ManagerName Varchar(100),
Number int
)
Insert into #TEST
Select x.[Name], x.ManagerName,x.number from (
select t.[Name],a.ManagerName as managerName, a.number as number from TEST t outer apply
(
select * from [fnRecursive](t.[Name],1)
) a)
x
Select * from #Test
If we do a pivot on the table (excluding the 'Number' column). Assuming we store in the table "#temp" it should list all the managers as a column.
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(c.[ManagerName] )
FROM #temp c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'select * from #temp
pivot
(
min([managername])
for managername in (' + #cols + ')
) p '
execute(#query)
But this doesn't name the column as 'Parent1', 'Parent2' instead with the dynamic column name.
Link below should help to set custom column name for the dynamic pivot table
https://stackoverflow.com/questions/16614994/sql-server-pivot-with-custom-column-names
I am using Sql-Server 2016 in a C# application.
Let's say I have two tables:
CREATE TABLE Table_A
(
UserID NVARCHAR2(15),
FullName NVARCHAR2(25),
Available NUMBER(1),
MachineID NVARCHAR2(20),
myDate date
);
and
CREATE TABLE Table_B
(
UserID NVARCHAR2(15),
FullName NVARCHAR2(25),
Team NVARCHAR2(15),
MachineID NVARCHAR2(20),
Stuff NUMBER(2)
);
I want to perform a global select so that I will get as result data from both tables, somehow concatenated and of course, when a column does not exist in one of the tables, that column to be automatically populated with NULL, and if a column exists on both tables the results must be merged in a single column.
The first solution that pops-up is a UNION with NULL aliases for the missing columns, sure. The problem is that at runtime I will not be able to know in advance which tables are interrogated so that I could anticipate the column names. I need a more general solution.
The expected result from the two tables must look like this:
user_Table_A; fullName_Table_A; 1; machineID_Table_A; 12-JUN-18; NULL; 10;
user_Table_B; fullName_Table_B; NULL; machineID_Table_B; NULL; team_Table_B; 20;
The data for the two tables is inserted with the following commands:
INSERT INTO Table_A VALUES ('user_Table_A', 'fullName_Table_A', 1, 'machineID_Table_A', TO_DATE('12-06-2018', 'DD-MM-YYYY'));
INSERT INTO Table_B VALUES ('user_Table_B', 'fullName_Table_B', 'team_Table_B', 'machineID_Table_B', 20);
You can do something like this. I havent have time to completely tweak it, so there can be something the order of the columns. But perhaps it can get you started:
You also write that you use Oracle - Im not sure what you wanted, but this is in pure sql-server version.
SQL:
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
/*Then it exists*/
DROP TABLE #temp;
GO
DECLARE #SQLList nvarchar(max)
DECLARE #SQLList2 nvarchar(max)
DECLARE #SQL nvarchar(max)
with table_a as (
select column_name as Table_aColumnName,ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'table_a'
)
,
table_b as (
select column_name as Table_bColumnName,ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'table_b'
)
,preresult as (
select case when Table_aColumnName IS null then 'NULL as ' + Table_bColumnName else Table_aColumnName end as Table_a_ColumnName,case when Table_bColumnName IS null then 'NULL as ' +Table_aColumnName else Table_bColumnName end as Table_b_ColumnName
,a.ORDINAL_POSITION,b.ORDINAL_POSITION as Table_b_Ordinal from table_a a full join Table_B b on a.Table_aColumnName = b.Table_bColumnName
)
select * into #temp from preresult
SET #SQLList = (
select distinct display = STUFF((select ','+table_a_columnName from #temp b order by table_b_ordinal FOR XML PATH('')),1,1,'') from #temp a
)
SET #SQLList2 = (
select distinct display = STUFF((select ','+table_b_columnName from #temp b order by Table_b_Ordinal FOR XML PATH('')),1,1,'') from #temp a
)
SET #SQL = 'select ' +#SQLList +' from dbo.Table_a union all select ' + #SQLList2 + ' from dbo.table_b'
exec(#SQL)
Result:
I have a query I am writing to test for permission access. One of the columns I have to look through is in the format of ABC|DEF|GHI|JKL and I need to see if there is a value that exists in that column.
The problem is, there are multiple things I need to test for which is causing my query to throw an error due to the subquery returning multiple values.
-- Default permission
DECLARE #hasAccess BIT = 0;
-- Define our temp data
DECLARE #managers AS TABLE(personnelID VARCHAR(10))
-- Insert our data for the manager test logic
INSERT INTO #managers( personnelID ) VALUES ( 'ABC' )
INSERT INTO #managers( personnelID ) VALUES ( 'XYZ' )
SELECT *
FROM Employees AS e
WHERE e.QID = #QID
AND e.PersonnelIDList LIKE '%' + (SELECT personnelID FROM #managers) + '%'
How can I go about testing to see if any one of the values in #managers exists in the column value (example column value: ABC|DEF|GHI|JKL) exists in the records.
In like clause, you're only allowed to have the subquery return a single result.
Join to your #managers table and use the like within the join clause.
-- Default permission
DECLARE #hasAccess BIT = 0;
-- Define our temp data
DECLARE #managers AS TABLE(personnelID VARCHAR(10))
-- Insert our data for the manager test logic
INSERT INTO #managers( personnelID ) VALUES ( 'ABC' )
INSERT INTO #managers( personnelID ) VALUES ( 'XYZ' )
SELECT
e.*
FROM
Employees AS e
inner join #managers as m
on e.PersonnelIDList LIKE '%' + m.personnelID + '%'
WHERE
e.QID = #QID
You should not be storing lists as strings. This is just a bad idea. There should be a separate table instead.
Sometimes, we are stuck with other people's really bad design decisions. In this case, you can use an exists subquery:
SELECT e.*
FROM Employees e
WHERE e.QID = #QID AND
EXISTS (SELECT 1
FROM #managers m
WHERE '|' + e.PersonnelIDList + '|' LIKE '%|' +personnelID + '|%'
);
Table1
ID Name Tags
----------------------------------
1 Customer1 Tag1,Tag5,Tag4
2 Customer2 Tag2,Tag6,Tag4,Tag11
3 Customer5 Tag6,Tag5,Tag10
and Table2
ID Name Tags
----------------------------------
1 Product1 Tag1,Tag10,Tag6
2 Product2 Tag2,Tag1,Tag5
3 Product5 Tag1,Tag2,Tag3
what is the best way to join Table1 and Table2 with Tags column?
It should look at the tags column which coma seperated on table 2 for each coma seperated tag on the tags column in the table 1
Note: Tables are not full-text indexed.
The best way is not to have comma separated values in a column. Just use normalized data and you won't have trouble with querying like this - each column is supposed to only have one value.
Without this, there's no way to use any indices, really. Even a full-text index behaves quite different from what you might thing, and they are inherently clunky to use - they're designed for searching for text, not meaningful data. In the end, you will not get much better than something like
where (Col like 'txt,%' or Col like '%,txt' or Col like '%,txt,%')
Using a xml column might be another alternative, though it's still quite a bit silly. It would allow you to treat the values as a collection at least, though.
I don't think there will ever be an easy and efficient solution to this. As Luaan pointed out, it is a very bad idea to store data like this : you lose most of the power of SQL when you squeeze what should be individual units of data into a single cell.
But you can manage this at the slight cost of creating two user-defined functions. First, use this brilliant recursive technique to split the strings into individual rows based on your delimiter :
CREATE FUNCTION dbo.TestSplit (#sep char(1), #s varchar(512))
RETURNS table
AS
RETURN (
WITH Pieces(pn, start, stop) AS (
SELECT 1, 1, CHARINDEX(#sep, #s)
UNION ALL
SELECT pn + 1, stop + 1, CHARINDEX(#sep, #s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT pn AS SplitIndex,
SUBSTRING(#s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS SplitPart
FROM Pieces
)
Then, make a function that takes two strings and counts the matches :
CREATE FUNCTION dbo.MatchTags (#a varchar(512), #b varchar(512))
RETURNS INT
AS
BEGIN
RETURN
(SELECT COUNT(*)
FROM dbo.TestSplit(',', #a) a
INNER JOIN dbo.TestSplit(',', #b) b
ON a.SplitPart = b.SplitPart)
END
And that's it, here is a test roll with table variables :
DECLARE #A TABLE (Name VARCHAR(20), Tags VARCHAR(100))
DECLARE #B TABLE (Name VARCHAR(20), Tags VARCHAR(100))
INSERT INTO #A ( Name, Tags )
VALUES
( 'Customer1','Tag1,Tag5,Tag4'),
( 'Customer2','Tag2,Tag6,Tag4,Tag11'),
( 'Customer5','Tag6,Tag5,Tag10')
INSERT INTO #B ( Name, Tags )
VALUES
( 'Product1','Tag1,Tag10,Tag6'),
( 'Product2','Tag2,Tag1,Tag5'),
( 'Product5','Tag1,Tag2,Tag3')
SELECT * FROM #A a
INNER JOIN #B b ON dbo.MatchTags(a.Tags, b.Tags) > 0
I developed a solution as follows:
CREATE TABLE [dbo].[Table1](
Id int not null,
Name nvarchar(250) not null,
Tag nvarchar(250) null,
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Table2](
Id int not null,
Name nvarchar(250) not null,
Tag nvarchar(250) null,
) ON [PRIMARY]
GO
get sample data for Table1, it will insert 28000 records
INSERT INTO Table1
SELECT CustomerID,CompanyName, (FirstName + ',' + LastName)
FROM AdventureWorks.SalesLT.Customer
GO 3
sample data for Table2.. i need same tags for Table2
declare #tag1 nvarchar(50) = 'Donna,Carreras'
declare #tag2 nvarchar(50) = 'Johnny,Caprio'
get sample data for Table2, it will insert 9735 records
INSERT INTO Table2
SELECT ProductID,Name, (case when(right(ProductID,1)>=5) then #tag1 else #tag2 end)
FROM AdventureWorks.SalesLT.Product
GO 3
My Solution
create TABLE #dt (
Id int IDENTITY(1,1) PRIMARY KEY,
Tag nvarchar(250) NOT NULL
);
I've create temp table and i will fill with Distinct Tag-s in Table1
insert into #dt(Tag)
SELECT distinct Tag
FROM Table1
Now i need to vertical table for tags
create TABLE #Tags ( Tag nvarchar(250) NOT NULL );
Now i'am fill #Tags table with While, you can use Cursor but while is faster
declare #Rows int = 1
declare #Tag nvarchar(1024)
declare #Id int = 0
WHILE #Rows>0
BEGIN
Select Top 1 #Tag=Tag,#Id=Id from #dt where Id>#Id
set #Rows =##RowCount
if #Rows>0
begin
insert into #Tags(Tag) SELECT Data FROM dbo.StringToTable(#Tag, ',')
end
END
last step : join Table2 with #Tags
select distinct t.*
from Table2 t
inner join #Tags on (',' + t.Tag + ',') like ('%,' + #Tags.Tag + ',%')
Table rowcount= 28000 Table2 rowcount=9735 select is less than 2 second
I use this kind of solution with paths of trees. First put a comma at the very begin and at the very end of the string. Than you can call
Where col1 like '%,' || col2 || ',%'
Some database index the column also for the like(postgres do it partially), therefore is also efficient. I don't know sqlserver.
I need to write a stored procedure that will take in a string to search a tree like structure and perform a recursive result set. First, here is the table:
CREATE TABLE [dbo].[WorkAreas] (
[Id] uniqueidentifier DEFAULT newid() NOT NULL,
[Name] nvarchar(max) COLLATE Latin1_General_CI_AS NULL,
[ParentWorkAreaId] uniqueidentifier NULL,
CONSTRAINT [PK__WorkArea__3214EC073FD07829] PRIMARY KEY CLUSTERED ([Id]),
CONSTRAINT [WorkArea_ParentWorkArea] FOREIGN KEY ([ParentWorkAreaId])
REFERENCES [dbo].[WorkAreas] ([Id])
ON UPDATE NO ACTION
ON DELETE NO ACTION,
)
I'd Like the stored procedure to output the results like this:
Work Area 1 - Child Of Work Area 1 - Child Child Of Work Area 1
So if this were real data it may look like this:
Top Floor - Room 7 - Left Wall
Top Floor - Room 9 - Ceiling
the stored procedure would take in a parameter: #SearchTerm varchar(255)
The search term would look at the results and perform a "contains" query.
So if we passed in "Room 9" the result should come up with the Room 9 example, or if just the word "Room" was passed in, we would see both results.
I am not sure how to construct the SP to recursively build the results.
Cade Roux's comment lead me to what I needed. Here is what I ended up with:
;WITH ProjectWorkAreas (EntityId ,ParentIDs,DisplayText)
AS
(
SELECT Id,CAST(Id AS VARCHAR(1000)) ,CAST(Name AS VARCHAR(1000))
FROM WorkAreas
WHERE ParentWorkAreaId IS NULL And ProjectId = #projectId
UNION ALL
SELECT Id, CAST( ParentIDs+','+ CAST(Id AS VARCHAR(100))
AS VARCHAR(1000)),CAST( DisplayText + ' - ' + Name AS VARCHAR(1000))
FROM WorkAreas AS ChildAreas
INNER JOIN ProjectWorkAreas
ON ChildAreas.ParentWorkAreaId = ProjectWorkAreas.EntityId
)
SELECT * FROM ProjectWorkAreas Where DisplayText like '%' + #searchTerm + '%'
I added the ProjectId to the mix
You would do something like this:
select w1.name, w2.name, w3.name
from workareas w1
inner join workareas w2
on w1.parentworkareaid = w2.id
inner join workareas w3
on w2.parentworkareaid = w3.id
where contains(w3.name, #yourSearchString)