May 11, 2010 at 2:29 pm
I'm trying to take some relational data and export it into an Excel Spreadsheet but I'm having issues getting the child data from rows into columns.
Example:
I have a table called accounts that links to csi_acct_leadsource joining on accountid.
select * from accounts a
inner join csi_acct_leadsource c
on a.accountid = c.accountid
I need to take the 1:many lead sources per account and return those as columns in my query so it will look like account, leadsource1, leadsource2, leadsource4, etc. I will also need to return the sourcedate for each as well.
It's currently stored in the table like this:
csi_acct_leadsource
accountid
leadsourceid
leadsourcedate
The description of the leadsource is actually stored in another table called leadsource that looks like
leadsourceid
leadsourcedesc
So I have to also join to leadsource to get the actual description that will be used in the column leadsource1, leadsource2, etc
It would be something like this:
select a.account, c.leaddate, l.description
from sysdba.account a
inner join sysdba.csi_acct_leadsource c
on a.accountid = c.accountid
inner join sysdba.leadsource l
on l.leadsourceid = c.leadsourceid
order by account
I need to figure out a way to take all these detail records and display them as columns. I've tried self joins and it only really works for the first record.
Any helpful hints would be greatly appreciated.
Thanks!
Rebecca
May 11, 2010 at 3:55 pm
Rebecca
I need to take the 1:many lead sources per account and return those as columns in my query so it will look like account, leadsource1, leadsource2, leadsource4, etc.
I suggest you read this article on SSC by Jeff Moden:
[http://www.sqlservercentral.com/articles/T-SQL/63681//url]
And then if you would like further assistance, post table definitions, sample data (non sensitive - but representative), and what you code you have attempted. In this manner I am sure you will receive, assistance which has been tested.
May 11, 2010 at 4:07 pm
Thanks for the link. I actually did read it earlier but will go thru it again. The version of SQL Server we are on does not support the PIVOT command so I wasn't able to try it.
I'll give it a shot again.
May 11, 2010 at 4:27 pm
From Jeff's article:
Last but not least, I currently only have SQL Server 2000 and 2005 installed. I indicate which rev each section of code will run on in parenthesis
Emphasis added by this poster
May 11, 2010 at 7:18 pm
bitbucket-25253 (5/11/2010)
From Jeff's article:Last but not least, I currently only have SQL Server 2000 and 2005 installed. I indicate which rev each section of code will run on in parenthesis
Emphasis added by this poster
You know what? I never noticed before but those particular notations in the code didn't show up for some reason. Looks like I need to do a little editing.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 11, 2010 at 7:19 pm
rfranklin-741429 (5/11/2010)
Thanks for the link. I actually did read it earlier but will go thru it again. The version of SQL Server we are on does not support the PIVOT command so I wasn't able to try it.I'll give it a shot again.
If that's the case, don't use the PIVOT version... use the CROSS TAB version of the code which will work on many flavors of SQL, not just SQL Server.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply