Solved

Modify SQL Code for

Posted on 2011-02-11
3
198 Views
Last Modified: 2012-05-11
Hi all,

I'd like to modify the following code which creates a column for the multiple partners associated with each record. I would like to have the same for the Contact column. Some of these records may have multiple contacts and I would like them listed in a separate column.


USE [Encore]
GO
/****** Object:  StoredProcedure [dbo].[MCR]    Script Date: 02/11/2011 10:23:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[MCR]
AS
select tUser.FirstName +' '+ tUser.LastName as Partner, convert(varchar(100),tGenie.StartTime) AS StartTime, convert(varchar(100),tGenie.GenieNotes) AS GenieNotes, convert(varchar(100),tCompany.CompanyName) AS CompanyName, tPeople.FirstName +' '+ tPeople.LastName as Contact
into #tmp
from tGenie
LEFT JOIN tBusDev ON tGenie.BusDevGUID = tBusDev.GUID
LEFT JOIN tCompany ON tBusDev.CompanyGUID = tCompany.GUID
LEFT JOIN tBusDevPeople ON tGenie.BusDevGUID = tBusDevPeople.BusDevGUID
LEFT JOIN tPeople ON tBusDevPeople.PeopleGUID = tPeople.GUID
LEFT JOIN tBusDevUser ON tGenie.BusDevGUID = tBusDevUser.BusDevGUID
LEFT JOIN tUser ON tBusDevUser.UserGUID = tUser.GUID
WHERE tGenie.GenieTypeGUID = 'FB3E2527-4DFD-448B-A34D-9C3F737F4783' AND (StartTime > '2011-02-01 00:01:00.000' AND StartTime < '2011-02-11 00:01:00.000')

declare @sql nvarchar(max)  
declare @columns1 varchar(max)
declare @columns2 varchar(max)
declare @maxcount int

select @maxcount=MAX(c) from
(select COUNT(distinct partner) c
 from #tmp
 group by StartTime,GenieNotes,CompanyName,Contact) x

select
	@columns1 = coalesce(@columns1+',','') + '[' + CONVERT(varchar(10),number) + ']',
	@columns2 = coalesce(@columns2,'') + ',[' + CONVERT(varchar(10),number) + '] [Partner' + CONVERT(varchar(10),number) + ']'
from master..spt_values where type='p' and number between 1 and @maxcount

set @sql = '  
	SELECT StartTime, GenieNotes, CompanyName'+@columns2+', Contact
	FROM (SELECT *, RN=ROW_NUMBER() OVER (
		PARTITION BY StartTime, GenieNotes, CompanyName, Contact
		ORDER BY PARTNER)
		FROM (SELECT DISTINCT * FROM #TMP) A) B
	PIVOT (MAX([Partner]) FOR RN IN (' + @columns1 + ') ) AS pvt'  
EXEC (@sql);

Open in new window

0
Comment
Question by:PrimusPilus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 34872787
I did not get your question. Can you explain little bit more. What is the current output and what is the expected result?
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 34876826
try this
USE [Encore]
GO
/****** Object:  StoredProcedure [dbo].[MCR]    Script Date: 02/11/2011 10:23:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[MCR]
AS
select tUser.FirstName +' '+ tUser.LastName as Partner
, convert(varchar(100),tGenie.StartTime) AS StartTime
, convert(varchar(100),tGenie.GenieNotes) AS GenieNotes
, convert(varchar(100),tCompany.CompanyName) AS CompanyName
, tPeople.FirstName +' '+ tPeople.LastName as Contact
into #tmp
from tGenie
LEFT JOIN tBusDev ON tGenie.BusDevGUID = tBusDev.GUID
LEFT JOIN tCompany ON tBusDev.CompanyGUID = tCompany.GUID
LEFT JOIN tBusDevPeople ON tGenie.BusDevGUID = tBusDevPeople.BusDevGUID
LEFT JOIN tPeople ON tBusDevPeople.PeopleGUID = tPeople.GUID
LEFT JOIN tBusDevUser ON tGenie.BusDevGUID = tBusDevUser.BusDevGUID
LEFT JOIN tUser ON tBusDevUser.UserGUID = tUser.GUID
WHERE tGenie.GenieTypeGUID = 'FB3E2527-4DFD-448B-A34D-9C3F737F4783' 
AND (StartTime > '2011-02-01 00:01:00.000' 
AND StartTime < '2011-02-11 00:01:00.000')

declare @sql nvarchar(max)  
declare @columns1 varchar(max),@columns3 varchar(max)
declare @columns2 varchar(max),@columns4 varchar(max)
declare @maxcount int

select @maxcount=MAX(c) from
(select COUNT(distinct partner) c
 from #tmp
 group by StartTime,GenieNotes,CompanyName) x

select
	@columns1 = coalesce(@columns1+',','') + '[' + CONVERT(varchar(10),number) + ']',
	@columns2 = coalesce(@columns2,'') + ',a.[' + CONVERT(varchar(10),number) 
         + '] [Partner' + CONVERT(varchar(10),number) + ']'
from master..spt_values where type='p' and number between 1 and @maxcount

select @maxcount=MAX(c) from
(select COUNT(distinct Contact) c
 from #tmp
 group by StartTime,GenieNotes,CompanyName) x

select
	@columns3 = coalesce(@columns3+',','') + '[' + CONVERT(varchar(10),number) + ']',
	@columns4 = coalesce(@columns4,'') + ',b.[' + CONVERT(varchar(10),number) 
         + '] [Contact ' + CONVERT(varchar(10),number) + ']'
from master..spt_values where type='p' and number between 1 and @maxcount


set @sql = 'Select coalesce(a.starttime,b.starttime) as Starttime
                  ,coalesce(a.GenieNotes,b.GenieNotes) as GenieNotes
                  ,coalesce(a.CompanyName,b.CompanyName) as CompanyName'
           +@columns2+@columns4
        
           +' From (SELECT StartTime, GenieNotes, CompanyName,'+@columns1+'
	FROM (SELECT *, RN=ROW_NUMBER() OVER (
		PARTITION BY StartTime, GenieNotes, CompanyName
		ORDER BY PARTNER)
		FROM (SELECT DISTINCT * FROM #TMP) A) B
	PIVOT (MAX([Partner]) FOR RN IN (' + @columns1 + ') ) AS pvt) as A'  
         +' Full Outer Join '
         +' (SELECT StartTime, GenieNotes, CompanyName,'+@columns3+'
	FROM (SELECT *, RN=ROW_NUMBER() OVER (
		PARTITION BY StartTime, GenieNotes, CompanyName
		ORDER BY Contact)
		FROM (SELECT DISTINCT * FROM #TMP) A) B
	PIVOT (MAX([Contact]) FOR RN IN (' + @columns1 + ') ) AS pvt) as b'
        +' on a.starttime=b.starttime and a.genieNotes=b.Genienotes and '
        +' a.CompanyName=b.CompanyName '
        +' Order by 1,3'

Print @SQL  -- comment out when happy

EXEC (@sql);

Open in new window

0
 

Author Closing Comment

by:PrimusPilus
ID: 35336373
Thank you that worked!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question