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)
 join or replace??

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 WkComm
99408 Contract1 Live Op1 Op2 Op4 Op7 Op9 OP11 Op1 Op2 Op4 06/01/2014
99779 Contract2 Hold Op1 Op2 Op1 06/01/2014
99791 Contract3 Live Op6 Op7 Op9 Op6 Op7 Op9 06/01/2014
99967 Contract4 Live Op1 Op11 06/01/2014
99985 Contract5 Hold Op8 Op5 06/01/2014
99992 Contract6 Live Op5 Op13 06/01/2014

In a separate table I have the following values:

Names
Op1
Op2
Op3
Op4
Op5

What 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 WkComm
99408 Contract1 Live Op1 Op2 Op4 Op1 Op2 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


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...

--Initialize
DECLARE @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 CTE
WITH 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 @temptable
UNION ALL
SELECT
[Contract],
SUBSTRING(MonAMWork, 0, CHARINDEX(' ', MonAMWork, 0)),
SUBSTRING(MonAMWork, CHARINDEX(' ', MonAMWork, 0) + 1, LEN(MonAMWork) - CHARINDEX(' ', MonAMWork, 0))
FROM REGULARIZED_AM
WHERE 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 @temptable
UNION ALL
SELECT
[Contract],
SUBSTRING(MonPMWork, 0, CHARINDEX(' ', MonPMWork, 0)),
SUBSTRING(MonPMWork, CHARINDEX(' ', MonPMWork, 0) + 1, LEN(MonPMWork) - CHARINDEX(' ', MonPMWork, 0))
FROM REGULARIZED_PM
WHERE 0 < CHARINDEX(' ', MonPMWork, 0)
)
--Substitute 'FOR XML' for group_concat
SELECT
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.WkComm
FROM @temptable BASE


-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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 WkComm
99408 Contract1 Live Op1 Op2
Op4 Op7
Op9 Op11 Op1 Op2 Op4 06/01/2014

99779 Contract2 Hold Op1 Op2 Op1 06/01/2014

99791 Contract3 Live Op6 Op7
Op9 Op6 Op7
Op9 06/01/2014

99967 Contract4 Live Op1 Op11 06/01/2014
99985 Contract5 Hold Op8 Op5 06/01/2014
99992 Contract6 Live Op5 Op13 06/01/2014


Names
Op1
Op2
Op3
Op4
Op5


Contract PH Status MonAM MonPM WkComm
99408 Contract1 Live Op1 Op2 Op4 Op1 Op2 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

Go to Top of Page

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,


Names
Op1
Op2
Op3
Op4
Op5



Contract, 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,
Go to Top of Page
   

- Advertisement -