如果值与上面的行中的值相同,则在Sql中折叠数据而不存储precedure或函数

时间:2016-07-08 13:48:30

标签: sql qlikview

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

我们的陈述如下:

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; 

输出如下:

Original output

如果值与之前的行/行中的值相同,则应对所需的输出进行分组,并且应该是ToDate,如屏幕截图中所示,即下一行-1天的日期。 所需的输出应如下所示:

Desired output

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

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

期待解决方案 迈克尔

2 个答案:

答案 0 :(得分:1)

您希望将处理剧集折叠为单行。这是一个"缺口和岛屿"问题。我喜欢行数方法的不同之处:

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标准窗口函数row_number(),它在大多数数据库中都可用。

答案 1 :(得分:0)

以下是可能的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; 

enter image description here