SqlReports

Изменения

4/2/2020 12:15:53 PM

Details

diff --git a/RevenueComparationByCategories.sql b/RevenueComparationByCategories.sql
index 2f2eead..a0935ba 100644
--- a/RevenueComparationByCategories.sql
+++ b/RevenueComparationByCategories.sql
@@ -77,7 +77,7 @@ begin
 		from dbo.Terminals t with (nolock)
 			inner join dbo.Shops s with (nolock) on 
 					s.Id = t.ShopId
-				and s.CompanyId = @CompanyID
+				and (s.CompanyId = @CompanyID or @CompanyID = 0)
 		
 		where 
 				isnull(t.HostName,'') <> ''
@@ -151,6 +151,7 @@ begin
 
 			inner join dbo.Products p with (nolock) on
 				p.Id = sl.ProductId
+			
 
 		group by tl.ShopID, p.CategoryId
 
diff --git a/RevenueComparationByCategoriesTotalByCompany.sql b/RevenueComparationByCategoriesTotalByCompany.sql
index 6bdd287..d8a39a9 100644
--- a/RevenueComparationByCategoriesTotalByCompany.sql
+++ b/RevenueComparationByCategoriesTotalByCompany.sql
@@ -1,3 +1,4 @@
+
 set noexec off
 go
 if object_id('RevenueComparationByCategoriesTotalByCompany') is not null
@@ -19,7 +20,8 @@ alter procedure dbo.RevenueComparationByCategoriesTotalByCompany
 (
 	@CompanyID int,
 	@PeriodTypeID int,
-	@PeriodID int
+	@PeriodID int,
+	@CategoryID int
 ) 
 as
 
@@ -28,11 +30,14 @@ begin
 --declare @CompanyID int
 --declare @PeriodTypeID int
 --declare @PeriodID int
+--declare @CategoryID int
 
 
---set @CompanyID = 1
+--set @CompanyID = 0
 --set @PeriodTypeID  = 1
 --set @PeriodID = 2
+--set @CategoryID = 1
+
 	
 	declare @ActualPeriodDateFrom DateTime
 	declare @ActualPeriodDateTo DateTime
@@ -75,13 +80,14 @@ begin
 		from dbo.Terminals t with (nolock)
 			inner join dbo.Shops s with (nolock) on 
 					s.Id = t.ShopId
-				and s.CompanyId = @CompanyID
+				and (s.CompanyId = @CompanyID or @CompanyID = 0)
 
 
-	declare @SalesTotals table (CategoryID int, LastPeriodSumm decimal(15, 2), ActualPeriodSumm decimal(15, 2), SalesLastPeriodCount int, SalesActualPeriodCount int)
+	declare @SalesTotals table (ProductId nvarchar(28), CategoryID int, LastPeriodSumm decimal(15, 2), ActualPeriodSumm decimal(15, 2), SalesLastPeriodCount int, SalesActualPeriodCount int)
 
 	insert into @SalesTotals
 		select
+			sl.ProductId,
 			p.CategoryId,
 			
 			Sum
@@ -139,15 +145,18 @@ begin
 				and sl.SessionId = ss.SessionId
 
 			inner join dbo.Products p with (nolock) on
-				p.Id = sl.ProductId
-
-		group by p.CategoryId
+					p.Id = sl.ProductId
+				and (p.CategoryId = @CategoryID or @CategoryID = 0)
+		group by 
+			sl.ProductId, 
+			p.CategoryId
 
 
-	declare @WithdrawsLoadingsTotals table (CategoryID int, WithdrawsLastPeriodCount int, WithdrawsActualPeriodCount int, LoadingsLastPeriodCount int, LoadingsActualPeriodCount int)
+	declare @WithdrawsLoadingsTotals table (ProductId nvarchar(28), CategoryID int, WithdrawsLastPeriodCount int, WithdrawsActualPeriodCount int, LoadingsLastPeriodCount int, LoadingsActualPeriodCount int)
 
 	insert into @WithdrawsLoadingsTotals
 		select
+			prc.ProductId,
 			p.CategoryId,
 
 			Sum
@@ -204,12 +213,16 @@ begin
 				and	prc.Type in (0, 1)
 			
 			inner join dbo.Products p with (nolock) on
-				p.Id = prc.ProductId		
-		
-		group by  p.CategoryId
+					p.Id = prc.ProductId		
+
+		group by 
+			prc.ProductId,
+			p.CategoryId
+			
 
 	
 	select
+		isnull(p.Name, '') as ProductName,
 		isnull(pc.Name, '') as CategoryName,
 		isnull(st.LastPeriodSumm, 0) as LastPeriodSumm,
 		isnull(st.ActualPeriodSumm, 0) as ActualPeriodSumm,
@@ -234,10 +247,11 @@ begin
 		isnull(wlt.WithdrawsActualPeriodCount, 0) as WithdrawsActualPeriodCount
 		
 	
-	from dbo.ProductCategories pc with (nolock) 
-		left join @SalesTotals st on st.CategoryID = pc.Id  
-		left join @WithdrawsLoadingsTotals wlt on wlt.CategoryID = pc.ID
+	from @SalesTotals st 
+		inner join dbo.Products p with (nolock) on p.Id = st.ProductId
+		inner join dbo.ProductCategories pc with (nolock) on pc.Id = st.CategoryID
+		left join @WithdrawsLoadingsTotals wlt on wlt.ProductId = st.ProductId
 
-	order by pc.Id
+	order by pc.Id, p.Name 
 
 end
\ No newline at end of file