Yeah thats just example, in the actual database there is quite a lot of same names so yeah, merging the duplicates as just one row would be perfect.
I am not sure how to achieve that using report tags and I could do it with SQL query if I knew how to deal with the JSON object in CustomData field with SQL.
stored procedure dml:
will throw an error if no matching entity type is found
/* Get Distinct Entity Custom Field Values */
IF OBJECT_ID('sc.p_DistinctEntityCustomFieldValuesGet') IS NOT NULL
DROP PROCEDURE sc.p_DistinctEntityCustomFieldValuesGet;
GO
CREATE PROCEDURE sc.p_DistinctEntityCustomFieldValuesGet
@EntityTypeName NVARCHAR(MAX),
@CustomFieldName NVARCHAR(MAX)
AS
BEGIN
DECLARE @ErrorMessage NVARCHAR(MAX);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
BEGIN TRY
DECLARE @EntityTypeId INT;
DECLARE @CustomData NVARCHAR(MAX);
DECLARE @FieldData NVARCHAR(MAX);
DECLARE @FieldName NVARCHAR(MAX);
DECLARE @FieldValue NVARCHAR(MAX);
DECLARE @CustomFieldValues TABLE
(
Value NVARCHAR(MAX) NULL
);
SELECT @EntityTypeId = Id
FROM dbo.EntityTypes
WHERE Name = @EntityTypeName;
IF (@EntityTypeId IS NULL)
BEGIN
SET @ErrorMessage = CONCAT('Entity type ', '"', @EntityTypeName, '"', ' not found');
SET @ErrorSeverity = 15;
SET @ErrorState = -1;
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
RETURN 1;
END;
DECLARE cur_custom_data CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT CustomData
FROM dbo.Entities
WHERE EntityTypeId = @EntityTypeId;
OPEN cur_custom_data;
FETCH NEXT FROM cur_custom_data
INTO @CustomData;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE cur_entity_fields CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT Value
FROM OPENJSON(@CustomData);
OPEN cur_entity_fields;
FETCH NEXT FROM cur_entity_fields
INTO @FieldData;
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @FieldName = FieldName,
@FieldValue = FieldValue
FROM
OPENJSON(@FieldData)
WITH (FieldName NVARCHAR(MAX) '$.Name',
FieldValue NVARCHAR(MAX) '$.Value');
INSERT INTO @CustomFieldValues
(Value)
VALUES
(@FieldName);
IF (@FieldName = @CustomFieldName)
BEGIN
INSERT INTO @CustomFieldValues
(Value)
VALUES
(@FieldValue);
END;
FETCH NEXT FROM cur_entity_fields
INTO @FieldData;
END;
CLOSE cur_entity_fields;
DEALLOCATE cur_entity_fields;
FETCH NEXT FROM cur_custom_data
INTO @CustomData;
END;
CLOSE cur_custom_data;
DEALLOCATE cur_custom_data;
SELECT DISTINCT
(Value)
FROM @CustomFieldValues
WHERE Value IS NOT NULL
AND Value <> '';
END TRY
BEGIN CATCH
SET @ErrorMessage = ERROR_MESSAGE();
SET @ErrorSeverity = ERROR_SEVERITY();
SET @ErrorState = ERROR_STATE();
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
END CATCH;
END;
GO
I’m not sure, my specials setup results in lots of orders which are result of same product but where order name is changed on the fly and the item sames report does exactly what your trying to do, it’s clearly not merging based on product and in theory each order is a seperate order in the orders table.
Perhaps sum just doesn’t work on phone? What output did you get on sum phone number?
Could you perhaps add a specific numerical field and sum that with sort on name?
Another poible option do an @ with sql to generate list of distinct names and report entities based on that list?