Y
Y
Yuri2015-10-27 15:23:02
SQL Server
Yuri, 2015-10-27 15:23:02

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)

request
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

is executed correctly, the Sum_Metric column is considered correct.
But the next request
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

returns NULL for some subtotals.
Those. if the data is taken from a linked server and there are NULL values ​​for the Metric field, then the sum for the subtotal will be NULL. What could be the problem? maybe it's some link settings?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksey Ratnikov, 2015-10-27
@mahoho

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 question

Ask a Question

731 491 924 answers to any question