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;
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
New contributor
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
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
sql-server sql
New contributor
New contributor
New contributor
asked Apr 16 at 12:45
user519554user519554
1
1
New contributor
New contributor
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
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
add a comment |
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes