Subquery returned more than 1 value. when the subquery is used as an expression.in SQL Server -
can tell me how solve problem?
here's code
subquery :
select a.storeno, c.[date], a.productbarcode, a.productqty ##inv1 #calender c outer apply (select top 100 percent * ##temp i.date < c.date , storeno in (select storeno ##storelist) order i.date) option (maxrecursion 0)
i used top 100 percent because have more 1000 productbarcode each storeno, if choose top 1 showing 1 productbarcode duplicate value next day.
declare @pheader nvarchar(max), @sql_pivot nvarchar(max) begin select @pheader = stuff((select distinct ',' + quotename([storeno]) ##storelist xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') --set @pheader = left(@pheader, len(@pheader) - 1) set @sql_pivot = 'select * (select s.[storeno], p.date date, p.productbarcode productbarcode, isnull(i.productqty - (select productqty runningsum #stock st st.date <= i.date , st.storeno = i.storeno , st.productbarcode = i.productbarcode), i.productqty) productqty #inventory left join ##storelist s on s.storeno = i.storeno left join #stock st on st.storeno = i.storeno , st.productbarcode = i.productbarcode , st.date = i.date right join ##product p on i.date = p.date , p.productbarcode = i.productbarcode -- p.productbarcode in(2300007115072,2300012213046,2300012712075) group s.storeno, i.storeno, i.productbarcode, i.date, p.productbarcode, p.date, i.productqty) p pivot (sum(productqty) [storeno] in ('+ @pheader+') )pvt' execute sp_executesql @sql_pivot
i cant 3 4 storeno @ time if select more 10 showing error
subquery returned more 1 value. not permitted when subquery follows =, !=, <, <= , >, >= or when subquery used expression.
i'm using sql server .
add top 1 subquery. try this,
declare @pheader nvarchar(max) ,@sql_pivot nvarchar(max) begin select @pheader = stuff(( select distinct ',' + quotename([storeno]) ##storelist xml path('') ,type ).value('.', 'nvarchar(max)'), 1, 1, '') set @pheader = left(@pheader, len(@pheader) - 1) set @sql_pivot = 'select * (select s.[storeno], p.date date, p.productbarcode productbarcode, isnull(i.productqty - (select top 1 productqty runningsum #stock st st.date <= i.date , st.storeno = i.storeno , st.productbarcode = i.productbarcode), i.productqty) productqty #inventory left join ##storelist s on s.storeno = i.storeno left join #stock st on st.storeno = i.storeno , st.productbarcode = i.productbarcode , st.date = i.date right join ##product p on i.date = p.date , p.productbarcode = i.productbarcode -- p.productbarcode in(2300007115072,2300012213046,2300012712075) group s.storeno, i.storeno, i.productbarcode, i.date, p.productbarcode, p.date, i.productqty) p pivot (sum(productqty) [storeno] in (' + @pheader + ') )pvt' execute sp_executesql @sql_pivot end
Comments
Post a Comment