SELECT
           gen_random_uuid() as StatisticsReportID
          ,r.ItemName
          ,SUM(r.ItemsCount) as ItemsCount
          ,MIN(r.StartTime) as StartTime
          ,MAX(r.StopTime) as StopTime
          ,SUM(r.TotalParsingTime) as TotalParsingTime
          ,MIN(r.MinParsingTime) as MinParsingTime
          ,MAX(r.MaxParsingTime) as MaxParsingTime
          ,SUM(r.TotalQueryTime) as TotalQueryTime
          ,MIN(r.MinQueryTime) as MinQueryTime
          ,MAX(r.MaxQueryTime) as MaxQueryTime
          ,SUM(r.TotalCreateTime) as TotalCreateTime
          ,MIN(r.MinCreateTime) as MinCreateTime
          ,MAX(r.MaxCreateTime) as MaxCreateTime
          ,SUM(r.TotalQueryCount) as TotalQueryCount
          ,MIN(r.MinQueryCount) as MinQueryCount
          ,MAX(r.MaxQueryCount) as MaxQueryCount
          ,SUM(r.TotalApartmentCount) as TotalApartmentCount
          ,MIN(r.MinApartmentCount) as MinApartmentCount
          ,MAX(r.MaxApartmentCount) as MaxApartmentCount
          ,SUM(r.TotalAttributesCount) as TotalAttributesCount
          ,MIN(r.MinAttributesCount) as MinAttributesCount
          ,MAX(r.MaxAttributesCount) as MaxAttributesCount
          ,SUM(r.FailedReportCount) as FailedReportCount
          ,COALESCE(r.ReportId, '00000000-0000-0000-0000-000000000000') as ReportId
          ,SUM(r.SuccessfulReportCount) as SuccessfulReportCount
    into temp table stat_rep
from VCLib.T_STATISTICS_REPORT r
group by COALESCE(r.ReportId, '00000000-0000-0000-0000-000000000000'), r.ItemName;

delete from VCLib.T_STATISTICS_REPORT;

INSERT INTO VCLib.T_STATISTICS_REPORT
           (StatisticsReportID
           ,ItemName
           ,ItemsCount
           ,StartTime
           ,StopTime
           ,TotalParsingTime
           ,MinParsingTime
           ,MaxParsingTime
           ,TotalQueryTime
           ,MinQueryTime
           ,MaxQueryTime
           ,TotalCreateTime
           ,MinCreateTime
           ,MaxCreateTime
           ,TotalQueryCount
           ,MinQueryCount
           ,MaxQueryCount
           ,TotalApartmentCount
           ,MinApartmentCount
           ,MaxApartmentCount
           ,TotalAttributesCount
           ,MinAttributesCount
           ,MaxAttributesCount
           ,FailedReportCount
           ,ReportId
           ,SuccessfulReportCount)
SELECT
            r.StatisticsReportID
           ,r.ItemName
           ,r.ItemsCount
           ,r.StartTime
           ,r.StopTime
           ,r.TotalParsingTime
           ,r.MinParsingTime
           ,r.MaxParsingTime
           ,r.TotalQueryTime
           ,r.MinQueryTime
           ,r.MaxQueryTime
           ,r.TotalCreateTime
           ,r.MinCreateTime
           ,r.MaxCreateTime
           ,r.TotalQueryCount
           ,r.MinQueryCount
           ,r.MaxQueryCount
           ,r.TotalApartmentCount
           ,r.MinApartmentCount
           ,r.MaxApartmentCount
           ,r.TotalAttributesCount
           ,r.MinAttributesCount
           ,r.MaxAttributesCount
           ,r.FailedReportCount
           ,r.ReportId
           ,r.SuccessfulReportCount
from stat_rep r;

alter table VCLib.T_STATISTICS_REPORT  alter column reportid set not null;