简体   繁体   English

什么是 function 将同一列中的 2 个值与条件相除

[英]What is the function to divide 2 values from the same column with conditions

I would like to return my total return divided by my total sale which are under the same column我想返回我的总回报除以我在同一列下的总销售额

select
(select count(order_type_id) from ods_emea_all.order_emea 
where order_type_id in ('Return', 'RETURN')
and brand_cd =('PB')
and iso_country_cd IN ('IT', 'ES', 'GB', 'FR', 'DE'))*100/
(select count(order_type_id )
from ods_emea_all.order_emea 
where order_type_id in ('Sale','SALE')
and brand_cd =('PB')
and iso_country_cd IN ('IT', 'ES', 'GB', 'FR', 'DE'))
 AS brand_return
from ods_emea_all.order_emea

If you have the following table with contents:如果您有下表的内容:

CREATE TABLE order_emea (
  order_type_id VARCHAR(100),
  brand_cd VARCHAR(100),
  iso_country_cd VARCHAR(100)
);

INSERT INTO order_emea VALUES
  ('Sale', 'PB', 'IT'),
  ('SALE', 'PB', 'FR'),
  ('Sale', 'PB', 'IT'),
  ('sale', 'PB', 'ES'),
  ('SALe', 'PB', 'ES'),
  ('sAle', 'PB', 'GB'),
  ('saLe', 'PB', 'FR'),
  ('sale', 'PB', 'DE'),
  ('Sale', 'PB', 'DE'),
  ('sale', 'PB', 'FR'),
  ('Return', 'PB', 'FR'),
  ('RETURN', 'PB', 'FR'),
  ('return', 'PB', 'GB'),
  ('REturn', 'PB', 'IT'),
  ('rEturn', 'PB', 'IT');

The following query will get you what you want:以下查询将为您提供所需的信息:

WITH base_table_n_returns_to_n_sales AS (
  SELECT
    SUM(CASE WHEN
      LOWER(order_type_id) = 'return'
      AND LOWER(brand_cd) = 'pb'
      AND LOWER(iso_country_cd) IN ('IT', 'ES', 'GB', 'FR', 'DE')
    THEN 1 ELSE 0 END) AS n_returns,
    SUM(CASE WHEN
      LOWER(order_type_id) = 'sale'
      AND LOWER(brand_cd) = 'pb'
      AND LOWER(iso_country_cd) IN ('IT', 'ES', 'GB', 'FR', 'DE')
    THEN 1 ELSE 0 END) AS n_sales
  FROM order_emea
)

SELECT
  *,
  n_returns / CAST(NULLIF(n_sales, 0) AS FLOAT) AS ratio_n_returns_to_n_sales
FROM base_table_n_returns_to_n_sales

See this fiddle .看到这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 使用子查询条件创建列 - SQL Create a column with conditions from a sub query SQL - GROUP BY 同一列的 3 个值 - SQL - GROUP BY 3 values of the same column 如何通过在 bigquery sql 中进行分组字符串比较来返回同一列中字符串值的差异? - How to return difference in string values from the same column by doing a grouped string comparison in bigquery sql? 如何对 SQL 中共享相同列名的 2 个单独表中的值求和 - How to SUM values from 2 separate tables that share the same column name in SQL SQL:从具有多个条件的表中查找值以返回最终计算 - SQL: lookup values from a table with multiple conditions to return final calculation CASE WHEN 错误 - 根据另一列的条件将值设置为列 - CASE WHEN error - setting value to column based on conditions from another column 如何在 BigQuery 中将所有列除以一列 - How to divide all columns by one column in BigQuery 如何计算同一列中两个值之间的百分比变化 - How to calculate percent change between two values in the same column BigQuery Standard SQL - 查询具有相同架构的 3 个不同表。 如何添加每行来自哪个表的列? - BigQuery Standard SQL - querying 3 different tables with same schema. How to add column with what table each row is from? 根据 BigQuery 中具有相同值的连续行创建条件列 - Creating a conditional column based on consecutive rows with same values in BigQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM