单行子查询返回多个行。
这是我在运行以下雪花代码时遇到的错误。 我知道这是因为TO_VARCHAR函数不适用于整个表列。.关于如何解决并将其应用于整个列的任何想法吗?
SELECT sv.SKU
, li.price
, li.cost_price
, pc.product_description
FROM raw.stitch_heroku.spree_line_items as li
LEFT OUTER JOIN (SELECT *
FROM raw.stitch_heroku.spree_variants
WHERE LENGTH(raw.stitch_heroku.spree_variants.SKU) Between 4 and 5) as sv
ON li.variant_id = sv.id
LEFT JOIN (SELECT analytics.dbt_lcasucci.product_category.product_description
, TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID)
FROM analytics.dbt_lcasucci.product_category) as pc
ON (SELECT TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID)
FROM analytics.dbt_lcasucci.product_category) = sv.id
GROUP BY sv.SKU,li.price,li.cost_price,pc.product_description
我必须将名为analytics.dbt_lcasucci.products
的表的数据类型从int更改为varchar,因为如果不这样做,联接将无法识别这些字段,并且即使单元格的实际内容为匹配。
答案 0 :(得分:0)
您在ON子句中有错误的子查询。
SELECT sv.SKU
, li.price
, li.cost_price
, pc.product_description
FROM raw.stitch_heroku.spree_line_items as li
LEFT OUTER JOIN (
SELECT *
FROM raw.stitch_heroku.spree_variants
WHERE LENGTH(raw.stitch_heroku.spree_variants.SKU) Between 4 and 5
) as sv ON li.variant_id = sv.id
LEFT JOIN (
SELECT analytics.dbt_lcasucci.product_category.product_description
, TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID) my_key
FROM analytics.dbt_lcasucci.product_category
) as pc ON pc.my_key = sv.id
并且没有聚集功能..您不需要分组依据。如果不想重复行,最终请使用distinct
ON (SELECT TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID)
FROM analytics.dbt_lcasucci.product_category) = sv.id
答案 1 :(得分:0)
您需要将多个值表达式TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID)
转换为单个值表达式。
使用任何聚合功能,例如MIN()
,MAX()
,AVG()
,SUM()
等
例如,您可以按以下方式使用MAX(TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID))
:
SELECT
sv.SKU,
li.price,
li.cost_price,
pc.product_description
FROM raw.stitch_heroku.spree_line_items as li
LEFT OUTER JOIN (
SELECT *
FROM raw.stitch_heroku.spree_variants
WHERE LENGTH(raw.stitch_heroku.spree_variants.SKU) Between 4 and 5
) as sv ON li.variant_id = sv.id
LEFT JOIN (
SELECT analytics.dbt_lcasucci.product_category.product_description,
TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID)
FROM analytics.dbt_lcasucci.product_category
) as pc ON (
SELECT MAX(TO_VARCHAR(analytics.dbt_lcasucci.product_category.product_ID))
FROM analytics.dbt_lcasucci.product_category
) = sv.id
GROUP BY sv.SKU, li.price, li.cost_price, pc.product_description