mysql—存储过程中查询的未知列错误(1054),该错误在查询窗口中工作

pod7payv  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(252)

我在mysql中遇到了一个奇怪的问题。我编写了一个查询,将跨国数据聚合成每小时一次的数据块。在workbench查询窗口中,该查询非常有效。然后我将查询实现为一个存储过程,得到一个“错误代码:1054”。“字段列表”消息中的未知列“d.siteid”。

SELECT  `d`.`SiteId`, `d`.`TransactionDate`, (`d`.`TimeGrouping` * 10000) as 'HourEndTime', Sum(`d`.`SalesAmount`) as 'TotalSales', count(`d`.`PosTransactionId`) as 'TransactionCount', Sum(`d`.`ItemCount`) as 'ItemCount', `d`.`Currency`
FROM    (SELECT DISTINCT `SiteId`, `TransactionDate`, `TimeGrouping`, `SalesAmount`, `PosTransactionId`, `ItemCount`, `Currency`
        FROM PosSalesDev.Transaction
        WHERE `SiteId` = 31
        AND `TransactionDate` = '2018-06-08') AS `d`
        GROUP BY TransactionDate, TimeGrouping
ORDER BY HourEndTime;

存储过程中的查询:

CREATE DEFINER=`user`@`%` PROCEDURE `TransactionGet`(siteId varchar(10),
                                    transactionDate Date)
BEGIN

# Query will remove duplicate records from the aggregated data

SELECT  `d`.`SiteId`, `d`.`TransactionDate`, (`d`.`TimeGrouping` * 10000) as 'HourEndTime', Sum(`d`.`SalesAmount`) as 'TotalSales', count(`d`.`PosTransactionId`) as 'TransactionCount', Sum(`d`.`ItemCount`) as 'ItemCount', `d`.`Currency`
FROM    (SELECT DISTINCT `SiteId`, `TransactionDate`, `TimeGrouping`, `SalesAmount`, `PosTransactionId`, `ItemCount`, `Currency`
        FROM PosSalesDev.Transaction
        WHERE `SiteId` = siteId
        AND `TransactionDate` = transactionDate) AS `d`
GROUP BY TransactionDate, TimeGrouping;

END

任何帮助都将不胜感激。
谢谢你,乔迪。

d4so4syb

d4so4syb1#

避免将参数命名为列名。见13.6.4.2局部变量范围和分辨率。
尝试:

DELIMITER //

CREATE DEFINER=`user`@`%` PROCEDURE `TransactionGet`(
  _siteId varchar(10),
  _transactionDate Date
)
BEGIN

# Query will remove duplicate records from the aggregated data

SELECT
  `d`.`SiteId`,
  `d`.`TransactionDate`,
  (`d`.`TimeGrouping` * 10000) as 'HourEndTime',
  Sum(`d`.`SalesAmount`) as 'TotalSales',
  count(`d`.`PosTransactionId`) as 'TransactionCount',
  Sum(`d`.`ItemCount`) as 'ItemCount',
  `d`.`Currency`
FROM
  (SELECT
     DISTINCT `SiteId`,
     `TransactionDate`,
     `TimeGrouping`,
     `SalesAmount`,
     `PosTransactionId`,
     `ItemCount`,
     `Currency`
   FROM
     PosSalesDev.Transaction
   WHERE
     `SiteId` = _siteId AND
     `TransactionDate` = _transactionDate
) AS `d`
GROUP BY
  `d`.`TransactionDate`,
  `d`.`TimeGrouping`;
END//

DELIMITER ;

相关问题