Answer the question
In order to leave comments, you need to log in
With rollup and linked server: why doesn't it calculate the amount correctly?
Good afternoon!
There is a table
CREATE TABLE [dbo].[TestRollup]
(
[Dim1] [nvarchar](255) NULL,
[Dim2] [nvarchar](255) NULL,
[Dim3] [nvarchar](255) NULL,
[Metric] [float] NULL
)
INSERT INTO [dbo].[TestRollup]
([Dim1],[Dim2],[Dim3],[Metric])
VALUES
('TestDim1','Dim2Value1','Dim3Value1',1),
('TestDim1','Dim2Value1','Dim3Value2',3),
('TestDim1','Dim2Value2','Dim3Value1',7),
('TestDim1','Dim2Value2','Dim3Value3',5),
('TestDim1','Dim2Value3','Dim3Value2',NULL),
('TestDim1','Dim2Value3','Dim3Value2',NULL),
('TestDim1','Dim2Value3','Dim3Value5',2),
('TestDim1','Dim2Value3','Dim3Value5',1)
SELECT
[Dim1]
,[Dim2]
,[Dim3]
,SUM([Metric]) Sum_Metric
,GROUPING(Dim1) Grouping_Dim1
,GROUPING(Dim2) Grouping_Dim2
,GROUPING(Dim3) Grouping_Dim3
FROM [dbo].[TestRollup]
GROUP BY [Dim1],[Dim2],[Dim3]
WITH ROLLUP
SELECT
[Dim1]
,[Dim2]
,[Dim3]
,SUM([Metric]) Sum_Metric
,GROUPING(Dim1) Grouping_Dim1
,GROUPING(Dim2) Grouping_Dim2
,GROUPING(Dim3) Grouping_Dim3
FROM <linked_server>.<name_db>.[dbo].[TestRollup]
GROUP BY [Dim1],[Dim2],[Dim3]
WITH ROLLUP
Answer the question
In order to leave comments, you need to log in
SUM() ignores NULL during aggregation and returns NULL if no match was found for the given criteria. If you have in the table exactly the data that you brought in INSERT, then for the combination 'TestDim1','Dim2Value3','Dim3Value2' SUM(metric) should be NULL.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question