多个Case语句在查询中

huangapple go评论131阅读模式
英文:

multiple Case statement in a query

问题

这个语句出错的原因是因为 SQL 编辑器遇到了一些不符合语法规则的内容,导致无法正确解析查询。你可以检查以下部分,看看是否需要修复:

  1. 在这两行中,你使用了 ' 来表示单引号:not ACT.ActivityTime_Date like '2019-09-%'。SQL 中通常使用单引号 ' 表示字符串,而不是 '。你可以将这些地方修改为 ',如:not ACT.ActivityTime_Date like '2019-09-%'

  2. 在查询的最后,你使用了 from Analytics.ConnectTodayActive ACT; 来指定表格。确保表格 ConnectTodayActive 存在并且已正确命名。

  3. 检查语法,确保查询的结构是正确的。如果仍然有问题,可能需要进一步检查整个查询以查找其他潜在的语法错误。

这些修复可能会解决你的问题。如果还有其他问题,请提供更多详细信息,以便更好地帮助你。

英文:

Can you please tell me why this statement is wrong, it is giving me an error that it was expecting something else instead of the case phrase

  1. SELECT ACT.Employee_Id,
  2. case when max( case when not ACT.ActivityTime_Date like '2019-09-%' and ACT.Activity_Time_Date not like '09/%/2019' and
  3. not (ACT.Action like 'ACTIVITY_create_%' or ACT.Action like 'ACTIVITY_update_%') and
  4. not ACT.Destination_ObjectType in ('announcement', 'attachment'
  5. ,'blog','blogpost','community', 'directmessage', 'document', 'event', 'idea', 'message', 'poll', 'project', 'question',
  6. 'socialgroup', 'task', 'thread', 'video', 'wallentry')) then 1 else 0
  7. END)=0
  8. then 'Contributor'
  9. from Analytics.ConnectTodayActive ACT;

This is the error

  1. SQL Editor 1: Encountered "case when max ( case when not ( ACT . ActivityTime_Date like \'2019-09-%\' or ACT . ActivityTime_Date like \'09/%/2019\' ) and not ( ACT . Action like not.
  2. Was expecting one of:
  3. "column" ...
  4. "row" ...
  5. "transactiontime" ...
  6. "validtime" ...
  7. "(" ...
  8. "until_changed" ...
  9. "until_closed" ...
  10. <NAME> ...
  11. "sampleid" ...
  12. "interval" ...
  13. "date" ...
  14. "time" ...
  15. "timestamp" ...
  16. "begin" ...
  17. "end" ...
  18. "next" ...
  19. <SINGLE_STRING_LITERAL> ...
  20. <INTEGER_LITERAL> ...
  21. <FLOATING_POINT_LITERAL> ...
  22. ":" ...
  23. "?" ...
  24. <NAMED_QUESTIONMARK> ...
  25. "@" ...
  26. "null" ...
  27. "user" ...
  28. "current_role" ...
  29. "current_user" ...
  30. "session" ...
  31. "account" ...
  32. "database" ...
  33. "role" ...
  34. "profile" ...
  35. "zone" ...
  36. "td_host" ...
  37. "td_authid" ...
  38. "new" ...
  39. "current_date" ...

答案1

得分: 2

你的查询似乎格式不正确:

  • 你外部的 CASE 语句缺少一个 END
  • 一个括号放置不当。
  • 如果你想返回 employee_id,你需要使用 GROUP BY,因为你使用了 MAX

你应该能够合并你的 LIKE 条件,所以类似这样:

  1. SELECT
  2. ACT.Employee_Id,
  3. CASE
  4. WHEN MAX (
  5. CASE
  6. WHEN
  7. ACT.ActivityTime_Date NOT LIKE ALL('2019-09-%','09/%/2019') AND
  8. ACT.Action NOT LIKE ALL ('ACTIVITY_create_%','ACTIVITY_update_%') AND
  9. ACT.Destination_ObjectType NOT IN ('announcement','attachment','blog','blogpost','community', 'directmessage', 'document', 'event','idea', 'message', 'poll', 'project', 'question', 'socialgroup', 'task', 'thread', 'video', 'wallentry')
  10. THEN 1
  11. ELSE 0
  12. END
  13. ) = 0 THEN 'Contributor'
  14. END AS my_col
  15. FROM Analytics.ConnectTodayActive ACT
  16. GROUP BY ACT.Employee_Id;

SQL Fiddle (Postgres)

英文:

Your query doesn't seem to be formatted correctly:

  • your outer CASE statement is missing an END
  • an end parenthesis is out of place
  • if you want to return employee_id, you need to use a GROUP BY since you're using MAX

You should be able to combine your LIKE conditions too, so something like this:

  1. SELECT
  2. ACT.Employee_Id,
  3. CASE
  4. WHEN MAX (
  5. CASE
  6. WHEN
  7. ACT.ActivityTime_Date NOT LIKE ALL('2019-09-%','09/%/2019') AND
  8. ACT.Action NOT LIKE ALL ('ACTIVITY_create_%','ACTIVITY_update_%') AND
  9. ACT.Destination_ObjectType NOT IN ('announcement','attachment','blog','blogpost','community', 'directmessage', 'document', 'event','idea', 'message', 'poll', 'project', 'question', 'socialgroup', 'task', 'thread', 'video', 'wallentry')
  10. THEN 1
  11. ELSE 0
  12. END
  13. ) = 0 THEN 'Contributor'
  14. END AS my_col
  15. FROM Analytics.ConnectTodayActive ACT
  16. GROUP BY ACT.Employee_Id;

SQL Fiddle (Postgres)

huangapple
  • 本文由 发表于 2020年1月6日 21:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定