我使用 SQL 来构建我自己的仪表板和图表,有时我会重新使用或合并一些请求,以便向某些图表(国家、城市等)添加一些细节我目前正在研究潜望镜中的 sql 图表,SQL我在这里使用的查询(CTE)完美地工作:
--yes-cache
WITH order_composition AS (
select
o.created_at::date as date,
case when (product_category.name in (...) and brand not in (...) and brand is not
null) then 'CORNERS'
when (product_category.name in (...) and brand in (...)) then '..'
else '..' end business_type,
sum(quantity*product.price) revenue
from "order" as o
inner join product on order_item.product_id = product.id
left join product_category on product.category = product_category.id
where (o.created_at::date = current_date or o.created_at::date = (current_date -
interval '1 day') or o.created_at::date = (current_date - interval '1 week'))
and [depot_id=Dépot]
group by 1, 2
)
但是当我添加这个 CTE 时,然后合并两个 CTE:
--yes_cache
WITH all_orders AS (
SELECT
depot_id, id,
CASE
WHEN ... THEN ...
END geographical_area
FROM (
SELECT
depot_id, id,
CAST(LEFT(address->>'zipCode', 2) AS INTEGER) trunc_zip_code,
address->>'country' as country
FROM "order" o
where (o.created_at::date = current_date or o.created_at::date = (current_date - interval '1 day') or o.created_at::date = (current_date - interval '1 week'))
AND cancel_date is null
AND depot_id IS NOT NULL
GROUP BY 1, 2, 3, 4
) as init
GROUP BY 1, 2, 3
),
(
[ + the CTE that i showed you above ]
)
SELECT *
FROM order_composition
LEFT JOIN all_orders USING(id)
WHERE geographical_area = 'Paris & région parisienne'
此问题错误:错误:运算符不存在:字符变化->>“未知”提示:没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。
某些列的性质可能存在一些问题,但我不知道是哪一个。