Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Complex Query

Author  Topic 

abenitez77
Yak Posting Veteran

53 Posts

Posted - 2014-09-18 : 02:49:08

I want to know if my value in a specific column exists in several rows in another table.
i.e. Table1 has a column called indexcolumn and it has 1 record with the values "cltstyle, InvNbr, PoNbr"
Table1:
indexColumn
cltstyle, InvNbr, PoNbr

Table2 has 2 columns (columnName, indexId). There are many records and I want to query the rows that have the 3 exact values above with the same indexid.

indexid, ColumnName
1, cltstyle
2, id
3, Tab9
4, cltstyle
4, InvNbr
4, PoNbr
5, InvNbr
6, ID
6, PoNbr
6, InvNbr

Results:
4, cltstyle
4, InvNbr
4, PoNbr

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-09-18 : 03:49:51
First version

;with Table1
AS
(SELECT 'cltstyle, InvNbr, PoNbr' AS indexColumn)
,Table2
AS
(SELECT 1 AS indexid, 'cltstyle' AS ColumnName UNION ALL
SELECT 2, 'id' UNION ALL
SELECT 3, 'Tab9' UNION ALL
SELECT 4, 'cltstyle' UNION ALL
SELECT 4, 'InvNbr' UNION ALL
SELECT 4, 'PoNbr' UNION ALL
SELECT 5, 'InvNbr' UNION ALL
SELECT 6, 'ID' UNION ALL
SELECT 6, 'PoNbr' UNION ALL
SELECT 6, 'InvNbr'UNION ALL
SELECT 4, 'PoNbr' )

, TableR
AS
(
SELECT
indexid
,ColumnName
,ROW_NUMBER() OVER(PARTITION BY indexid , ColumnName ORDER BY indexid,ColumnName) AS RN
,CASE WHEN CHARINDEX (ColumnName,indexColumn,0)>0 THEN 1 --columnName 'cltstyle' in indexColumn 'cltstyle, InvNbr, PoNbr'
ELSE 0 END AS Positions
,LEN(indexColumn) - LEN(REPLACE(indexColumn,',','')) + 1 AS NoOfColumns --Numbers of columns from indexColumn
FROM
Table2
CROSS JOIN Table1
)

, TableB
AS
(
SELECT
A.indexid
,A.ColumnName
,SUM(A.Positions) OVER(PARTITION BY indexid ORDER BY indexid,ColumnName ) AS X
,NoOfColumns
FROM
TableR AS A
WHERE A.RN = 1
AND A.Positions = 1
)

SELECT
indexID
,ColumnName
FROM
TableB as B
WHERE
B.indexID IN (select indexID FROM TableB GROUP BY indexID,NoOfColumns HAVING MAX(x)=NoOfColumns)




output:

indexID ColumnName
4 cltstyle
4 InvNbr
4 PoNbr




sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-09-18 : 04:19:14
ver2


;with TableSplit
AS
( SELECT
iIndex
,REPLACE(splitData,' ','') AS ColumnName
FROM
Table1
CROSS APPLY dbo.fnSplitString(indexColumn,',') )
,
TableResult
AS
(
SELECT
A.indexID
,A.ColumnName
,S.iIndex
,ROW_NUMBER() OVER (PARTITION BY A.indexID,A.ColumnName Order by A.indexID,A.ColumnName) AS RN
,DENSE_RANK() OVER (PARTITION BY A.indexID Order by A.indexID,A.ColumnName) AS Dk
FROM
Table2 AS A
LEFT JOIN TableSplit AS S
ON A.ColumnName = S.ColumnName
)


SELECT
indexID
,ColumnName
FROM TableResult
WHERE
1=1
--AND indexID NOT IN (SELECT indexID FROM TableResult WHERE iIndex IS NULL)
AND indexID IN (SELECT indexID FROM TableResult GROUP BY indexID HAVING MAX(iIndex) = (SELECT MAX(iIndex) FROM TableSplit))
AND iIndex = Dk
AND RN = 1



indexID ColumnName
4 cltstyle
4 InvNbr
4 PoNbr



sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-09-18 : 04:21:10
I used a split function;
you can find it :
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/
little updated to have index column


sabinWeb MCP
Go to Top of Page

abenitez77
Yak Posting Veteran

53 Posts

Posted - 2014-09-19 : 12:33:47
I'm having trouble understanding how to translate this to my tables. I could not paste the image so I am pasting data from the 2 tables. Also the code, I edited:


;with TableSplit
AS
( SELECT distinct
RequiredObjectsID as iIndex
,REPLACE(IndexColumn,' ','') AS ColumnName
FROM
PRGX_AS_DEV_SandBox_REFERENCE_TABLES.dbo.RequiredObjects
CROSS APPLY dbo.fnSplitString(indexColumn,',') )
,
TableResult
AS
(
SELECT
A.indexID
,A.ColumnName
,S.iIndex
,ROW_NUMBER() OVER (PARTITION BY A.indexID,A.ColumnName Order by A.indexID,A.ColumnName) AS RN
,DENSE_RANK() OVER (PARTITION BY A.indexID Order by A.indexID,A.ColumnName) AS Dk
FROM
( SELECT
TableName = t.name,
IndexName = ind.name,
IndexId = ind.index_id,
ColumnId = ic.index_column_id,
ColumnName = col.name,
ind.is_unique,
ind.is_primary_key,
ic.is_included_column,
col.is_nullable,
col.is_identity,
col.is_computed
FROM
sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
WHERE
t.is_ms_shipped = 0
--ORDER BY
-- t.name, ind.name, ind.index_id, ic.index_column_id
) AS A
LEFT JOIN TableSplit AS S
ON A.ColumnName = S.ColumnName
)


SELECT
indexID
,ColumnName
FROM TableResult
WHERE
1=1
--AND indexID NOT IN (SELECT indexID FROM TableResult WHERE iIndex IS NULL)
AND indexID IN (SELECT indexID FROM TableResult GROUP BY indexID HAVING MAX(iIndex) = (SELECT MAX(iIndex) FROM TableSplit))
AND iIndex = Dk
AND RN = 1


Table1:
RequiredObjectsID ObjectName ObjectType AuditType ObjectLocation Role_Name IndexColumn IndexTable DateEntered
30 TEST INDEX ALL Server NULL CltStyle, InvNbr, PoNbr TAB06 2014-09-17 21:52:00


-------------------------------------------------------------------
Table2(query):
TableName IndexName IndexId ColumnId ColumnName is_unique is_primary_key is_included_column is_nullable is_identity is_computed
Tab06 TEST 123 1 Cltstyle 0 0 0 1 0 0
Tab06 TEST 123 2 InvNbr 0 0 0 1 0 0
Tab06 TEST 123 3 PoNbr 0 0 1 1 0 0
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-09-19 : 13:31:01
[code]DECLARE @Table1 TABLE
(
IndexColumn VARCHAR(100) NOT NULL
);

INSERT @Table1
(
IndexColumn
)
VALUES ('cltstyle, InvNbr, PoNbr');

DECLARE @Table2 TABLE
(
IndexID INT NOT NULL,
ColumnName VARCHAR(20) NOT NULL
);

INSERT @Table2
(
IndexID,
ColumnName
)
VALUES (1, 'cltstyle'),
(2, 'id'),
(3, 'Tab9'),
(4, 'cltstyle'),
(4, 'InvNbr'),
(4, 'PoNbr'),
(5, 'InvNbr'),
(6, 'ID'),
(6, 'PoNbr'),
(6, 'InvNbr');

-- SwePeso
SELECT t2.IndexID,
t2.ColumnName
FROM @Table2 AS t2
CROSS APPLY (
SELECT ', ' + x.ColumnName
FROM @Table2 AS x
WHERE x.IndexID = t2.IndexID
ORDER BY x.ColumnName
FOR XML PATH(''),
TYPE
) AS f(Data)
INNER JOIN @Table1 AS t1 ON t1.IndexColumn = STUFF(f.Data.value('(.)', 'VARCHAR(100)'), 1, 2, '');[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

abenitez77
Yak Posting Veteran

53 Posts

Posted - 2014-09-19 : 17:30:41
Thanks, that worked like a charm
Go to Top of Page
   

- Advertisement -