如果值与上一行中的值相同,则在没有存储过程或函数的情况下折叠 Sql 中的数据

Collapse Data in Sql without stored precedure or function if a value is the same as the value from row above

如果值与上一行中的值相同,我会遇到有关分组的问题。

我们的声明如下所示:

SELECT pat_id, 
       treatData.treatmentdate AS Date, 
       treatMeth.name          AS TreatDataTableInfo, 
       treatData.treatmentid   AS TreatID 
FROM   dialysistreatmentdata treatData 
       LEFT JOIN hdtreatmentmethods treatMeth 
              ON treatMeth.id = treatData.hdtreatmentmethodid 
WHERE  treatData.hdtreatmentmethodid IS NOT NULL 
       AND Year(treatData.treatmentdate) >= 2013 
       AND ekeyid = 12 
ORDER  BY treatData.ekeyid, 
          treatmentdate DESC, 
          treatdatatableinfo; 

输出如下所示:

如果值与之前的 row/rows 中的值相同,则应将所需的输出分组,并且应该是 ToDate,正如您在屏幕截图中看到的那样,即下一行的日期 -1 天. 所需的输出应如下所示:

希望有人能解决这个问题!

或者也许有人知道如何在 qlikview 中解决这个问题。

期待解决方案 迈克尔

您想将治疗集折叠成单行。这是一个 "gaps-and-islands" 问题。我喜欢行号方法的差异:

select patid, min(date) as fromdate, max(date) as todate, TreatDataTableInfo,
       min(treatid)
from (select td.Pat_ID, td.TreatmentDate As Date, tm.Name As TreatDataTableInfo,      
           td.TreatmentID As TreatID,
           row_number() over (partition by td.pat_id order by td.treatmentdate) as seqnum_p,
           row_number() over (partition by td.pat_id, tm.name order by td.treatment_date) as seqnum_pn
      from DialysisTreatmentData td Left join
           HDTreatmentMethods tm
           On tm.ID = td.HDTreatmentMethodID
      where td.HDTreatmentMethodID Is Not Null And
            td.TreatmentDate) >= '2013-01-01' and
            EKeyID = 12
     ) t
group by patid, TreatDataTableInfo, (seqnum_p - seqnum_pn)
order by patid, TreatmentDate Desc, TreatDataTableInfo;

注意:这使用 ANSI 标准 window 函数 row_number(),它在大多数数据库中可用。

下面是一个可能的 Qlikview 解决方案。我在脚本中添加了一些注释。如果不清楚,请告诉我。结果图片在脚本下方

RawData:
Load * Inline [
    Pat_ID,Date,TreatDataTableInfo,TreatId
    PatNum_12,08.07.2016,HDF Pradilution,1
    PatNum_12,07.07.2016,HDF Predilution,2
    PatNum_12,23.03.2016,HD,3
    PatNum_12,24.11.2015,HD,4
    PatNum_12,22.11.2015,HD,5
    PatNum_12,04.09.2015,HD,6
    PatNum_12,01.09.2015,HD,7
    PatNum_12,30.07.2015,HD,8
    PatNum_12,12.01.2015,HD,9
    PatNum_12,09.01.2015,HD,10
    PatNum_12,26.08.2014,Hemodialysis,11
    PatNum_12,08.07.2014,Hemodialysis,12
    PatNum_12,23.05.2014,Hemodialysis,13
    PatNum_12,19.03.2014,Hemodialysis,14
    PatNum_12,29.01.2014,Hemodialysis,15
    PatNum_12,14.12.2013,Hemodialysis,16
    PatNum_12,26.10.2013,Hemodialysis,17
    PatNum_12,05.10.2013,Hemodialysis,18
    PatNum_12,03.10.2013,HD,19
    PatNum_12,24.06.2013,Hemodialysis,20
    PatNum_12,03.06.2013,Hemodialysis,21
    PatNum_12,14.05.2013,Hemodialysis,22
    PatNum_12,26.02.2013,HDF Postdilution,23
    PatNum_12,23.02.2013,HDF Pradilution,24
    PatNum_12,21.02.2013,HDF Postdilution,25
    PatNum_12,07.02.2013,HD,26
    PatNum_12,25.01.2013,HDF Pradilution,27
    PatNum_12,18.01.2013,HDF Pradilution,28
];


GroupedData:
Load
    *,
    // assign new GroupId for all rows where the TreatDataTableInfo is equal
    if( RowNo() = 1, 1,
        if( TreatDataTableInfo <> peek('TreatDataTableInfo'),
            peek('GroupId') + 1, peek('GroupId'))) as GroupId,  
    // assign new GroupSubId (incremental int) for all the records in each group
    if( TreatDataTableInfo <> peek('TreatDataTableInfo'), 
        1, peek('GroupSubId') + 1) as GroupSubId,
    // pick the first Date field value and spread it acccross the group
    if( TreatDataTableInfo <> peek('TreatDataTableInfo'), TreatId, peek('TreatId_Temp')) as TreatId_Temp
Resident
    RawData
;

Drop Table RawData; 


right join (GroupedData)

// get the max GroupSubId for each group and right join it to 
// the GroupedData table to remove the records we dont need
MaxByGroup:
Load
    max(GroupSubId) as GroupSubId,
    GroupId
Resident
    GroupedData
Group By
    GroupId 
;       

// these are not needed anymore 
Drop Fields GroupId, GroupSubId, TreatId;

// replace the old TreatId with the new TreatId_Temp field
// which contains the first TreatId for each group
Rename Field TreatId_Temp to TreatId;