现有一个数据表的数据像这样:
我们的需求是基于表中的Item列新增一个status字段,如果item的内容是第一次出现,那么status列就是New,反之显示Old,
比如,应该是下面的显示内容
在原文章的回复中, Robert Cemper给出了下面的建议
新建一个存储过程,并在SELECT查询语句中生效:
/// Return NEW for first occurance of item
/// otherwise return OLD
Class User.ItemStat Extends %RegisteredObject
{
ClassMethod NewOld(item As %String = "") As %String [ SqlProc ]
{
if item="" quit "?"
if $d(^||list(item)) quit "OLD"
if $i(^||list(item)) quit "NEW"
}
}
ObjectScriptObjectScript
使用下面SELECT语句使之生效:
SELECT *, ItemStat_NewOld(item) as Status
FROM items order by 2
SQLSQL
结果如下:
ID date item Status
1 09/13/1932 A NEW
2 04/06/1933 D NEW
10 06/15/1940 A OLD
4 11/26/1940 A OLD
6 02/19/1956 B NEW
8 04/22/1957 D OLD
7 05/01/1959 D OLD
9 06/29/1961 ?
3 07/04/1992 B OLD
5 12/08/2020 D OLD
ObjectScriptObjectScript