Author |
Topic |
Kerryman
Starting Member
17 Posts |
Posted - 2014-01-09 : 12:52:55
|
Hi Guys, I was hoping that somebody could help me out with the following problem, my sql knowledge is limited at best. I have a temp table which produces the following output:Contract PH Status MonAM MonPM WkComm99408 Contract1 Live Op1 Op2 Op4 Op7 Op9 OP11 Op1 Op2 Op4 06/01/201499779 Contract2 Hold Op1 Op2 Op1 06/01/201499791 Contract3 Live Op6 Op7 Op9 Op6 Op7 Op9 06/01/201499967 Contract4 Live Op1 Op11 06/01/201499985 Contract5 Hold Op8 Op5 06/01/201499992 Contract6 Live Op5 Op13 06/01/2014In a separate table I have the following values:NamesOp1Op2Op3Op4Op5What I'd like to do by either a join or a replace or whatever is to produce the following output. In other words only Ops shown in the Names table are allowed into the final table. Contract PH Status MonAM MonPM WkComm99408 Contract1 Live Op1 Op2 Op4 Op1 Op2 Op4 06/01/201499779 Contract2 Hold Op1 Op2 Op1 06/01/201499791 Contract3 Live 06/01/201499967 Contract4 Live Op1 06/01/201499985 Contract5 Hold Op5 06/01/201499992 Contract6 Live Op5 06/01/2014 |
|
nagino
Yak Posting Veteran
75 Posts |
Posted - 2014-01-10 : 00:20:02
|
Recommendation is that use the temp table regularized.The following, forcibly reqularized from temp table, is maybe work however without sophistication...--InitializeDECLARE @temptable TABLE( [Contract] int, PH varchar(20), [Status] varchar(20), MonAM varchar(200), MonPM varchar(200), WkComm date);INSERT INTO @temptable VALUES (99408, 'Contract1', 'Live', 'Op1 Op2 Op4 Op7 Op11', 'Op1 Op2 Op4', '2014-01-06'), (99779, 'Contract2', 'Hold', 'Op1 Op2', 'Op1', '2014-01-06'), (99791, 'Contract3', 'Live', 'Op6 Op7 Op9', 'Op6 Op7 Op9', '2014-01-06'), (99967, 'Contract4', 'Live', 'Op1', 'Op11', '2014-01-06'), (99985, 'Contract5', 'Hold', 'Op8', 'Op5', '2014-01-06'), (99992, 'Contract6', 'Live', 'Op5', 'Op13', '2014-01-06');DECLARE @condition TABLE( Names varchar(20));INSERT INTO @condition VALUES ('Op1'), ('Op2'), ('Op3'), ('Op4'), ('Op5');--Regularize in CTEWITH REGULARIZED_AM([Contract], MonAM, MonAMWork) AS (SELECT [Contract], SUBSTRING(MonAM, 0, CHARINDEX(' ', MonAM + ' TERMINATER', 0)), CONVERT(varchar(200), SUBSTRING(MonAM, CHARINDEX(' ', MonAM, 0) + 1, LEN(MonAM) - CHARINDEX(' ', MonAM, 0)) + CASE WHEN 0 < CHARINDEX(' ', MonAM, 0) THEN ' TERMINATER' END)FROM @temptableUNION ALLSELECT [Contract], SUBSTRING(MonAMWork, 0, CHARINDEX(' ', MonAMWork, 0)), SUBSTRING(MonAMWork, CHARINDEX(' ', MonAMWork, 0) + 1, LEN(MonAMWork) - CHARINDEX(' ', MonAMWork, 0))FROM REGULARIZED_AMWHERE 0 < CHARINDEX(' ', MonAMWork, 0)),REGULARIZED_PM([Contract], MonPM, MonPMWork) AS (SELECT [Contract], SUBSTRING(MonPM, 0, CHARINDEX(' ', MonPM + ' TERMINATER', 0)), CONVERT(varchar(200), SUBSTRING(MonPM, CHARINDEX(' ', MonPM, 0) + 1, LEN(MonPM) - CHARINDEX(' ', MonPM, 0)) + CASE WHEN 0 < CHARINDEX(' ', MonPM, 0) THEN ' TERMINATER' END)FROM @temptableUNION ALLSELECT [Contract], SUBSTRING(MonPMWork, 0, CHARINDEX(' ', MonPMWork, 0)), SUBSTRING(MonPMWork, CHARINDEX(' ', MonPMWork, 0) + 1, LEN(MonPMWork) - CHARINDEX(' ', MonPMWork, 0))FROM REGULARIZED_PMWHERE 0 < CHARINDEX(' ', MonPMWork, 0))--Substitute 'FOR XML' for group_concatSELECT BASE.Contract, BASE.PH, BASE.Status, ISNULL(( SELECT MonAM + ' ' FROM REGULARIZED_AM WHERE EXISTS( SELECT * FROM @condition WHERE REGULARIZED_AM.MonAM = Names) AND BASE.[Contract] = REGULARIZED_AM.[Contract] FOR XML PATH('') ), '') AS MonAM, ISNULL(( SELECT MonPM + ' ' FROM REGULARIZED_PM WHERE EXISTS( SELECT * FROM @condition WHERE REGULARIZED_PM.MonPM = Names) AND BASE.[Contract] = REGULARIZED_PM.[Contract] FOR XML PATH('') ), '') AS MonPM, BASE.WkCommFROM @temptable BASE -------------------------------------From JapanSorry, my English ability is limited. |
|
|
Kerryman
Starting Member
17 Posts |
Posted - 2014-01-10 : 06:51:14
|
Thank you nagino, I'll test your solution out and let you know. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-10 : 06:59:28
|
it would be much better if you can format your sample data. I cant really make out which values belong to which column in your posted sampkle------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Kerryman
Starting Member
17 Posts |
Posted - 2014-01-10 : 10:39:19
|
Hi Visakh16, I agree, it didn't quite look the same posted as it did in XL! Hope this format demonstrates the problem, I've no idea how to get table formatting to copy here. Nagino, thank you again for your solution, I've extended it through a 7 day week and although I can get it to work with the sample data, Op1, Op2 etc I'm can't as yet get it working with the actual operative name data.Contract PH Status MonAM MonPM WkComm99408 Contract1 Live Op1 Op2 Op4 Op7 Op9 Op11 Op1 Op2 Op4 06/01/201499779 Contract2 Hold Op1 Op2 Op1 06/01/201499791 Contract3 Live Op6 Op7 Op9 Op6 Op7 Op9 06/01/201499967 Contract4 Live Op1 Op11 06/01/201499985 Contract5 Hold Op8 Op5 06/01/201499992 Contract6 Live Op5 Op13 06/01/2014NamesOp1Op2Op3Op4Op5Contract PH Status MonAM MonPM WkComm99408 Contract1 Live Op1 Op2 Op4 Op1 Op2 Op4 06/01/201499779 Contract2 Hold Op1 Op2 Op1 06/01/201499791 Contract3 Live 06/01/201499967 Contract4 Live Op1 06/01/201499985 Contract5 Hold Op5 06/01/201499992 Contract6 Live Op5 06/01/2014 |
|
|
Kerryman
Starting Member
17 Posts |
Posted - 2014-01-10 : 11:05:31
|
Comma separated which may help?Contract PH, Status, MonAM, MonPM, WkComm,99408, Contract1, Live, Op1 Op2 Op1 Op2 06/01/2014, Op4 Op7 Op4, Op9 OP11, 99779, Contract2, Hold, Op1 Op2, Op1, 06/01/2014,99791, Contract3, Live, Op6 Op7 Op6 Op7, 06/01/2014, Op9, Op9 99967, Contract4, Live, Op1, Op11, 06/01/2014,99985, Contract5, Hold, Op8, Op5, 06/01/2014,99992, Contract6, Live, Op5, Op13, 06/01/2014,NamesOp1Op2Op3Op4Op5Contract, PH, Status, MonAM, MonPM, WkComm,99408, Contract1, Live, Op1 Op2 Op1 Op2 Op4, Op4 , 06/01/2014,99779, Contract2, Hold, Op1 Op2, Op1, 06/01/2014,99791, Contract3, Live, , , 06/01/2014,99967, Contract4, Live, Op1, , 06/01/2014,99985, Contract5, Hold, , Op5, 06/01/2014,99992, Contract6, Live, Op5, , 06/01/2014, |
|
|
|
|
|