title: Unknown Column Error (1054) for a Query in a Stored Procedure, that Works in the Query Window
body: <p>I have a strange issue in MySQL.  I wrote a query that aggregates transnational data into hourly chunks.  The query works great in Workbench query window.  I then implemented the query as a stored procedure and I get a 'Error Code: 1054. Unknown column 'd.SiteId' in 'field list'' message.</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>The query in the stored procedure:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Thanks
  Geordie.</p>