Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 96
Reputation: Tank50 is an unknown quantity at this point 
Solved Threads: 1
Tank50's Avatar
Tank50 Tank50 is offline Offline
Junior Poster in Training

SQL

 
0
  #1
23 Days Ago
Hi

I need a help in SQL,There is table called sales.Its contains the day,sales quantity.If I write sql like below

  1. SELECT day,Sales_quantity FROM Sales.

Then result should be
Day SalesQuantity
Monday 20
Monday 302
Monday 50
Tuseday 30
etc..

The problem is I want to get result like below

Day SalesQuantity
Monday 20,302,50
Tuesday 30

There should be ',' sign between two values.How Do I write this kind of SQL.

Thanks
Tank50
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster
 
0
  #2
18 Days Ago
I think you cannot use simple sql command to get result like that. You can use cursor to concatenate the sales quantity like below:
  1. declare @Day varchar(255), @Sales int
  2.  
  3. CREATE TABLE #tmpSales ([Day] varchar(255), SalesQuantity int)
  4.  
  5. INSERT #tmpSales
  6. SELECT 'Monday', 20 union ALL
  7. SELECT 'Monday', 302 union ALL
  8. SELECT 'Monday', 50 union ALL
  9. SELECT 'Tuseday', 30
  10.  
  11. SELECT DISTINCT [Day], cast('' AS varchar(1024)) AS strSalesQuantity
  12. INTO #tmpResult
  13. FROM #tmpSales
  14.  
  15. declare cs_Day cursor forward_only FOR SELECT [Day] FROM #tmpResult
  16. open cs_Day
  17.  
  18. fetch next FROM cs_Day INTO @Day
  19. while @@fetch_status = 0
  20. begin
  21. declare cs_Sales cursor forward_only FOR SELECT SalesQuantity FROM #tmpSales where [Day] = @Day
  22. open cs_Sales
  23. fetch next FROM cs_Sales INTO @Sales
  24. while @@fetch_status = 0
  25. begin
  26. UPDATE #tmpResult set strSalesQuantity = strSalesQuantity + case when strSalesQuantity <> '' then ',' else '' end + cast(@Sales as varchar) where [Day] = @Day
  27. fetch next FROM cs_Sales INTO @Sales
  28. end
  29. close cs_Sales
  30. deallocate cs_Sales
  31.  
  32. fetch next FROM cs_Day INTO @Day
  33. end
  34.  
  35. close cs_Day
  36. deallocate cs_Day
  37. SELECT * FROM #tmpResult
  38.  
  39. DROP TABLE #tmpResult
  40. DROP TABLE #tmpSales
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster
 
0
  #3
18 Days Ago
or use these commands:
  1. declare @Day varchar(255), @Sales int, @strSales varchar(1024)
  2.  
  3. CREATE TABLE #tmpSales ([Day] varchar(255), SalesQuantity int)
  4. INSERT #tmpSales
  5. SELECT 'Monday', 20 union ALL
  6. SELECT 'Monday', 302 union ALL
  7. SELECT 'Monday', 50 union ALL
  8. SELECT 'Tuseday', 30
  9.  
  10. SELECT DISTINCT [Day], cast('' AS varchar(1024)) AS strSalesQuantity
  11. INTO #tmpResult
  12. FROM #tmpSales
  13.  
  14. declare cs_Day cursor forward_only FOR SELECT [Day] FROM #tmpResult
  15. open cs_Day
  16. fetch next FROM cs_Day INTO @Day
  17. while @@fetch_status = 0
  18. begin
  19. SELECT @strSales = ''
  20. SELECT @strSales = @strSales + case when @strSales <> '' then ',' else '' end + cast(SalesQuantity AS varchar)
  21. FROM #tmpSales
  22. WHERE [Day] = @Day
  23. UPDATE #tmpResult set strSalesQuantity = @strSales where [Day] = @Day
  24.  
  25. fetch next FROM cs_Day INTO @Day
  26. end
  27. close cs_Day
  28. deallocate cs_Day
  29.  
  30. SELECT * FROM #tmpResult
  31.  
  32. DROP TABLE #tmpResult
  33. DROP TABLE #tmpSales
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC