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 - Тарых жана география Навигация менюсу

          Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

          Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070