0

我有以下要转换为视图的查询:

SELECT
  PartNum,
  SUM(IF(DAYOFWEEK(DeliveryDate) = '2', value, NULL)) AS 'Mon',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '3', value, NULL)) AS 'Tue',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '4', value, NULL)) AS 'Wed',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '5', value, NULL)) AS 'Thu',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '6', value, NULL)) AS 'Fri',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '7', value, NULL)) AS 'Sat',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '1', value, NULL)) AS 'Sun',
  SUM(IF(DeliveryDate > DATE_ADD(CURDATE(),INTERVAL 7 DAY), value, NULL)) AS 'Future'
FROM (
  SELECT PartNum, DeliveryDate , SUM(Ordered) value FROM v_archived_items_due
    GROUP BY PartNum, DeliveryDate
    ) t
  GROUP BY PartNum;

当我尝试将其另存为视图时,出现以下错误:

1349 - 视图 SELECT 在 FROM 子句中包含子查询。

查询本身可以正常工作。我如何把它变成一个视图?

4

2 回答 2

0

MySQL 不允许查看子查询,因此没有看到任何示例数据以了解如何在没有子查询的情况下对其进行返工。由于这按预期工作,我将创建您的子查询的视图:

create view view1 as
    SELECT PartNum, DeliveryDate , SUM(Ordered) value 
    FROM v_archived_items_due
    GROUP BY PartNum, DeliveryDate;

然后只需在查询中调用此视图:

SELECT
  PartNum,
  SUM(IF(DAYOFWEEK(DeliveryDate) = '2', value, NULL)) AS 'Mon',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '3', value, NULL)) AS 'Tue',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '4', value, NULL)) AS 'Wed',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '5', value, NULL)) AS 'Thu',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '6', value, NULL)) AS 'Fri',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '7', value, NULL)) AS 'Sat',
  SUM(IF(DAYOFWEEK(DeliveryDate) = '1', value, NULL)) AS 'Sun',
  SUM(IF(DeliveryDate > DATE_ADD(CURDATE(),INTERVAL 7 DAY), value, NULL)) AS 'Future'
FROM view1
GROUP BY PartNum;
于 2012-12-18T17:23:41.653 回答
0

按设计行事

http://dev.mysql.com/doc/refman/5.1/en/create-view.html

您必须从子查询中构建一个视图并使用它。

于 2012-12-18T17:24:13.207 回答