944,098 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 783
  • MS SQL RSS
Nov 11th, 2009
0

SQL

Expand Post »
Hi

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

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 1
Junior Poster
Tank50 is offline Offline
124 posts
since Aug 2008
Nov 15th, 2009
0
Re: SQL
I think you cannot use simple sql command to get result like that. You can use cursor to concatenate the sales quantity like below:
ms sql Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008
Nov 16th, 2009
0
Re: SQL
or use these commands:
ms sql Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: SQL SUM Decimals
Next Thread in MS SQL Forum Timeline: My Insert statement needs a trim





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC