|
Stevan23
Starting Member
15 Posts |
Posted - 2012-07-04 : 21:12:47
|
| Hi all,I'm fairly new to using SQL and I'm using SQL Server 2005 and I've been trying to concatenate quantities and to group them by their item code and name eventually, but I'm running into a problem. I've tried google and have tried to search for the answer in a few forums but I don't think any of them have been specific enough, so I thought I'd ask here.Here's the UDF code:ALTER FUNCTION [dbo].[concat_boxes_produced] ( -- Add the parameters for the function here @ItemCode nvarchar(20))RETURNS nvarchar(20)ASBEGIN -- Declare the return variable here DECLARE @str nvarchar(200) -- Add the T-SQL statements to compute the return value here SELECT @str = coalesce(@str + ', ', '') + cast(t0.CmpltQty As nvarchar(53)) FROM dbo.OWOR t0INNER JOIN dbo.OITM t1 ON t0.ItemCode = t1.ItemCodeINNER JOIN dbo.WOR1 t2 ON t0.DocEntry = t2.DocEntryINNER JOIN dbo.OITM t3 ON t2.ItemCode = t3.ItemCodeWHERE t0.ItemCode = @ItemCode and (t0.PostDate >= '20120703' and t0.PostDate <= '20120703'and t1.ItmsGrpCod in ('129','130','131','138') and t3.ItemName like '%%Egg%%' and t0.CmpltQty <> 0) -- Return the result of the function RETURN @strENDAnd here's the query for the day's production:SELECT DISTINCT t0.DocNum, t0.ItemCode, t0.PostDate, t1.ItemName, t0.CmpltQty, dbo.concat_boxes_produced(t0.ItemCode) As BoxesFROM dbo.OWOR t0INNER JOIN dbo.OITM t1 ON t0.ItemCode = t1.ItemCodeINNER JOIN dbo.WOR1 t2 ON t0.DocEntry = t2.DocEntryINNER JOIN dbo.OITM t3 ON t2.ItemCode = t3.ItemCodeWHERE (t0.PostDate >= '20120703' and t0.PostDate <= '20120703'and t1.ItmsGrpCod in ('129','130','131','138') and t3.ItemName like '%%Egg%%' and t0.CmpltQty <> 0)The syntax is all ok and it does concatenate quantities, but only up to the second quantity produced (for some items there are as many as 24 quantities produced). (eg. 12, 45)Is there a reason why it would only be going up to the second quantity? Some quantities are repeated due to the required quantity to be made to fill the box, so don't know if this is an issue (eg, 45 repeats).I'd like it to pick all the quantities produced for the items on that day if possible? (eg. 12, 45, 45,45,45,45,45,45,45,3,45,45,45,45,45,45,45,41,45,45,45)Any help would be greatly appreciated! |
|