访问查询:太复杂了

时间:2013-03-12 21:40:25

标签: sql ms-access

我正在尝试将查询作为报告的一部分来运行,以确定是否需要发生事件。有许多组合,因为后续事件取决于第一和第二事件中的观察结果,这些组合因鱼类大小而异,并按物种分组。希望这提供了一些背景信息。我已经在这里搜索了一些问题,但是没有找到任何解决方案。这是代码的片段。代码大约是160行左右,大部分都包含在Switch函数中。谢谢你的任何想法!

SELECT headerid, species, ageclass, P1, P2, P3, P4, P5,
switch(ageclass in ('Subyearling', 'Yearling') and P3 is null and P1<4 and P2<1,    
'SHOCK',
ageclass in ('Subyearling', 'Yearling') and P3 is null and P1 between 4 and 5 and 
P2<2, 'SHOCK',
ageclass in ('Subyearling', 'Yearling') and P3 is null and P1 between 6 and 7 and   
P2<3, 'SHOCK',
ageclass in ('Subyearling', 'Yearling') and P3 is null and P1 between 8 and 9 and 
P2<4, 'SHOCK',

   ageclass like 'Subadult' and P3 is null and P1<5 and P2<1, 'SHOCK',
   ageclass like 'Subadult' and P3 is null and P1 between 5 and 8 and P2<2, 'SHOCK',
   ageclass like 'Subadult' and P3 is null and P1 between 9 and 11 and P2<3, 'SHOCK',
   ageclass like 'Subadult' and P3 is null and P1 between 12 and 13 and P2<4, 'SHOCK',
   ageclass like 'Subadult' and P3 is null and P1 between 14 and 16 and P2<5, 'SHOCK'

   as Event from data;

1 个答案:

答案 0 :(得分:0)

对于“和”,您可以分阶段进行这些查询。按第一个属性过滤,并在第二个属性上过滤结果。

对于“或”,您可以单独执行这些查询并加入结果。

编程逻辑是将问题分解为更小问题的艺术。因此,您需要将查询细分为较小的步骤并按顺序应用它们。它可能具有高复杂性,这些查询根据前面的信息分支不同,您应该分支算法逻辑,而不是在select语句中。