如何将事件类型限制为每天一次

发布于 2024-10-30 16:59:35 字数 1177 浏览 0 评论 0原文

我有一个像这样的查询..它不起作用,但是,我在 WHERE NOT EXISTS 位上收到语法错误。然而我不确定它是否会按照我想要的方式工作..我想做的是让它在任何一天只能有一个“B”,而且这将是第一个实例..但我不确定下面的代码是否有效,无论如何,直到最后一切都会是“B”。

UPDATE FoodIntake
 SET MealTypeCode = CASE

WHEN substr(DateTime, -8)
 BETWEEN (SELECT BreakfastStart FROM Patient WHERE PatientId = :PatientId)
 AND (SELECT LunchStart FROM Patient WHERE PatientId = :PatientId)
 AND CarbAmount >= 25
 AND MealTypeCode IS NULL
 WHERE NOT EXISTS (
    SELECT * FROM FoodIntake as Old 
    WHERE MealTypeCode = 'B' 
    AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10)
)
 THEN 'B'
END

另一种选择,我不确定如何处理,是将所有适用的内容标记为 B,然后进行第二次查询以查找一天内有 2 个 B 的任何时间,并将第二个选项重命名为其他名称。

我尝试过一个查询,但我知道它有一些主要问题..有一些语法错误我不太清楚,但我认为它至少能满足我想要做的事情。

UPDATE FoodIntake
 SET MealTypeCode = 'U'
WHERE MealTypeCode = 'B'
AND count(SELECT 1 FROM FoodIntake as Old WHERE MealTypeCode ='B' AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10) ) > 1
AND FoodIntake.DateTime > (SELECT DateTime FROM FoodIntake as Old WHERE MealTypeCode ='B' AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10) ORDER By DateTime ASC LIMIT = count-1)

I have a query like so.. it's not working, though, I'm getting a syntax error on the WHERE NOT EXISTS bit. However I'm not sure it would work the way I'd want it to anyway.. What I want to do is make it so there can only be one 'B' in any day, and it will be the first instance.. but I'm not sure if the below code worked, that anything would be 'B' until the very end anyway.

UPDATE FoodIntake
 SET MealTypeCode = CASE

WHEN substr(DateTime, -8)
 BETWEEN (SELECT BreakfastStart FROM Patient WHERE PatientId = :PatientId)
 AND (SELECT LunchStart FROM Patient WHERE PatientId = :PatientId)
 AND CarbAmount >= 25
 AND MealTypeCode IS NULL
 WHERE NOT EXISTS (
    SELECT * FROM FoodIntake as Old 
    WHERE MealTypeCode = 'B' 
    AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10)
)
 THEN 'B'
END

The other option, that I'm not sure how to approach would be to tag everything that applies as B, then do a second query to find any time there are 2 Bs in one day and rename the second one as something else.

I have a query I tried for that but I know it's got some major problems.. there's some syntax errors I can't quite figure out, but I think it at least gets across what I want to do.

UPDATE FoodIntake
 SET MealTypeCode = 'U'
WHERE MealTypeCode = 'B'
AND count(SELECT 1 FROM FoodIntake as Old WHERE MealTypeCode ='B' AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10) ) > 1
AND FoodIntake.DateTime > (SELECT DateTime FROM FoodIntake as Old WHERE MealTypeCode ='B' AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10) ORDER By DateTime ASC LIMIT = count-1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2024-11-06 16:59:35

我认为您最好使用 IF ... ELSE 语句来实现此目的,虽然可能更冗长,但会更明确地说明此处发生的逻辑。

IF (SELECT COUNT(1) FROM FoodIntake 
    WHERE (date('now') BETWEEN date('now','start of day') 
    AND date('now','start of day','+1 day') )
    AND MealTypeCode = 'B') > 0

 -- Make update based on 'B' code already taken

ELSE

 -- Make update based on 'B' code not yet taken

我不是一个 SQLite 人员,但我很擅长 mySQL 和 T-SQL,所以如果日期函数不准确,我参考了这篇文章: http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions

I think you'd be better off using an IF ... ELSE statement for this, which while perhaps more verbose will more explicitly state the logic occurring here.

IF (SELECT COUNT(1) FROM FoodIntake 
    WHERE (date('now') BETWEEN date('now','start of day') 
    AND date('now','start of day','+1 day') )
    AND MealTypeCode = 'B') > 0

 -- Make update based on 'B' code already taken

ELSE

 -- Make update based on 'B' code not yet taken

I am not a SQLite guy, but I am good with mySQL and T-SQL, so if the date functions are inaccurate, I was referencing this article: http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文