是否可以以编程方式编辑SQLite CREATE VIEW语句?

时间:2015-10-18 07:48:13

标签: sql sqlite livecode

我有一个包含许多嵌套视图的视图,如下所示。

主要观点:

CREATE VIEW `qryAttackRate` AS 
Select qryFoodInCases.fldCaseID,qryFoodInCases.fldFood,
AteAndGotSick,TotalAte,AteAttackRate,
NotAteAndGotSick,TotalNotAte,NotAteAttackRate,
ROUND(AteAttackRate/CAST(NotAteAttackRate AS FLOAT),2) RelativeRisk 
FROM qryFoodInCases
LEFT JOIN qryNotAteAttackRate QA
ON qryFoodInCases.fldFood=QA.fldFood
LEFT JOIN qryAteAttackRate QN
ON qryFoodInCases.fldFood=QN.fldFood
GROUP BY qryFoodInCases.fldFood
ORDER BY  RelativeRisk Desc

这是2个子视图。还有一些:

CREATE VIEW `qryAteAttackRate` 
AS SELECT qryFoodInCases.fldCaseID,qryFoodInCases.fldFood,
COALESCE(qryAteAndGotSick.AteAndGotSick,0) AteAndGotSick,
qryFoodInCases.fldFoodFrequency TotalAte,
 100*COALESCE(qryAteAndGotSick.AteAndGotSick,0)/
qryFoodInCases.fldFoodFrequency AteAttackRate
FROM qryFoodInCases
LEFT JOIN qryAteAndGotSick 
ON qryFoodInCases.fldFood=qryAteAndGotSick.fldFood
GROUP BY qryFoodInCases.fldFood


CREATE VIEW `qryFoodInCases` 
AS SELECT tblCases.fldCaseID,fldfood,
COUNT(tblFoodHistory.fldFoodID) AS fldFoodFrequency 
FROM tblFood 
INNER JOIN tblFoodHistory 
ON tblFoodHistory.fldFoodID=tblFood.fldFoodID)
INNER JOIN tblMealHistory 
ON tblFoodHistory.fldMealID=tblMealHistory.fldMealHistoryID)
INNER JOIN tblInterviews 
ON tblInterviews.fldInterviewID=tblMealHistory.fldInterviewID)
INNER JOIN tblCases 
ON tblCases.fldCaseID=tblInterviews.fldCaseID
GROUP BY tblCases.fldCaseID,tblFood.fldFood

我想查询每个fldCaseID的主视图。问题是,查询主视图时的WHERE条件不适用于子视图。

除了创建一个冗长的复杂SQL语句之外,有没有办法以编程方式将WHERE条件插入到子视图中?

为了说明问题,请在添加第二个案例之前和之后查看select * from qryAttackRate where fldcaseid=1的输出,而不对与第一个案例相关的数据进行任何更改 之前

+-----------+------------+---------------+----------+---------------+------------------+-------------+------------------+--------------+
    | fldCaseID |  fldFood   | AteAndGotSick | TotalAte | AteAttackRate | NotAteAndGotSick | TotalNotAte | NotAteAttackRate | RelativeRisk |
    +-----------+------------+---------------+----------+---------------+------------------+-------------+------------------+--------------+
    |         1 | Beans      |             4 |        4 |           100 |                1 |           3 |               33 | 3.03         |
    |         1 | Cabagge    |             2 |        3 |            66 |                3 |           4 |               75 | 0.88         |
    |         1 | fried fish |             2 |        3 |            66 |                3 |           4 |               75 | 0.88         |
    |         1 | Banana     |             1 |        2 |            50 |                4 |           5 |               80 | 0.62         |
    |         1 | Pork       |             2 |        4 |            50 |                3 |           3 |              100 | 0.5          |
    |         1 | Chicken    |             1 |        3 |            33 |                4 |           4 |              100 | 0.33         |
    |         1 | Potatoes   |             0 |        2 |             0 |                5 |           5 |              100 | 0            |
    |         1 | Rice       |             0 |        2 |             0 |                5 |           5 |              100 | 0            |
    +-----------+------------+---------------+----------+---------------+------------------+-------------+------------------+--------------+

+-----------+------------+---------------+----------+---------------+------------------+-------------+------------------+--------------+
| fldCaseID |  fldFood   | AteAndGotSick | TotalAte | AteAttackRate | NotAteAndGotSick | TotalNotAte | NotAteAttackRate | RelativeRisk |
+-----------+------------+---------------+----------+---------------+------------------+-------------+------------------+--------------+
|         1 | Cabagge    |             2 |        3 |            66 |                4 |           7 |               57 | 1.16         |
|         1 | fried fish |             2 |        3 |            66 |                4 |           7 |               57 | 1.16         |
|         1 | Pork       |             2 |        4 |            50 |                4 |           6 |               66 | 0.76         |
|         1 | Potatoes   |             0 |        2 |             0 |                6 |           8 |               75 | 0            |
+-----------+------------+---------------+----------+---------------+------------------+-------------+------------------+--------------+

1 个答案:

答案 0 :(得分:0)

视图始终在应用外部查询中的任何处理之前计算固定结果集。 (数据库实际上试图在视图中优化掉不需要的处理,但它总是表现得像预先实现了视图。)

无法更改视图(使用参数或任何其他机制)。

如果子查询必须动态更改,则无法使用视图。 您必须将它们编写为子查询,或者(在SQLite 3.8.3或更高版本中)将其写为common table expressions