Spark SQL 2.4实现SQL Server上季度末日期计算方案问询
Convert SQL Server Previous Quarter End Date Query to Spark SQL 2.4
Got it, let's solve this problem of getting the last day of the previous quarter for a given date in Spark SQL 2.4, matching the behavior of your original SQL Server query.
The Spark SQL Solution
Here's the equivalent query that produces exactly the output you need:
SELECT input_date, date_sub( trunc(cast(input_date AS DATE), 'quarter'), 1 ) AS end_date FROM your_table_name;
How It Works (Breakdown vs. SQL Server Logic)
Let's map this to your original SQL Server query to show the equivalent steps:
cast(input_date AS DATE): Same as your originalcast('2020-09-09' AS DATE)– ensures we're working with a proper date type.trunc(..., 'quarter'): This replaces thedateadd(qq, datediff(qq, 0, [date]), 0)part of your SQL Server query. Thetruncfunction with'quarter'parameter returns the first day of the input date's current quarter (e.g.,2020-09-09becomes2020-07-01).date_sub(..., 1): Replacesdateadd(dd, -1, ...)– subtracts 1 day from the quarter's first day to get the last day of the previous quarter.
Verification with Your Sample Data
Let's test this against your input/output pairs to confirm it works:
- Input:
2020-01-21→truncgives2020-01-01→date_subgives2019-12-31✔️ - Input:
2020-02-06→truncgives2020-01-01→date_subgives2019-12-31✔️ - Input:
2020-04-15→truncgives2020-04-01→date_subgives2020-03-31✔️ - Input:
2020-07-10→truncgives2020-07-01→date_subgives2020-06-30✔️ - Input:
2020-10-20→truncgives2020-10-01→date_subgives2020-09-30✔️ - Input:
2021-02-04→truncgives2021-01-01→date_subgives2020-12-31✔️
Notes
- If your
input_datecolumn is already aDATEtype (not a string), you can omit thecast(input_date AS DATE)part and just usetrunc(input_date, 'quarter'). - This works reliably in Spark SQL 2.4 – the
truncfunction's'quarter'parameter is fully supported in this version.
内容的提问来源于stack exchange,提问作者dexter80
相关产品推荐
相关产品推荐

