sql server show multiple columns one by one using pivot [closed] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Come Celebrate our 10 Year Anniversary!SQL Server Using 100% CPUOne vs. multiple DB instances on a SQL serverAccess or SQL query to list most recent AutoNumber within a specific RangeHow to dump a Microsoft SQL Server database to a SQL script?How to filter columns in SQL Server replication?Query Running Slow after migrating to SQL Server 2008SQL Server 2008 table with multiple FilegroupsSQL Server connection error (a weird one) (unsolved yet)MSSQL - show columns in specific table and databaseFor SQL server with one User CAL, can one user be logged in concurrently from multiple devices?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

Trying to enter the Fox's den

Does Prince Arnaud cause someone holding the Princess to lose?

Is it OK if I do not take the receipt in Germany?

Marquee sign letters

Kepler's 3rd law: ratios don't fit data

What could prevent concentrated local exploration?

A German immigrant ancestor has a "Registration Affidavit of Alien Enemy" on file. What does that mean exactly?

Lights are flickering on and off after accidentally bumping into light switch

Short story about an alien named Ushtu(?) coming from a future Earth, when ours was destroyed by a nuclear explosion

How to mute a string and play another at the same time

lm and glm function in R

Can a Wizard take the Magic Initiate feat and select spells from the Wizard list?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Why did Israel vote against lifting the American embargo on Cuba?

Raising a bilingual kid. When should we introduce the majority language?

What helicopter has the most rotor blades?

Assertions In A Mock Callout Test

Why these surprising proportionalities of integrals involving odd zeta values?

When speaking, how do you change your mind mid-sentence?

Are there any AGPL-style licences that require source code modifications to be public?

Like totally amazing interchangeable sister outfit accessory swapping or whatever

Is Vivien of the Wilds + Wilderness Reclimation a competitive combo?

“Since the train was delayed for more than an hour, passengers were given a full refund.” – Why is there no article before “passengers”?



sql server show multiple columns one by one using pivot [closed]



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Come Celebrate our 10 Year Anniversary!SQL Server Using 100% CPUOne vs. multiple DB instances on a SQL serverAccess or SQL query to list most recent AutoNumber within a specific RangeHow to dump a Microsoft SQL Server database to a SQL script?How to filter columns in SQL Server replication?Query Running Slow after migrating to SQL Server 2008SQL Server 2008 table with multiple FilegroupsSQL Server connection error (a weird one) (unsolved yet)MSSQL - show columns in specific table and databaseFor SQL server with one User CAL, can one user be logged in concurrently from multiple devices?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















sql server i need



query of



declare @fromdate varchar(100)='2019-03-01',
@Todate varchar(100)='2019-05-05',
@CompanyID varchar(100)=1,
@BookType varchar(100)=1


DECLARE @Cols NVARCHAR(max)
DECLARE @NAME nVARCHAR(1000)=''
SET @Cols=''

--make column list for PIVOT
SELECT @Cols=@Cols+ '['+s.Branch_Name +']'+ ', ' FROM
(Select distinct isnull(BH.Branch_Name,'')as Branch_Name,isnull(BH.Branch_Name+' Amount','')as amount from tbl_Branch BH
left outer join tbl_StockPosting SP
on SP.StockPosting_BranchId =BH.Branch_ID
left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

) AS s
declare @Cols1 nvarchar(max)
set @Cols1=''
SELECT @Cols1=@Cols1+ '['+s1.amount +']'+ ', ' FROM
(Select distinct isnull(BH.Branch_Name+' Amount','')as Amount from tbl_Branch BH
left outer join tbl_StockPosting SP
on SP.StockPosting_BranchId =BH.Branch_ID
left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

) AS s1
IF @Cols<>''
BEGIN
--remove last comma from column list
SET @Cols=LEFT(@Cols,LEN(@Cols)-1)
set @Cols1=left(@Cols1,len(@Cols1)-1)
--create pivot query as we have just added distinct year list in @Cols variable
SET @Cols='SELECT * from (
Select
sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
where StockPosting_Date between '''+@fromdate+''' and '''+@Todate+'''
and StockPosting_CompanyId='''+@CompanyID+'''
and StockPosting_BookType=case when '''+@BookType+'''<>0 then '''+@BookType+''' else StockPosting_BookType end
Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


) up
PIVOT (Sum(StockPosting_Qty) for Branch_Name in ('+@cols+','+@Cols1+')) AS pivo
--PIVOT (Sum(StockPosting_Qty) FOR amount IN ('+@Cols1+')) AS P2
order by Batch_Id asc'

print @Cols
print @Cols1
EXECUTE sp_executeSQL @Cols
End


output of



 SELECT * from (
Select
sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
where StockPosting_Date between '2019-03-01' and '2019-05-05'
and StockPosting_CompanyId='1'
and StockPosting_BookType=case when '1'<>0 then '1' else StockPosting_BookType end
Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


) up
PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN], [MUMBAI], [NERUL], [RETAILSOFT], [RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [STORE], [THAEN], [THANE], [TURBHE], [VASHI], [WAREHOUSE],[KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS pivo
--PIVOT (Sum(StockPosting_Qty) FOR amount IN ([KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS P2
order by Batch_Id asc
[KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount]


and need result as



PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN],,[KALYAN Amount], [MUMBAI],[MUMBAI Amount], [NERUL],[NERUL Amount], [RETAILSOFT], [RETAILSOFT Amount],[RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [RETAILSOFT MUMBAI Amount], [STORE], [STORE Amount], [THAEN],[THAEN Amount], [THANE], [THANE Amount], [TURBHE],[TURBHE Amount], [VASHI],[VASHI Amount], [WAREHOUSE], [WAREHOUSE Amount])) AS pivo


please help me










share|improve this question







New contributor




user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by Michael Hampton Apr 16 at 15:01


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – Michael Hampton
If this question can be reworded to fit the rules in the help center, please edit the question.






















    -1















    sql server i need



    query of



    declare @fromdate varchar(100)='2019-03-01',
    @Todate varchar(100)='2019-05-05',
    @CompanyID varchar(100)=1,
    @BookType varchar(100)=1


    DECLARE @Cols NVARCHAR(max)
    DECLARE @NAME nVARCHAR(1000)=''
    SET @Cols=''

    --make column list for PIVOT
    SELECT @Cols=@Cols+ '['+s.Branch_Name +']'+ ', ' FROM
    (Select distinct isnull(BH.Branch_Name,'')as Branch_Name,isnull(BH.Branch_Name+' Amount','')as amount from tbl_Branch BH
    left outer join tbl_StockPosting SP
    on SP.StockPosting_BranchId =BH.Branch_ID
    left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

    ) AS s
    declare @Cols1 nvarchar(max)
    set @Cols1=''
    SELECT @Cols1=@Cols1+ '['+s1.amount +']'+ ', ' FROM
    (Select distinct isnull(BH.Branch_Name+' Amount','')as Amount from tbl_Branch BH
    left outer join tbl_StockPosting SP
    on SP.StockPosting_BranchId =BH.Branch_ID
    left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

    ) AS s1
    IF @Cols<>''
    BEGIN
    --remove last comma from column list
    SET @Cols=LEFT(@Cols,LEN(@Cols)-1)
    set @Cols1=left(@Cols1,len(@Cols1)-1)
    --create pivot query as we have just added distinct year list in @Cols variable
    SET @Cols='SELECT * from (
    Select
    sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
    ,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
    left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
    inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
    inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
    left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
    where StockPosting_Date between '''+@fromdate+''' and '''+@Todate+'''
    and StockPosting_CompanyId='''+@CompanyID+'''
    and StockPosting_BookType=case when '''+@BookType+'''<>0 then '''+@BookType+''' else StockPosting_BookType end
    Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


    ) up
    PIVOT (Sum(StockPosting_Qty) for Branch_Name in ('+@cols+','+@Cols1+')) AS pivo
    --PIVOT (Sum(StockPosting_Qty) FOR amount IN ('+@Cols1+')) AS P2
    order by Batch_Id asc'

    print @Cols
    print @Cols1
    EXECUTE sp_executeSQL @Cols
    End


    output of



     SELECT * from (
    Select
    sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
    ,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
    left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
    inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
    inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
    left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
    where StockPosting_Date between '2019-03-01' and '2019-05-05'
    and StockPosting_CompanyId='1'
    and StockPosting_BookType=case when '1'<>0 then '1' else StockPosting_BookType end
    Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


    ) up
    PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN], [MUMBAI], [NERUL], [RETAILSOFT], [RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [STORE], [THAEN], [THANE], [TURBHE], [VASHI], [WAREHOUSE],[KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS pivo
    --PIVOT (Sum(StockPosting_Qty) FOR amount IN ([KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS P2
    order by Batch_Id asc
    [KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount]


    and need result as



    PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN],,[KALYAN Amount], [MUMBAI],[MUMBAI Amount], [NERUL],[NERUL Amount], [RETAILSOFT], [RETAILSOFT Amount],[RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [RETAILSOFT MUMBAI Amount], [STORE], [STORE Amount], [THAEN],[THAEN Amount], [THANE], [THANE Amount], [TURBHE],[TURBHE Amount], [VASHI],[VASHI Amount], [WAREHOUSE], [WAREHOUSE Amount])) AS pivo


    please help me










    share|improve this question







    New contributor




    user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    closed as off-topic by Michael Hampton Apr 16 at 15:01


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – Michael Hampton
    If this question can be reworded to fit the rules in the help center, please edit the question.


















      -1












      -1








      -1








      sql server i need



      query of



      declare @fromdate varchar(100)='2019-03-01',
      @Todate varchar(100)='2019-05-05',
      @CompanyID varchar(100)=1,
      @BookType varchar(100)=1


      DECLARE @Cols NVARCHAR(max)
      DECLARE @NAME nVARCHAR(1000)=''
      SET @Cols=''

      --make column list for PIVOT
      SELECT @Cols=@Cols+ '['+s.Branch_Name +']'+ ', ' FROM
      (Select distinct isnull(BH.Branch_Name,'')as Branch_Name,isnull(BH.Branch_Name+' Amount','')as amount from tbl_Branch BH
      left outer join tbl_StockPosting SP
      on SP.StockPosting_BranchId =BH.Branch_ID
      left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

      ) AS s
      declare @Cols1 nvarchar(max)
      set @Cols1=''
      SELECT @Cols1=@Cols1+ '['+s1.amount +']'+ ', ' FROM
      (Select distinct isnull(BH.Branch_Name+' Amount','')as Amount from tbl_Branch BH
      left outer join tbl_StockPosting SP
      on SP.StockPosting_BranchId =BH.Branch_ID
      left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

      ) AS s1
      IF @Cols<>''
      BEGIN
      --remove last comma from column list
      SET @Cols=LEFT(@Cols,LEN(@Cols)-1)
      set @Cols1=left(@Cols1,len(@Cols1)-1)
      --create pivot query as we have just added distinct year list in @Cols variable
      SET @Cols='SELECT * from (
      Select
      sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
      ,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
      left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
      inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
      inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
      left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
      where StockPosting_Date between '''+@fromdate+''' and '''+@Todate+'''
      and StockPosting_CompanyId='''+@CompanyID+'''
      and StockPosting_BookType=case when '''+@BookType+'''<>0 then '''+@BookType+''' else StockPosting_BookType end
      Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


      ) up
      PIVOT (Sum(StockPosting_Qty) for Branch_Name in ('+@cols+','+@Cols1+')) AS pivo
      --PIVOT (Sum(StockPosting_Qty) FOR amount IN ('+@Cols1+')) AS P2
      order by Batch_Id asc'

      print @Cols
      print @Cols1
      EXECUTE sp_executeSQL @Cols
      End


      output of



       SELECT * from (
      Select
      sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
      ,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
      left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
      inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
      inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
      left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
      where StockPosting_Date between '2019-03-01' and '2019-05-05'
      and StockPosting_CompanyId='1'
      and StockPosting_BookType=case when '1'<>0 then '1' else StockPosting_BookType end
      Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


      ) up
      PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN], [MUMBAI], [NERUL], [RETAILSOFT], [RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [STORE], [THAEN], [THANE], [TURBHE], [VASHI], [WAREHOUSE],[KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS pivo
      --PIVOT (Sum(StockPosting_Qty) FOR amount IN ([KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS P2
      order by Batch_Id asc
      [KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount]


      and need result as



      PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN],,[KALYAN Amount], [MUMBAI],[MUMBAI Amount], [NERUL],[NERUL Amount], [RETAILSOFT], [RETAILSOFT Amount],[RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [RETAILSOFT MUMBAI Amount], [STORE], [STORE Amount], [THAEN],[THAEN Amount], [THANE], [THANE Amount], [TURBHE],[TURBHE Amount], [VASHI],[VASHI Amount], [WAREHOUSE], [WAREHOUSE Amount])) AS pivo


      please help me










      share|improve this question







      New contributor




      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      sql server i need



      query of



      declare @fromdate varchar(100)='2019-03-01',
      @Todate varchar(100)='2019-05-05',
      @CompanyID varchar(100)=1,
      @BookType varchar(100)=1


      DECLARE @Cols NVARCHAR(max)
      DECLARE @NAME nVARCHAR(1000)=''
      SET @Cols=''

      --make column list for PIVOT
      SELECT @Cols=@Cols+ '['+s.Branch_Name +']'+ ', ' FROM
      (Select distinct isnull(BH.Branch_Name,'')as Branch_Name,isnull(BH.Branch_Name+' Amount','')as amount from tbl_Branch BH
      left outer join tbl_StockPosting SP
      on SP.StockPosting_BranchId =BH.Branch_ID
      left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

      ) AS s
      declare @Cols1 nvarchar(max)
      set @Cols1=''
      SELECT @Cols1=@Cols1+ '['+s1.amount +']'+ ', ' FROM
      (Select distinct isnull(BH.Branch_Name+' Amount','')as Amount from tbl_Branch BH
      left outer join tbl_StockPosting SP
      on SP.StockPosting_BranchId =BH.Branch_ID
      left outer join [dbo].[tbl_Batch] on [Batch_Id]=SP.StockPosting_BatchID

      ) AS s1
      IF @Cols<>''
      BEGIN
      --remove last comma from column list
      SET @Cols=LEFT(@Cols,LEN(@Cols)-1)
      set @Cols1=left(@Cols1,len(@Cols1)-1)
      --create pivot query as we have just added distinct year list in @Cols variable
      SET @Cols='SELECT * from (
      Select
      sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
      ,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
      left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
      inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
      inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
      left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
      where StockPosting_Date between '''+@fromdate+''' and '''+@Todate+'''
      and StockPosting_CompanyId='''+@CompanyID+'''
      and StockPosting_BookType=case when '''+@BookType+'''<>0 then '''+@BookType+''' else StockPosting_BookType end
      Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


      ) up
      PIVOT (Sum(StockPosting_Qty) for Branch_Name in ('+@cols+','+@Cols1+')) AS pivo
      --PIVOT (Sum(StockPosting_Qty) FOR amount IN ('+@Cols1+')) AS P2
      order by Batch_Id asc'

      print @Cols
      print @Cols1
      EXECUTE sp_executeSQL @Cols
      End


      output of



       SELECT * from (
      Select
      sum(StockPosting_Qty) as StockPosting_Qty ,Batch_Id,Batch_No,convert(varchar,Batch_PackedDate,105)as PackedDate
      ,convert(varchar,Batch_ExpiryDate,105)as ExpiryDate,IT.Item_Name,ID.Item_ConversionUnit,Branch_Name,0 amount,Batch_SellingPrice from tbl_Batch
      left outer join tbl_StockPosting SP on Batch_Id=StockPosting_BatchID
      inner join tbl_Item IT on IT.Item_Id=Batch_ItemId
      inner join tbl_ItemDetail ID on IT.Item_Id=ID.ItemDt_ItemId
      left outer join tbl_Branch BH on BH.Branch_ID=SP.StockPosting_BranchId
      where StockPosting_Date between '2019-03-01' and '2019-05-05'
      and StockPosting_CompanyId='1'
      and StockPosting_BookType=case when '1'<>0 then '1' else StockPosting_BookType end
      Group by Batch_Id,Batch_No,Batch_SellingPrice,Branch_Name,Batch_PackedDate,Item_Name,Batch_ExpiryDate,Item_ConversionUnit


      ) up
      PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN], [MUMBAI], [NERUL], [RETAILSOFT], [RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [STORE], [THAEN], [THANE], [TURBHE], [VASHI], [WAREHOUSE],[KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS pivo
      --PIVOT (Sum(StockPosting_Qty) FOR amount IN ([KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount])) AS P2
      order by Batch_Id asc
      [KALYAN Amount], [MUMBAI Amount], [NERUL Amount], [RETAILSOFT Amount], [RETAILSOFT MUMBAI Amount], [RETAILSOFT TECHNOLOGIES Amount], [STORE Amount], [THAEN Amount], [THANE Amount], [TURBHE Amount], [VASHI Amount], [WAREHOUSE Amount]


      and need result as



      PIVOT (Sum(StockPosting_Qty) for Branch_Name in ([KALYAN],,[KALYAN Amount], [MUMBAI],[MUMBAI Amount], [NERUL],[NERUL Amount], [RETAILSOFT], [RETAILSOFT Amount],[RETAILSOFT MUMBAI], [RETAILSOFT TECHNOLOGIES], [RETAILSOFT MUMBAI Amount], [STORE], [STORE Amount], [THAEN],[THAEN Amount], [THANE], [THANE Amount], [TURBHE],[TURBHE Amount], [VASHI],[VASHI Amount], [WAREHOUSE], [WAREHOUSE Amount])) AS pivo


      please help me







      sql-server sql






      share|improve this question







      New contributor




      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Apr 16 at 12:45









      user519554user519554

      1




      1




      New contributor




      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user519554 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      closed as off-topic by Michael Hampton Apr 16 at 15:01


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – Michael Hampton
      If this question can be reworded to fit the rules in the help center, please edit the question.







      closed as off-topic by Michael Hampton Apr 16 at 15:01


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – Michael Hampton
      If this question can be reworded to fit the rules in the help center, please edit the question.




















          0






          active

          oldest

          votes

















          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

          Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

          What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company