Hi,
I am trying to return some data from one table. I would like to see every thing grouped by Customer Number, on one row,so even though a customer may have mulitiple products, these will appear beside each other in different columns rather then in different rows.
I have been trying to concat the data so that i can show it row by row but without sucess of getting the sturcture i need.
I would like to see the date like this;
Cus No, Prod 1, Price1, New Produc, New Price1. Prodcut2, Price2, New Product 2,New Price2, Address....
A customer could have one or 20 products on their account.
So i really want to see what the existing prod a customer has against the new one i have against them , and the same with the price.
Can any one advise me please.
This is what i have tried so far, but it gives me all my products in one field seperated by commas, and when i try to get the value for the price, its just Summing rather then giving me individual prices.
Please let me know if you need anymore information.
Thanks,
putoch
CREATE TABLE [dbo].[temp_post]( [ServiceId] [float] NULL, [CustomerNumber] [float] NULL, [Noofoccurances] [float] NULL, [ExistingProduct] [nvarchar](255) NULL, [ExistingMonthlyFee] [money] NULL, [NewProduct] [nvarchar](255) NULL, [NewMonthlyFee] [money] NULL, [ServiceAddress1] [nvarchar](255) NULL ) ON [PRIMARY]insert into temp_post values (22182,100015,4,'Breeze 4MB Lite 24:1',70,'Breeze 6Mb 24:1'80,SomeAddress)insert into temp_post values (22184,100015,4,'Breeze 4MB 12:1',135,'Breeze 6Mb Plus 12:1',150,SomeAddress)insert into temp_post values (48265,100015,4,'Breeze 4MB 12:1',135,'Breeze 6Mb Plus 12:1',150,SomeAddress)insert into temp_post values (88935,100015,4,'Breeze 4MB 12:1',135,'Breeze 6Mb Plus 12:1',150,SomeAddress)--I've tried using this code but its not giving me what i really wantWITH Ranked ( CustomerNumber2, rnk, ExistingProduct ,NewProduct , ExistingMonthlyFee ) AS ( SELECT CustomerNumber2, ROW_NUMBER() OVER( PARTITION BY CustomerNumber2 ORDER BY CustomerNumber2 ), CAST( ExistingProduct AS VARCHAR(8000) ), ExistingMonthlyFee, CAST ( NewProduct AS VARCHAR (8000)) FROM temp_post), AnchorRanked ( CustomerNumber2, rnk, ExistingProduct,ExistingMonthlyFee,NewProduct ) AS ( SELECT CustomerNumber2, rnk, ExistingProduct, ExistingMonthlyFee,NewProduct FROM Ranked WHERE rnk = 1 ),RecurRanked ( CustomerNumber2, rnk, ExistingProduct, ExistingMonthlyFee, NewProduct ) AS ( SELECT CustomerNumber2, rnk, ExistingProduct, ExistingMonthlyFee,NewProduct FROM AnchorRanked UNION ALL SELECT Ranked.CustomerNumber2, Ranked.rnk, RecurRanked.ExistingProduct + ', ' + Ranked.ExistingProduct, RecurRanked.ExistingMonthlyFee +', '+ Ranked.ExistingMonthlyFee, RecurRanked.NewProduct + ', '+ Ranked.NewProduct FROM Ranked INNER JOIN RecurRanked ON Ranked.CustomerNumber2 = RecurRanked.CustomerNumber2 AND Ranked.rnk = RecurRanked.rnk + 1 )SELECT CustomerNumber2, MAX( ExistingProduct ),MAX(ExistingMonthlyFee) FROM RecurRanked GROUP BY CustomerNumber2;
Can you convert the pricing to varchar prior to concatenating it? Does that help?
Cheers
David
Putoch
ASKER
Yea i've done that , actually , i've set up a view now and then run some concatation against that. (i may have some extra colums of data in the snipit below)
This brings back the Customer Number, then the Old Products and Prices together, then the New Procds and Prices together, and then the Contact deteails. If i copy to Excel and copy to text i can dilminate these out into seperate columns, however i then have to go and copy past the OldProduct 1, Old Price1 Beside the New Product 1 and New Price 1 and so on.. i was hoping there would be a clever way to do this in SQL without manually having to copy and paste.
ANy ideas?
Thanks,
Putoch
CREATE view vemail as SELECT convert( varchar(10),serviceid)serviceid,customernumber,CustomerNumber2, ExistingProduct, NewProduct, convert(varchar(10),ExistingMonthlyFee)existingMonthlyfee, convert (varchar(10),NewMonthlyFee)NewMonthlyfee, emailaddress,companyname,customername,serviceaddress1,serviceaddress2,serviceaddress3,county1, ROW_NUMBER() OVER (PARTITION BY CustomerNumber2 ORDER BY CustomerNumber2 DESC)AS RANKING FROM temp_post AS tp ;WITH Orders(Id)AS ( SELECT DISTINCT customernumber2 FROM vpost)SELECT Id, ( SELECT serviceid + ' || ' + ExistingProduct + ' || ' + ExistingMonthlyFee +' || ' FROM vpost WHERE customernumber2 = Id ORDER BY customernumber2, Ranking FOR XML PATH('') ) AS ExistingProd, ( SELECT NewProduct + ' || ' + NewMonthlyfee +' || ' FROM vpost WHERE customernumber2 = Id ORDER BY customernumber2, Ranking FOR XML PATH('') ) AS NewProd,( SELECT companyname + ' | '+ customername + ' | ' + serviceaddress1 + ' | ' + serviceaddress2 +' | ' + serviceaddress3 +' | '+ county1 +' || ' FROM Vpost Where customernumber2 = Id ORDER BY customernumber2,Ranking FOR XML PATH('') ) AS ContactDetailsFROM Orders
Can you convert the pricing to varchar prior to concatenating it? Does that help?
Cheers
David