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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 passing column values to a single row

Author  Topic 

vgr.raju
Yak Posting Veteran

95 Posts

Posted - 2005-07-11 : 00:13:33
use northwind
go
declare @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 for
select customers.customerid,orders.orderid,[order details].productid from customers
join orders on customers.customerid=orders.customerid
join [order details] on [order details] .orderid=orders.orderid
order by customers.customerid,orders.customerid,[order details].orderid
open cursorcustomer
fetch next from cursorcustomer into @cnext,@onext,@pnext
set @c=@cnext
while @@fetch_status =0
begin
if @c<@cnext
begin
insert @table select @c,@o,@p
select @c=@cnext,@o=@onext
end
else
set @o=coalesce(@o+' : ',space(0))+@onext
set @p=@pnext
if @p<@pnext
begin
insert @table select @c,@o,@p
select @p=@pnext
end
else
set @p=coalesce(@p+' : ',space(0))+@pnext

fetch next from cursorcustomer into @cnext,@onext,@pnext
end
insert @table select @c,@o,@p
close cursorcustomer
deallocate cursorcustomer
select * from @table



I have written the above query.The results are given below:
ALFKI 10643 : 10643 : 10643 : 10692 : 10702 : 10702 : 10835 : 10835 : 10952 : 10952 : 11011 : 11011 71 : 71
ANATR 10308 : 10308 : 10625 : 10625 : 10625 : 10759 : 10926 : 10926 : 10926 : 10926 72 : 72



It gives the customername,orderid,and productid.
I would like to get distinct orderid and and their respective product id .

for example:
customerid orderid list productidlist
I 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
Go to Top of Page

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?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 northwind
go
declare @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 for
select customers.customerid,orders.orderid,[order details].productid from customers
join orders on customers.customerid=orders.customerid
join [order details] on [order details] .orderid=orders.orderid
order by customers.customerid,orders.customerid,[order details].orderid
open cursorcustomer
fetch next from cursorcustomer into @cnext,@onext,@pnext
set @c=@cnext
while @@fetch_status =0
begin
if @c<@cnext
begin
insert @table select @c,@o,@p
select @c=@cnext,@o=@onext
end
else
set @o=coalesce(@o+' : ',space(0))+@onext
set @p=@pnext
if @p<@pnext
begin
insert @table select @c,@o,@p
select @p=@pnext
end
else
set @p=coalesce(@p+' : ',space(0))+@pnext

fetch next from cursorcustomer into @cnext,@onext,@pnext
end
insert @table select @c,@o,@p
close cursorcustomer
deallocate cursorcustomer
select * from @table



I have written the above query.The results are given below:
ALFKI 10643 : 10643 : 10643 : 10692 : 10702 : 10702 : 10835 : 10835 : 10952 : 10952 : 11011 : 11011 71 : 71
ANATR 10308 : 10308 : 10625 : 10625 : 10625 : 10759 : 10926 : 10926 : 10926 : 10926 72 : 72



It gives the customername,orderid,and productid.
I would like to get distinct orderid and and their respective product id .

for example:
customerid orderid list productidlist
I want only three columns .
If somebody can help me how to get the desired results using cursor,i would be really greatful.



Go to Top of Page
   

- Advertisement -