Snowflake SQL-将mmddyyyy转换为mmyyyy,然后按mmyyyy分组

时间:2019-08-22 05:09:22

标签: sql date truncate to-date snowflake-data-warehouse

在雪花SQL中工作。我的日期字段是mm-dd-yyyy。我想将其转换为mm-yyyy,然后在mm-yyyy级别将其余查询分组。

我有:

select
    SFDC_ACCOUNT_KEY,
    date_trunc('month',DATE_CURRENT) as mm_yyyy,
    max(ACTIVE_COMMERCIAL_UNITS) as max_active_commercial_units,
    max(ACTIVE_HOA_UNITS) as max_active_hoa_units,
    max(ACTIVE_RESIDENTIAL_UNITS) as max_active_res_units,
    max(TOTAL_ACTIVE_UNITS) as max_total_active_units
FROM SHARED.CUSTOMERS_DIM
group by sfdc_account_key, date_current
order by sfdc_account_key, date_current;

由于原始日期是mm-dd-yyyyy,所以我仍要返回日级数据集(原始表位于mm-dd-yyyyy级别),当我希望将其包装为mm- yyyy级别。

这让我发疯了,因为我敢肯定这很简单,但是已经晚了,我很困惑。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

在分组依据中使用date_trunc('month',DATE_CURRENT)而不是date_current

select
    SFDC_ACCOUNT_KEY,
    date_trunc('month',DATE_CURRENT) as mm_yyyy,
    max(ACTIVE_COMMERCIAL_UNITS) as max_active_commercial_units,
    max(ACTIVE_HOA_UNITS) as max_active_hoa_units,
    max(ACTIVE_RESIDENTIAL_UNITS) as max_active_res_units,
    max(TOTAL_ACTIVE_UNITS) as max_total_active_units
FROM SHARED.CUSTOMERS_DIM
group by sfdc_account_key, date_trunc('month',DATE_CURRENT)
order by sfdc_account_key, date_trunc('month',DATE_CURRENT)