简体   繁体   English

在 SQL 中,如何使用 left 语句计算新创建的别名的不同值

[英]In SQL, how do you count distinct values of a newly created alias with a left statement

I probably didn't word the title properly, but basically I've created a column and I'm trying to count the distinct values within that column:我可能没有正确地用词标题,但基本上我已经创建了一个列,并且我正在尝试计算该列中的不同值:

SELECT
    LEFT (route_id, 7) AS unique_route_plan,
    route_id,
    station_code,
    country_code,
    plan_wave
FROM
    perfectmile_na.d_perfectmile_routing
WHERE
    date BETWEEN '2020-01-01 00:00:00'
    AND '2020-12-31 00:00:00'
    AND station_code LIKE 'D%'
    AND plan_planning_algo <> 'DYNAMIC-PLANNER'

So in the above, I'd like to know how many distinct values of unique_route_plan there are.所以在上面,我想知道 unique_route_plan 有多少不同的值。

Appreciate any help!感谢任何帮助!

You can use the main query in a subquery like this and then do a count of distinct.您可以像这样在子查询中使用主查询,然后进行不同的计数。

select count(distinct unique_route_plan) from
(SELECT
    LEFT (route_id, 7) AS unique_route_plan,
    route_id,
    station_code,
    country_code,
    plan_wave
FROM
    perfectmile_na.d_perfectmile_routing
WHERE
    date BETWEEN '2020-01-01 00:00:00'
    AND '2020-12-31 00:00:00'
    AND station_code LIKE 'D%'
    AND plan_planning_algo <> 'DYNAMIC-PLANNER')

You don't say how you want your data or provide an example of what you're looking for, however presumably you want a count of the distinct values as a column in the same query, so you can do您没有说出您想要数据的方式或提供您正在寻找的示例,但是大概您希望将不同值的计数作为同一查询中的列,因此您可以这样做

SELECT
    LEFT (route_id, 7) AS unique_route_plan,
    count(*) over (partition by left(route_id, 7)) as unique_route_plan_count,
    route_id,
    station_code,
    country_code,
    plan_wave
FROM
    perfectmile_na.d_perfectmile_routing
WHERE
    date BETWEEN '2020-01-01 00:00:00'
    AND '2020-12-31 00:00:00' -- << -- Are you sure??
    AND station_code LIKE 'D%'
    AND plan_planning_algo <> 'DYNAMIC-PLANNER'

And note your date range excludes the 31st December - that might be what you intended but I suspect not?请注意您的日期范围不包括 12 月 31 日 - 这可能是您的意图,但我怀疑不是?

select count(distinct unique_route_plan) from
(
SELECT
    LEFT (route_id, 7) AS unique_route_plan,
    route_id,
    station_code,
    country_code,
    plan_wave
FROM
    perfectmile_na.d_perfectmile_routing
WHERE
    date BETWEEN '2020-01-01 00:00:00'
    AND '2020-12-31 00:00:00'
    AND station_code LIKE 'D%'
    AND plan_planning_algo <> 'DYNAMIC-PLANNER')t

I would simply use:我会简单地使用:

SELECT COUNT(DISTINCT LEFT(route_id, 7)) AS num_unique_route_plan
FROM perfectmile_na.d_perfectmile_routing
WHERE date BETWEEN '2020-01-01' AND '2020-12-31' AND
      station_code LIKE 'D%' AND
      plan_planning_algo <> 'DYNAMIC-PLANNER';

Note: If you want all of 2020, the logic would be:注意:如果你想要 2020 年的全部,逻辑是:

WHERE date >= '2020-01-01' AND
      date < '2021-01-01' AND
      station_code LIKE 'D%' AND
      plan_planning_algo <> 'DYNAMIC-PLANNER';

I would advise you not to use BETWEEN on dates.我建议您不要在日期上使用BETWEEN

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM