【问题标题】:SQL: How to count number of each item within categories in a tableSQL:如何计算表中类别内每个项目的数量
【发布时间】:2018-07-09 13:35:44
【问题描述】:

我有一个表格,其中显示了商店的名称和订单来自的各个城市,取自 PostgreSQL 中的 json 查询。它基于一项调查,在该调查中,我们还可以看到该调查被查看了多少次以及有多少回复。我正在尝试在表中创建另一列,该列基本上计算每个商店的每个城市的订单数量。 此处显示了一个示例列表,其中商店名称在左侧,城市在右侧:

 1. Glossier   |   New York       |    7    |    6
 2. Glossier   |   New York       |    7    |    6
 3. Glossier   |   Chicago        |    7    |    6
 4. Glossier   |   Boston         |    7    |    6
 5. Glossier   |   New York       |    7    |    6
 6. Glossier   |   Chicago        |    7    |    6
 7. Sephora    |   New York       |    10   |    8
 8. Sephora    |   Baltimore      |    10   |    8
 9. Sephora    |   New York       |    10   |    8
 10. Sephora   |   Boston         |    10   |    8
 11. Sephora   |   New York       |    10   |    8
 12. Sephora   |   Baltimore      |    10   |    8
 13. Sephora   |   Chicago        |    10   |    8
 14. Sephora   |   San Francisco  |    10   |    8

我一直使用的代码是:

WITH
    cities AS (
        SELECT context->>'city' AS city,
        context->>'shop_id' AS shopid
        FROM public.analytics_events
        ),
    views AS (
        SELECT
            DISTINCT ON (ref_id) ref_id,
            CASE WHEN context->>'order_total' IS NULL THEN 0
            ELSE cast(context->>'order_total' AS NUMERIC)
            END AS total,
                context->>'shop_id' AS shop_id
        FROM public.analytics_events
        ),
    view_counts AS (
            SELECT COUNT(DISTINCT ref_id) AS views, SUM(total)
            AS volume, shop_id
            FROM views
            GROUP BY shop_id
        ),
    response_counts AS (
            SELECT COUNT(survey_responses.id) AS responses,
            surveys.shop_id AS shop_id, surveys.question, 
            surveys.id AS surveyid
            FROM public.survey_responses
            LEFT JOIN public.surveys
            ON surveys.id = survey_responses.survey_id
            GROUP BY surveys.shop_id, surveys.question, surveyid
        )
    SELECT
        name,
        CASE WHEN views IS NULL THEN 0 ELSE views END,
        CASE WHEN responses IS NULL THEN 0 ELSE responses END,
        city,
        COUNT(*)
    FROM public.shops
    LEFT JOIN view_counts ON shops.id = view_counts.shop_id
    LEFT JOIN response_counts ON shops.id = response_counts.shop_id
    LEFT JOIN cities on shops.id = cities.shopid
    GROUP BY name, city;

但是,我现在被困在如何创建下一步。

【问题讨论】:

  • 什么是“订单”?你想要什么结果?
  • 您确实需要告诉我们您使用的是哪个数据库,因为 JSON API 差异很大。

标签: sql postgresql


【解决方案1】:

我怀疑你只是想要group by:

WITH cities AS (
      SELECT context->>'city' AS city, context->>'shop_id' AS shopid
      FROM analytics
     )
SELECT name, city, COUNT(*)
FROM shops LEFT JOIN
     cities 
     ON shops.id = cities.shopid
GROUP BY name, city;

【讨论】:

  • 认为这无关紧要,但我正在尝试同时利用调查视图和响应中的数据,并且我不断收到错误“列“view_counts.views”必须出现在组中BY 子句或在聚合函数中使用”,不知道如何解释
  • @JuliaMartin。 . .此查询没有引用 view_countsview_counts.views。也许你应该问另一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多