如何将事件类型限制为每天一次
我有一个像这样的查询..它不起作用,但是,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您最好使用
IF ... ELSE
语句来实现此目的,虽然可能更冗长,但会更明确地说明此处发生的逻辑。我不是一个 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.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