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:indexColumncltstyle, InvNbr, PoNbrTable2 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, ColumnName1, cltstyle2, id3, Tab94, cltstyle4, InvNbr4, PoNbr5, InvNbr6, ID6, PoNbr6, InvNbrResults:4, cltstyle4, InvNbr4, PoNbr |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-09-18 : 03:49:51
|
First version ;with Table1AS (SELECT 'cltstyle, InvNbr, PoNbr' AS indexColumn),Table2AS (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' ), TableRAS( 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), TableBAS( 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 ,ColumnNameFROM TableB as BWHERE B.indexID IN (select indexID FROM TableB GROUP BY indexID,NoOfColumns HAVING MAX(x)=NoOfColumns) output:indexID ColumnName4 cltstyle4 InvNbr4 PoNbr sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-09-18 : 04:19:14
|
ver2;with TableSplitAS( SELECT iIndex ,REPLACE(splitData,' ','') AS ColumnName FROM Table1 CROSS APPLY dbo.fnSplitString(indexColumn,',') ),TableResultAS( 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 TableResultWHERE 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 ColumnName4 cltstyle4 InvNbr4 PoNbrsabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
|
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 TableSplitAS( SELECT distinct RequiredObjectsID as iIndex ,REPLACE(IndexColumn,' ','') AS ColumnName FROM PRGX_AS_DEV_SandBox_REFERENCE_TABLES.dbo.RequiredObjects CROSS APPLY dbo.fnSplitString(indexColumn,',') ),TableResultAS( 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 TableResultWHERE 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 = 1Table1:RequiredObjectsID ObjectName ObjectType AuditType ObjectLocation Role_Name IndexColumn IndexTable DateEntered30 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_computedTab06 TEST 123 1 Cltstyle 0 0 0 1 0 0Tab06 TEST 123 2 InvNbr 0 0 0 1 0 0Tab06 TEST 123 3 PoNbr 0 0 1 1 0 0 |
|
|
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');-- SwePesoSELECT t2.IndexID, t2.ColumnNameFROM @Table2 AS t2CROSS 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 |
|
|
abenitez77
Yak Posting Veteran
53 Posts |
Posted - 2014-09-19 : 17:30:41
|
Thanks, that worked like a charm |
|
|
|
|
|