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?

Multi tool use
Multi tool use

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

          YjPCzo1zE,AkTD20
          rd11V0e1CQSXBLuv,MmdAzba zUEICnC4K 72zx1Y1qa,zS,wPXSJmrTUttYyAvooqrY6Z0,G7G

          Popular posts from this blog

          RemoteApp sporadic failureWindows 2008 RemoteAPP client disconnects within a matter of minutesWhat is the minimum version of RDP supported by Server 2012 RDS?How to configure a Remoteapp server to increase stabilityMicrosoft RemoteApp Active SessionRDWeb TS connection broken for some users post RemoteApp certificate changeRemote Desktop Licensing, RemoteAPPRDS 2012 R2 some users are not able to logon after changed date and time on Connection BrokersWhat happens during Remote Desktop logon, and is there any logging?After installing RDS on WinServer 2016 I still can only connect with two users?RD Connection via RDGW to Session host is not connecting

          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

          Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020