SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Eliminate duplicate ID by concatenating string values from rows, where all columns except first one...


Eliminate duplicate ID by concatenating string values from rows, where all columns except first one are dynamic

Author
Message
naik.jo
naik.jo
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 107
I have a table as follow-:

TranID Field1 Field2
1 f2
1 f1
2 2f2
2 2f1

where field1, field2 are dynamic columns of type varchar
There can be any number of columns Field1, Field2, Field3 so on

My problem is, I want to remove repeated transactionid and show only once as below-:

TranID Field1 Field2
1 f1 f2
2 2f1 2f2

Can anybody help me out on this as I am unable to figure out a way to handle this in dynamic columns.

Thanks in advance.
J-F Bergeron
J-F Bergeron
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1255 Visits: 2707
Look up "FOR XML" in BOL, it will help you concatenate those strings together, based on the TranID.

Hope that helps,

Cheers,

J-F
naik.jo
naik.jo
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 107
Thanks J-F.

I think this approach can solve my problem listed earlier.

But I have come across another problem. When I try excuting following query on PUB database its showing me only half output. I can't see all rows converted to xml. M I misssing anything?

Query-:
SELECT store.stor_id as Id, stor_name as Name, sale.ord_num as OrderNo,
sale.qty as Qty
FROM stores store inner join sales sale on store.stor_id = sale.stor_id
ORDER BY stor_name
FOR XML AUTO

Output-:

I think editor will not show xml output I pasted under output.


Could you please suggest here.
J-F Bergeron
J-F Bergeron
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1255 Visits: 2707
Well actually, since you are on SQL server 2000, the solution will not work, it would have implied you can use for xml path(''), and it appeared in sql 2005...

Really sorry for the mislead.

Cheers,

J-F
Dave Ballantyne
Dave Ballantyne
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3880 Visits: 8370
This is all you need to know about concatenating strings

http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/



Clear Sky SQL
My Blog
naik.jo
naik.jo
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 107
I am facing few problems as all the columns except one are dynamic. But I am working on that.

Thanks a lot.
naik.jo
naik.jo
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 107
Hi,

Above mentioned query is given expected output in sql 2000 too.

I modified few settings in query analyzer in order to increase the maximum characters per column for result set and it worked.

But it’s not working for dynamic column query of my application.
I am trying to make it work.

Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search