March 1, 2017 at 1:02 pm
I have a cross apply query I have performed on my table that has data that looks like this:
Name Function Line State Contact Address
IYE CR COM NY Sarah 111 Jones Str
IYE DM MCR CA Sarah 111 Jones Str
IYE UM MCD FL Sarah 111 Jones Str
I did my cross apply query and because the function, line, and state are char it ends up looking like this
Name Function Line State Contact Address
IYE CR , DM , UM , COM , MCR , MCD , NY , CA , FL , Sarah 111 Jones Str
I am trying to make it look like:
Name Function Line State Contact Address
IYE CR, DM, UM COM, MCR, MCD NY, CA, FL Sarah 111 Jones Str
I have tried substr ltrim rtrim and nothing seems to work. I have tried incorporating the 3 in my original cross apply and then after the fact when I put the cross apply into another SQL table. I have over all 7910 rows and with the cross apply end up down to 698 rows making the Name distinct and then cross applying everything else
March 1, 2017 at 1:08 pm
We can't tell you what is wrong with your query if you don't provide your query. You should also post data in a CONSUMABLE format. That is, post a script that will insert your data into a table (preferably a temp table or table variable) as outlined in the first link in my signature.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
March 1, 2017 at 1:26 pm
Apart from a few commas here and there, your two rows of results look the same. Try to imagine yourself looking at your post for the first time – it's very difficult to offer any meaningful advice, based on what you've given us. Were you expecting us to somehow fix your absent query?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
March 1, 2017 at 4:09 pm
I figured it out. Now just need to code for the trailing comma. I thought the information would come over without making it look like it does above. However it did not so I will attach the file with add to see the before, during, after and then also post my code to help others.
select distinct DelegatesName, MSOName, delegatesfunctions as DelegatesFunction, lobabbrevs as LOBAbbrev, markets as Market, ContactNames as ContractName, addressline1s as AddressLine1, AddressLine2s as AddressLine2,
Citys as City, States as State, ZipCodes as ZipCode, Phones as Phone, ContactPhoneExts as ContactPhoneExt, ContactEmails as ContactEmail, DelegatesServiceDescs as DelegateServiceDesc, Onsite, VendorAuditLocations as VendorAuditLocation,
ICEAffiliation, CurrentStatuss as CurrentStatus, accreditationmanagernames as AccreditationManagerName, Fax, DelegatesWebsite, Origins as Origin, SubDelegateNames as SubDelegateName,
DelegatePhysicalAddress1s as DelegatePhysicalAddress, DelegatePhysicalAddress2s as DelegatePhysicalAddress2, DelegatePhysicalCitys as DelegatePhysicalCity, delegatephysicalzipcodes as DelegatePhysicalZipCode,
DelegatePhone2, accreditationheldncqas as AccreditationHeldNCQA, DoestheDelegateSubDelegates as DoestheDelegatesSubDelegate, CurrentAuditStatus, ContractEffDate, contracttermdate
into anthemq.dbo.DelegatesReport
from anthemq.dbo.delegates d1
cross apply ( select distinct
left(delegatesfunction, len(delegatesfunction)-0) + ', '
from anthemq.dbo.delegates d2
where d1.delegatesname = d2.delegatesname
for XML path ('')) as t2 (delegatesfunctions)
cross apply (select distinct
left(lobabbrev, LEN(lobabbrev)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t3 (lobabbrevs)
cross apply (select distinct
left(market, len(market)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t4 (markets)
cross apply (select distinct
left(contactname, len(contactname)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t5 (contactnames)
cross apply (select distinct
left(addressline1, len(addressline1)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t7 (addressline1s)
cross apply (select distinct
left(addressline2, len(addressline1)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t8 (addressline2s)
cross apply (select distinct
left(city, len(city)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t9 (citys)
cross apply (select distinct
left(state, len(state)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t10 (states)
cross apply (select distinct
LEFT(zipcode, len(zipcode)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t11 (zipcodes)
cross apply (select distinct
left(phone, len(phone)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t12 (phones)
cross apply (select distinct
left(contactphoneext, len(contactphoneext)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t13 (contactphoneexts)
cross apply (select distinct
left(contactemail, len(contactemail)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t14 (contactemails)
cross apply (select distinct
left(currentstatus, len(currentstatus)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t15 (currentstatuss)
cross apply (select distinct
left(delegatesservicedesc, len(delegatesservicedesc)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t16 (delegatesservicedescs)
cross apply (select distinct
left(subdelegatename, LEN(subdelegatename)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t17 (subdelegatenames)
cross apply (select distinct
left(accreditationmanagername, len(accreditationmanagername)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t18 (accreditationmanagernames)
cross apply (select distinct
left(vendorauditlocation, len(vendorauditlocation)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t19 (vendorauditlocations)
cross apply (select distinct
left(origin, len(origin)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t20 (origins)
cross apply (select distinct
left(delegatephysicaladdress1, len(delegatephysicaladdress1)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t21 (delegatephysicaladdress1s)
cross apply (select distinct
left(delegatephysicaladdress2, len(delegatephysicaladdress2)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t22 (delegatephysicaladdress2s)
cross apply (select distinct
left(delegatephysicalcity, len(delegatephysicalcity)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t23 (delegatephysicalcitys)
cross apply (select distinct
LEFT(delegatephysicalzipcode, len(delegatephysicalzipcode)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t24 (delegatephysicalzipcodes)
cross apply (select distinct
left(accreditationheldncqa, len(accreditationheldncqa)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t25 (accreditationheldncqas)
cross apply (select distinct
left(doesthedelegatesubdelegate, len(doesthedelegatesubdelegate)-0) + ', '
from anthemq.dbo.Delegates d2
where d1.DelegatesName = d2.DelegatesName
for XML path ('')) as t26 (doesthedelegatesubdelegates) order by delegatesname asc;
March 2, 2017 at 8:51 am
First, I said you should post CONSUMABLE data. An Excel file is not consumable. You should reread the first link in my signature. Furthermore, people are hesitant to open Excel files they find on the web.
Second, it's far easier to deal with leading commas rather than trailing commas, because you know the leading comma will always be at position 1. That's why the common XML concatenation method uses leading commas rather than trailing commas.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply