Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vgr.raju
Yak Posting Veteran
95 Posts |
Posted - 2005-07-11 : 00:13:33
|
| use northwindgodeclare @table table(customerid varchar(1000),orderlist varchar(1000),productdetails varchar(1000))declare @c varchar(1000),@cnext varchar(1000),@o varchar(1000),@onext varchar(1000)declare @p varchar(1000),@pnext varchar(1000)declare cursorcustomer cursor forselect customers.customerid,orders.orderid,[order details].productid from customersjoin orders on customers.customerid=orders.customeridjoin [order details] on [order details] .orderid=orders.orderidorder by customers.customerid,orders.customerid,[order details].orderidopen cursorcustomerfetch next from cursorcustomer into @cnext,@onext,@pnextset @c=@cnextwhile @@fetch_status =0begin if @c<@cnext begin insert @table select @c,@o,@p select @c=@cnext,@o=@onext end elseset @o=coalesce(@o+' : ',space(0))+@onextset @p=@pnext if @p<@pnext begin insert @table select @c,@o,@p select @p=@pnext end elseset @p=coalesce(@p+' : ',space(0))+@pnext fetch next from cursorcustomer into @cnext,@onext,@pnextend insert @table select @c,@o,@pclose cursorcustomerdeallocate cursorcustomerselect * from @tableI have written the above query.The results are given below:ALFKI 10643 : 10643 : 10643 : 10692 : 10702 : 10702 : 10835 : 10835 : 10952 : 10952 : 11011 : 11011 71 : 71ANATR 10308 : 10308 : 10625 : 10625 : 10625 : 10759 : 10926 : 10926 : 10926 : 10926 72 : 72It gives the customername,orderid,and productid.I would like to get distinct orderid and and their respective product id .for example:customerid orderid list productidlistI want only three columns .If somebody can help me how to get the desired results using cursor,i would be really greatful. |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2005-07-11 : 01:01:24
|
| Can you post some sample data and an example of the result you're looking for? Seems you're looking for a complicated solution to a simple problem (on first reading, anyway....)Tim |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-11 : 01:26:46
|
| How about storing distinct customerids in the table and use coalesce based on that?MadhivananFailing to plan is Planning to fail |
 |
|
|
vgr.raju
Yak Posting Veteran
95 Posts |
Posted - 2005-07-11 : 01:28:22
|
Hello, Thank you for your reply.Primarily i am working with northwind database.The tables involved are customers,orders,orderdetails.Thank you.quote: Originally posted by vgr.raju use northwindgodeclare @table table(customerid varchar(1000),orderlist varchar(1000),productdetails varchar(1000))declare @c varchar(1000),@cnext varchar(1000),@o varchar(1000),@onext varchar(1000)declare @p varchar(1000),@pnext varchar(1000)declare cursorcustomer cursor forselect customers.customerid,orders.orderid,[order details].productid from customersjoin orders on customers.customerid=orders.customeridjoin [order details] on [order details] .orderid=orders.orderidorder by customers.customerid,orders.customerid,[order details].orderidopen cursorcustomerfetch next from cursorcustomer into @cnext,@onext,@pnextset @c=@cnextwhile @@fetch_status =0begin if @c<@cnext begin insert @table select @c,@o,@p select @c=@cnext,@o=@onext end elseset @o=coalesce(@o+' : ',space(0))+@onextset @p=@pnext if @p<@pnext begin insert @table select @c,@o,@p select @p=@pnext end elseset @p=coalesce(@p+' : ',space(0))+@pnext fetch next from cursorcustomer into @cnext,@onext,@pnextend insert @table select @c,@o,@pclose cursorcustomerdeallocate cursorcustomerselect * from @tableI have written the above query.The results are given below:ALFKI 10643 : 10643 : 10643 : 10692 : 10702 : 10702 : 10835 : 10835 : 10952 : 10952 : 11011 : 11011 71 : 71ANATR 10308 : 10308 : 10625 : 10625 : 10625 : 10759 : 10926 : 10926 : 10926 : 10926 72 : 72It gives the customername,orderid,and productid.I would like to get distinct orderid and and their respective product id .for example:customerid orderid list productidlistI want only three columns .If somebody can help me how to get the desired results using cursor,i would be really greatful.
|
 |
|
|
|
|
|
|
|