如何显示哪一列的最新日期?

时间:2018-06-27 19:13:23

标签: sql sql-server

我的雇主经常让我直接从我们的数据库中运行报表,而我似乎遇到了其中一个障碍。我需要确定索赔是登录到系统还是从系统注销,并且在表中有2个不同的列(Log_In_Date和Log_MAIL_Date),这些列跟踪我们所生成报告的一部分的索赔(Claimlog)。

CREATE TABLE CLAIMLOG(
   LOG_DEALER    VARCHAR(16) NOT NULL PRIMARY KEY
  ,LOG_IN_DATE   VARCHAR(19) NOT NULL
  ,LOG_BATCH     INTEGER  NOT NULL
  ,LOG_BSEQ      INTEGER  NOT NULL
  ,LOG_CLAIM     INTEGER  NOT NULL
  ,LOG_CSEQ      INTEGER  NOT NULL
  ,LOG_MAIL_DATE VARCHAR(19) NOT NULL
  ,LOG_NUMBER    INTEGER  NOT NULL
  ,LOG_TYPE      VARCHAR(1) NOT NULL
  ,LOG_DELETE    VARCHAR(1)
  ,LOG_REJECT    VARCHAR(1)
  ,LOG_CLAIM_SUB VARCHAR(2)
  ,LOG_RO_DATE   VARCHAR(19) NOT NULL
  ,LOG_ID        INTEGER  NOT NULL
);

INSERT INTO CLAIMLOG VALUES ('OH025',  '2017-06-01 00:00:00',533996,1, 682543,1,'2017-06-06 00:00:00',43,'M',NULL,NULL,NULL,'2017-05-30 00:00:00',2393818);
INSERT INTO CLAIMLOG VALUES ('OH025',  '2017-06-01 00:00:00',533995,1, 682581,1,'2017-06-08 00:00:00',90,'R',NULL,NULL,NULL,'2017-05-30 00:00:00',2393819);
INSERT INTO CLAIMLOG VALUES ('163369', '2017-01-30 00:00:00',486838,8, 117664,1,'2017-01-31 00:00:00',32,'M',NULL,NULL,NULL,'2017-01-27 00:00:00',2239381);
INSERT INTO CLAIMLOG VALUES ('132729', '2017-09-11 00:00:00',573238,13,239381,1,'2017-09-14 00:00:00',56,'M',NULL,NULL,NULL,'2017-09-07 00:00:00',2519220);
INSERT INTO CLAIMLOG VALUES ('08285',  '2018-05-14 00:00:00',671898,1, 239381,1,'2018-05-17 00:00:00',33,'M',NULL,NULL,NULL,'2018-05-10 00:00:00',2819892);
INSERT INTO CLAIMLOG VALUES ('06240',  '2013-11-15 00:00:00',780029,1, 239381,1,'2013-12-13 00:00:00',4, 'C',NULL,'X', 'A', '2013-11-11 00:00:00',944206);
INSERT INTO CLAIMLOG VALUES ('04839',  '2018-06-07 00:00:00',681150,1, 239381,2, NULL,                1, 'C',NULL,NULL,NULL,'2018-04-11 00:00:00',2785317);
INSERT INTO CLAIMLOG VALUES ('04839',  '2018-04-16 00:00:00',660798,1, 239381,1,'2018-04-30 00:00:00',53,'M',NULL,NULL,NULL,'2018-04-11 00:00:00',2785317);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533928,3, 387468,1,'2017-06-01 00:00:00',85,'M',NULL,NULL,NULL,'2017-05-30 00:00:00',2393813);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533928,2, 387462,1,'2017-06-01 00:00:00',85,'M',NULL,NULL,NULL,'2017-05-30 00:00:00',2393816);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533928,1, 387358,1,'2017-06-02 00:00:00',85,'M',NULL,NULL,NULL,'2017-05-26 00:00:00',2393814);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533927,2, 387458,1,'2017-06-01 00:00:00',4, 'R',NULL,NULL,NULL,'2017-05-30 00:00:00',2393811);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533927,1, 387290,1,'2017-06-01 00:00:00',4, 'R',NULL,NULL,NULL,'2017-05-24 00:00:00',2393810);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533926,2, 387257,1,'2017-06-01 00:00:00',4, 'C',NULL,NULL,NULL,'2017-05-24 00:00:00',2393815);
INSERT INTO CLAIMLOG VALUES ('01563-5','2017-05-31 00:00:00',533926,1, 386930,1,'2017-07-05 00:00:00',4, 'C',NULL,NULL,NULL,'2017-05-17 00:00:00',2393812);
INSERT INTO CLAIMLOG VALUES ('01429-4','2014-09-12 00:00:00',179427,2, 32051, 1,'2014-09-19 00:00:00',12,'M',NULL,NULL,NULL,'2014-09-04 00:00:00',1239381);
INSERT INTO CLAIMLOG VALUES ('01087',  '2015-02-20 00:00:00',239381,1, 45427, 1,'2015-02-24 00:00:00',11,'M',NULL,NULL,NULL,'2015-02-19 00:00:00',1431193);

我最初使用了一个case语句:

SELECT LOG_DEALER,
 LOG_CLAIM,
  CASE WHEN Log_In_Date > Log_Mail_Date THEN 'In' ELSE 'Out' END AS 'Log In Status'
FROM CHANGELOG

这在大多数情况下都是有效的,但是我遇到了一些重复的结果,其中“ In”和“ Out”返回。经过研究后,我将查询调整为:

SELECT CASE WHEN LogInDate > COALESCE(LogOutDate, '2000-01-01') THEN 'In' ELSE 'Out' END AS 'Log In Status'

表示空值。不幸的是,对于相同的条目,我仍然得到In和Out的返回:

  LOG_DEALER   LOG_CLAIM   LoggedInStatus  
 ------------ ----------- ---------------- 
  04839        239831      In              
  04839        239831      Out             
  01563-5      387468      Out    

我尝试将COALESCE替换为NOTNULL,并将select语句重写为:

SELECT IIF(LogInDate > LogOutDate, 'In','Out')

仍然没有运气获得结果。有人对如何正确查询此有任何建议吗?

2 个答案:

答案 0 :(得分:0)

根据示例数据和查询,LOG_MAIL_DATE具有约束NOT NULL。这样可以防止插入以下值:

    INSERT INTO #mytable VALUES ('04839',  '2018-06-07 00:00:00',681150,1, 239381,2, NULL,                1, 'C',NULL,NULL,NULL,'2018-04-11 00:00:00',2785317);

此外,插入的值具有LOG_DEALER ='04839'的重复行。默认情况下,SQL Server认为NULL为最大值。因此,对于两行,将发生以下结果:

  1. 此处,Log_In_Date ='2018-06-07 00:00:00'并且Log_Mail_Date = NULL 由于NULL>'2018-06-07 00:00:00',导致LoggedInStatus ='Out'

    INSERT INTO mytable VALUES ('04839',  '2018-06-07 00:00:00',681150,1, 239381,2, NULL,                1, 'C',NULL,NULL,NULL,'2018-04-11 00:00:00',2785317);
    
  2. 第二种情况产生Log_In_Date> Log_Mail_Date。因此,该条件满足并导致LoggedInStatus ='In'。

    INSERT INTO mytable VALUES ('04839',  '2018-04-16 00:00:00',660798,1, 239381,1,'2018-04-30 00:00:00',53,'M',NULL,NULL,NULL,'2018-04-11 00:00:00',2785317);
    

答案 1 :(得分:0)

尝试使用GROUP BY

SELECT LOG_DEALER,
 LOG_CLAIM,
  CASE WHEN MAX( Log_In_Date ) > COALESCE( MAX( Log_Mail_Date ), '2000-01-01') THEN 'In' ELSE 'Out' END AS 'Log In Status'
FROM #CLAIMLOG
GROUP BY LOG_DEALER, LOG_CLAIM

这将仅返回不同的LOG_DEALER, LOG_CLAIM组合。通过使用MAX,您将获得最新日期:

LOG_DEALER       LOG_CLAIM   Log In Status
---------------- ----------- -------------
01429-4          32051       Out
01087            45427       Out
163369           117664      Out
04839            239381      In
06240            239381      Out
08285            239381      Out
132729           239381      Out
01563-5          386930      Out
01563-5          387257      Out
01563-5          387290      Out
01563-5          387358      Out
01563-5          387458      Out
01563-5          387462      Out
01563-5          387468      Out
OH025            682543      Out
OH025            682581      Out

我也希望您不要将日期存储为VARCHAR