仅在具有最新时间戳的情况下选择每个特定变量

时间:2017-01-18 16:49:27

标签: sql qlikview

我有一张看起来像这样的表 -

ChangeKeyValue  ChangeFieldName ChangeOldValue  ChangeNewValue
106 OGCSTATUSID 20  10
106 OGCSTATUSID 10  20
106 DATALOADSTATUSID    10  20
106 DATAAMOUNTSTATUSID  10  20
106 OGCSTATUSID 20  10
106 DATALOADSTATUSID    20  10
106 DATAAMOUNTSTATUSID  20  10
106 OGCSTATUSID Not Yet Reviewed    Under Review
106 OGCSTATUSID 20  Not Yet Reviewed
106 DATALOADSTATUSID    Not Loaded  Loaded - Outside DSC
106 DATALOADSTATUSID    Loaded - Outside DSC    Not Loaded
106 DATAAMOUNTSTATUSID  None    Partial
106 DATAAMOUNTSTATUSID  Partial Full
106 DATAAMOUNTSTATUSID  30  None
106 OGCSTATUSID Not Yet Reviewed    Under Review
106 OGCSTATUSID 20  Not Yet Reviewed
106 DATALOADSTATUSID    Not Loaded  Loaded - Outside DSC
106 DATALOADSTATUSID    20  Not Loaded
106 DATAAMOUNTSTATUSID  None    Partial
106 DATAAMOUNTSTATUSID  20  None
106 OGCSTATUSID Not Yet Reviewed    Rejected - Do Not Load
106 OGCSTATUSID Rejected - Do Not Load  Not Yet Reviewed
106 DATALOADSTATUSID    Not Loaded  Loaded - Outside DSC
106 DATALOADSTATUSID    Loaded - Outside DSC    Not Loaded
106 DATAAMOUNTSTATUSID  None    Partial
106 DATAAMOUNTSTATUSID  Partial None
106 OGCSTATUSID Not Yet Reviewed    Under Review
106 OGCSTATUSID Under Review    Not Yet Reviewed
106 DATALOADSTATUSID    Not Loaded  Loaded - Outside DSC
106 DATALOADSTATUSID    Loaded - Outside DSC    Not Loaded
106 DATAAMOUNTSTATUSID  None    Partial
106 DATAAMOUNTSTATUSID  Partial Quarantined - DSC

时间戳看起来像这样 -

2016-03-01 11:03:01.703
2016-03-01 11:37:11.117
2016-03-01 11:37:27.933
2016-03-01 11:37:30.017
2016-03-01 11:37:40.837
2016-03-01 11:37:43.250
2016-03-01 11:37:45.890
2016-03-01 12:55:47.567
2016-03-01 12:56:42.347
2016-03-01 12:56:46.917
2016-03-01 12:56:51.413
2016-03-01 12:56:54.737
2016-03-01 12:57:01.293
2016-03-01 12:57:03.500
2016-03-01 12:59:39.487
2016-03-01 12:59:43.443
2016-03-01 12:59:47.840
2016-03-01 12:59:51.410
2016-03-01 12:59:55.147
2016-03-01 12:59:57.137
2016-03-01 13:03:10.350
2016-03-01 13:03:12.450
2016-03-01 13:03:15.677
2016-03-01 13:03:17.813
2016-03-01 13:03:21.067
2016-03-01 13:03:24.127
2016-03-01 14:24:46.523
2016-03-01 14:24:52.347
2016-03-01 14:24:56.480
2016-03-01 14:24:59.363
2016-03-01 14:25:02.777
2016-03-01 14:25:05.640

我想要做的是选择ChangeNewValue,但前提是它是最新的时间戳。具体来说,我只想在ChangeNewValueQuarantined - DSC的情况下选择最新的时间戳。

请注意我使用的QlikView代码(失败)如下 -

Left keep
Table6:
Load
ChangeKeyValue as ITMSNumber,
IF(ChangeNewValue='Quarantined - DSC', date(Today()) - Floor(date(TIMESTAMP(ChangeDate, 'YYYY-MM-DD hh.mm.ss.fff'))));

SQL SELECT ChangeKeyValue,
ChangeNewValue,
ChangeDate
FROM MPL.dbo."tblCHANGE";

表格名称为MPL.dbo."tblCHANGE"

我请你在答案中使用我在这篇文章中添加的标签,以避免混淆。

3 个答案:

答案 0 :(得分:0)

SQL server / Oracle:

with CTE as
(
select a1.*, row_number() over(partition by ChangeFieldName order by TimeStampField) as R_ORD
from MyTable a1
)
select *
from CTE
where R_ORD = 1
and ChangeNewValue = 'Quarantined - DSC'

MySQL的:

select *
from 
(
select ChangeFieldName, max(TimeStampField) as maxField
from MyTable
group by ChangeFieldName
) a1
inner join MyTable a2
on a1.ChangeFieldName = a2.ChangeFieldName
and a1.maxField = a2.TimeStampField
and a2.ChangeNewValue = 'Quarantined - DSC'

答案 1 :(得分:0)

试试这个

SQL:
SQL SELECT ChangeKeyValue,
ChangeNewValue,
ChangeDate
FROM MPL.dbo."tblCHANGE";

left join (SQL)
Load max(ChangeDate) as Max_ChangeDate 
resident SQL
where ChangeNewValue='Quarantined - DSC';

Table6:
Load
ChangeKeyValue as ITMSNumber,
date(Today()) - Floor(date(TIMESTAMP(ChangeDate, 'YYYY-MM-DD hh.mm.ss.fff')))
resident SQL
where ChangeDate=Max_ChangeDate and ChangeNewValue='Quarantined - DSC';

drop field Max_ChangeDate; drop table SQL;

对于较少的代码,您也可以使用inner joinright join但这应该可以正常使用

答案 2 :(得分:0)

这就是为我做的。

SQL
    SELECT  ITMSNumber,Date_Chg,DATEDIFF(dd,Date_Chg,GETDATE()) AS tDays
    FROM    tblSTRATEGY Strgy
        inner join
          ( SELECT  ChangeKeyValue AS ITMS_Chg,MAX(ChangeDate) AS Date_Chg
            FROM    tblCHANGE Chg
            WHERE   ChangeFieldName='DATALOADSTATUSID'
                AND ChangeNewValue='Quarantined - DSC'
            GROUP BY ChangeKeyValue
        ) Chg1 ON Chg1.ITMS_Chg=Strgy.ITMSNumber
    WHERE   Strgy.DATALOADSTATUSID=30
    ORDER BY TDAYS;

我希望它有所帮助。