1

I have tried with this code to get my result, but I can not understand what I have missed. I have below table(s) and try to generate a dynamic sql to get the result.

Declare  
    @date1 date = '2015-06-01',
    @date2 date = '2015-06-30',
    @StoreNo Nvarchar(Max) = ' AND S.StoreNo IN (61,63,450,451)'
AS
BEGIN    
    Declare @sql_store nvarchar(max)

    SET @sql_store = 'SELECT StoreNo,StoreName INTO ##StoreList FROM Store S WHERE StoreNo IN  (61,63,450,451)' + @StoreNo

    EXECUTE sp_executesql @sql_store


create table #inventory 
(
    StoreNo int,  
    Date date, 
    ProductBarCode varchar(14),
    ProductQty int 
)

BEGIN
    INSERT INTO #inventory
        SELECT
            S.StoreNo, s.Date, s.ProductBarCode,
            ISNULL(SUM(s.ProductQty), 0) as ProductQty
        FROM
            (SELECT
                 StoreNo, Date, ProductBarCode, ProductQty 
             FROM
                 ##inv1 
             WHERE
                 Date BETWEEN @date_s AND @date_e 
                 AND StoreNo IN (SELECT StoreNo from ##StoreList )  -- I was using like this 

                            Union all
                            Select StoreNo,Date,ProductBarCode,ProductQty from ##inv2 where  Date between @date_s and @date_e and StoreNo IN (select StoreNo from ##StoreList )
                            )S
                            WHERE StoreNo IS NOT NULL
                           GROUP BY s.StoreNo,s.Date,s.ProductBarCode
Declare     @pheader nvarchar(Max),@sql_pivot nvarchar(max)

Begin 

    SELECT   @pheader=ISNULL(@pheader,'')+'['+StoreName+'],'
                                                FROM ##StoreList GROUP BY StoreNo,StoreName ORDER BY StoreNo
                                                SET @pheader= LEFT(@pheader, LEN(@pheader) - 1)

                                                SET @sql_pivot=
                                                    N'Select * from'+
                                                    '(
                                                        SELECT 

                                                                StoreName
                                                              ,i.Date as Date
                                                              ,i.ProductBarCode as ProductBarCode
                                                              ,isnull(i.productqty-(select ProductQty as runningsum from #Stock st where st.date<=i.date and st.storeno=i.storeno and st.ProductBarCode = i.ProductBarCode ),i.productqty) as ProductQty

                                            From #inventory i
                                                        LEFT JOIN Store S ON S.StoreNo = i.StoreNo 
                                                    Left JOIN #Stock st ON st.StoreNo = i.StoreNo and st.ProductBarCode = i.ProductBarCode and st.Date = i.Date
                                                    --Right Join ##product p ON i.ProductBarCode = p.ProductBarCode and i.Date = P.Date
                                                      --WHERE p.ProductBarCode IN (0065103700004,
                                                            --                          2017961746012)
                                                        GROUP BY 

                                                                i.storeNo
                                                                ,StoreName
                                                              ,i.ProductBarCode 

                                                               --,p.ProductBarCode
                                                               ,i.Date
                                                               ,i.ProductQty)p


                                                            pivot
                                                            (sum(ProductQty) for [StoreName] IN ('+ @pheader+N')
                                                            ) As Pivt order By Date,ProductBarCode'


                                    EXECUTE sp_executesql  @sql_pivot


            SET @pheader = NULL  
            SELECT   @pheader=ISNULL(@pheader,'')+'0 as ['+cast(StoreNo as varchar)+'],'
            FROM ##StoreList GROUP BY StoreNo ORDER BY StoreNo
            SET @pheader= LEFT(@pheader, LEN(@pheader) - 1)

            SET  @pheader =  ' SELECT  0 as '+''''+'         '+''''+', 0 as '+''''+'          '+''''+','+@pheader
            EXECUTE sp_executesql  @pheader
  END

When I tried in pivot It's output shows only one StoreNo and Other Null.

Date        ProductBarCode  col1 col2  col3 col4
2015-06-01  2300007115072   7    NULL  NULL NULL
2015-06-01  2300012213046   5    NULL  NULL NULL
2015-06-01  2300012712075   8    NULL  NULL NULL

I'm not sure what mistake I did here..

mad
  • 41
  • 7
  • You need dynamic SQL if you do not know your columns – PacoDePaco Aug 25 '16 at 05:34
  • @PawełKucharski little bit explanation please !! – mad Aug 25 '16 at 05:59
  • Please read [this](http://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query) question, you can find there a pretty basic working example of what you need. I see you have already edited your question ;] – PacoDePaco Aug 25 '16 at 06:30

0 Answers0