sql - How to order by clause with distinct -


i have table in sql server in have values. example below

transaction   productno ------------- ------------- 2001-01-01          1 2001-01-01          3 2001-01-01          4 2001-01-02          2 2001-01-02          3 2001-01-02          5 

i have written query below

select distinct trans.[transaction],  stuff(( select ',' + trans1.productno  [text()] transactions trans1  trans.[transaction]=trans1.[transaction] order trans1.[transaction] xml path('')), 1,1,'')[productno]  transactions trans 

output below:

transaction   productno ------------- ------------- 2001-01-01        1,3,4 2001-01-02        2,3,5 

but getting below exception

conversion failed when converting varchar value ',' data type int. 

',' + trans1.productno - here problem. sql server thinks trying add comma number. try changing trans1.productnoto cast(trans1.productno varchar).


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -