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.productno
to cast(trans1.productno varchar)
.
Comments
Post a Comment