我正在为产品的位置制定一个定义的案例,然后希望稍后将该案例作为值返回。但是,我收到错误“'字段列表'中的未知列位置。”
SELECT location, avg(pcnt), max(pcnt), min(pcnt)
FROM (
-- create temporary table 'a' to serve data from inner typing table created below
SELECT a.date_key, a.num_snowflakes/k.tot_snowflakes AS pcnt
FROM (
SELECT
date_key,
CASE
WHEN location_type_key=2 then 'at_customer'
WHEN location_type_key IN (1,13) then 'on_floor'
WHEN location_type_key IN (6,7,11) then 'sat1'
WHEN location_type_key IN (4,5) then 'film'
WHEN location_type_key IN (8,9) then 'repair'
WHEN location_type_key IN (12) then 'inspection'
WHEN location_type_key IN (2) then 'outside'
ELSE NULL END AS location,
COUNT(distinct garment_key) AS num_snowflakes,
COUNT(distinct product_key) AS num_skus
FROM database
-- excluding vendor return and retired
WHERE location_type_key NOT IN (3,10)
-- between 6/15/13 and 8/15/13
AND date_key >= 20130615 AND date_key <= 20130815
-- aggregates by day and location case
GROUP BY date_key, location
) AS a
JOIN
-- create temporary table 'k' to serve 'tot_snowflakes' to table 'a' above
(SELECT b.date_key, sum(b.num_snowflakes) AS tot_snowflakes
FROM (
SELECT
date_key,
CASE
WHEN location_type_key=2 then 'at_customer'
WHEN location_type_key IN (1,13) then 'on_floor'
WHEN location_type_key IN (6,7,11) then 'sat1'
WHEN location_type_key IN (4,5) then 'film'
WHEN location_type_key IN (8,9) then 'repair'
WHEN location_type_key IN (12) then 'inspection'
WHEN location_type_key IN (2) then 'outside'
ELSE NULL END AS location,
COUNT(distinct garment_key) AS num_snowflakes,
COUNT(distinct product_key) AS num_skus
FROM database
WHERE location_type_key NOT IN (3,10)
-- between 6/15/13 and 8/15/13
AND date_key >= 20130615 AND date_key <= 20130815
-- aggregates by day and location case
GROUP BY date_key, location
) AS b
) AS k
ON a.date_key=k.date_key
GROUP BY a.date_key, a.location
) AS z
我试图确保我总是调用正确的表格,所以我不确定错误来自哪里。有人有什么想法吗?
谢谢!
答案 0 :(得分:0)
您最外面的查询SELECTs FROM table z。
表z源自SELECT FROM表和JOIN表k。
表a派生自SELECT FROM表“database”。
表k派生自SELECT FROM派生表b。
表b派生自SELECT FROM基表“database”。
这里的简短回答是,您的查询的第一行实际上是在询问:
SELECT z.location, avg(z.pcnt), max(z.pcnt), min(z.pcnt)
派生表z只有两列,“date_key”和“pcnt”。
(SELECT a.date_key, a.num_snowflakes/k.tot_snowflakes AS pcnt
...
) AS z
要使查询起作用,表z需要一个位置,该位置必须在每个嵌套选择中都是显式的,直到您可以将其追溯到您想要实际读取它的基表。
看起来对于你想要完成的结果来说,查询的工作量超过了必要的工作量,但我一直盯着它,直到我的眼睛失去了专注的能力,所以我会坚持目前这个答案:每个需要将其“位置”列向上渗透到最外层选择的派生表需要明确地选择它。