Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenation or pivot in SQL

Status
Not open for further replies.

tmcrouse

Programmer
Dec 21, 2011
39
US
I deleted my old post and reposting since I did not make myself clear what I was looking to do.

I have a 4800 approx. row database in SQL Server. I need to either take that data and in SQL Server perform SQL code to be able to pivot the data. An example of some pivot code is:
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;

I have attempted to apply this methodology but I have too many rows and not sure quite how to get what I need done. So my option number 2 was to try concatenation in Excel.

I thought I could do an extract of the raw data into Excel and then some sort of concatenation formula to say if Column A is the same concatenate all the other columns associated. So if Column A2 through A3 is identical concatenate anything in B2 through X3. Everything varies. I attached an example of an extract from the database that is the data have and then below is an example of the need. I don't want to have to do an extract and manually do all this. I am sure there is a way just not sure of that way and what the right process or path is to take.
 
 http://files.engineering.com/getfile.aspx?folder=a5dff4f2-7a42-4d06-b480-6354de43924b&file=example.xlsx
Hi, tmcrouse (Programmer)

Good news. You can program a solution in Excel VBA.

I'd suggest asking for help in forum707.

However, I have a question regarding

Access2Care | Simply Health | Call Center, Claims, Credentialing, Member Outreach, Provider Outreach, Network,

...however, column B for Access2Care has either Simply Health or NOTHING. The column C values for Simply Health only has 4 values, not 5 (not Network).


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Thanks, I actually figured out a way to do it with separate pivots in sql server. I opted for this solution because this is going to change time and time again the more the folks that update sharepoint add data and I have to align as well as make what they change in sharepoint look right in the backend database. Because in the end sharepoint they want 1 row per delegatename but in backend it is distinct across many things. so this is part of what I did


/*now work with the multiple delegatesfunction to find out how many delegatesfunction are per delegatesname do a count*/
select distinct delegatesname, delegatesfunction into deletest from deleb where msoname is not null group by DelegatesName, delegatesfunction;
select distinct delegatesname, COUNT(delegatesfunction) as a from deletest group by DelegatesName order by a desc;/*the most right now is 10*/

select distinct delegatesname, delegatesfunction1, delegatesfunction2, delegatesfunction3, delegatesfunction4, delegatesfunction5, delegatesfunction6, delegatesfunction7, delegatesfunction8,
delegatesfunction9, delegatesfunction10 into deled/ *holds the delegatesfunction retain until last final query*/
from (select delegatesname, delegatesfunction, 'delegatesfunction' + CAST(row_number() over(partition by delegatesname order by delegatesname) as varchar(50))columnsequence
from deletest
) temp pivot(max(delegatesfunction)
for columnsequence in (delegatesfunction1, delegatesfunction2, delegatesfunction3, delegatesfunction4, delegatesfunction5, delegatesfunction6, delegatesfunction7, delegatesfunction8,
delegatesfunction9, delegatesfunction10)) piv;

drop table deletest;
 
This assumes a max of 10 rows of column C values.

Might just BREAK if that row count is ever exceeded.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top