我想排除不屬於 case when 語句中指定的值範圍的記錄 - SQL/MySQL
P粉428986744
P粉428986744 2023-09-11 23:15:24
0
2
483

我正在嘗試為來自 DTBL_SCHOOL_DATES 表的特定日期設定季節範圍。以下是我的邏輯,它根據年份及其地區來設定給定範圍內的季節。

CASE WHEN RTRIM(dtbl_school_dates.local_school_year) = '2021-2022' THEN CASE WHEN RTRIM(dtbl_schools_ext.region) = 'Bay Area' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '08/07/2021' and '09/08/2021' THEN 'FALL' WHEN RTRIM(dtbl_schools_ext.region) = 'Bay Area' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '11/27/2021' and '12/15/2021' THEN 'WINTER' WHEN RTRIM(dtbl_schools_ext.region) = 'Bay Area' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '03/04/2022' and '03/22/2022' THEN 'SPRING' WHEN RTRIM(dtbl_schools_ext.region) = 'Central Valley' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '07/31/2021' and '09/01/2021' THEN 'FALL' WHEN RTRIM(dtbl_schools_ext.region) = 'Central Valley' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '11/27/2021' and '12/15/2021' THEN 'WINTER' WHEN RTRIM(dtbl_schools_ext.region) = 'Central Valley' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '02/19/2022' and '03/08/2022' THEN 'SPRING' WHEN RTRIM(dtbl_schools_ext.region) = 'Los Angeles' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '08/14/2021' and '09/15/2021' THEN 'FALL' WHEN RTRIM(dtbl_schools_ext.region) = 'Los Angeles' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '11/27/2021' and '12/15/2021' THEN 'WINTER' WHEN RTRIM(dtbl_schools_ext.region) = 'Los Angeles' AND CAST(dtbl_school_dates.date_value AS DATE) BETWEEN '03/04/2022' and '03/22/2022' THEN 'SPRING' ELSE 'NOT IN RANGE' END ELSE FTBL_TEST_SCORES.test_admin_period END AS "C4630"

但是每當日期不在邏輯中指定的範圍內時,我希望它們被忽略。又稱為“不在範圍內”的值應該被排除。我嘗試使用 FTBL_TEST_SCORES.test_admin_period 不為空,並且由於資料庫中沒有值為空,它們將無法運作。

不在範圍內的值應該被排除在結果中,我如何在 where 子句中實現這一點

我嘗試使用 Alias 進行限制,但不起作用。 我不確定是否可以將值分配給 case 語句中的特定字段,例如 Case when 'a' then field ='B' end

不在範圍內的值應該被排除在結果中,我如何在 where 子句中實現這一點

P粉428986744
P粉428986744

全部回覆 (2)
P粉083785014

如果無法查看完整的查詢,就不可能提出最佳解決方案。最簡單的解決方案是將標準新增為HAVING子句:

HAVING `C4630` <> 'NOT IN RANGE'

我對你的日期格式感到困惑。如果這應該是 MySQL 查詢,則它們應該採用 yyyy-mm-dd 格式。可以說,即使在支援不明確的本機日期格式的 SQL Server 上,它們也應該採用這種格式。

為什麼都是RTRIMCAST?數據應該以正確的格式存儲,並在輸入時進行清理,如果無法做到這一點,請定期清理數據,而不是對每個查詢進行清理。

明智的做法是將您的季節移動到它們自己的表中,而不是在查詢時定義它們。然後它是一個簡單的連接到seasons。我在這裡使用了子查詢,但希望您能明白:

SELECT /* other columns */ COALESCE(seasons.season, FTBL_TEST_SCORES.test_admin_period) AS `C4630` FROM all_the_other_tables LEFT JOIN ( SELECT 'Bay Area' AS region, 'FALL' AS season, '2021-08-07' AS start, '2021-09-08' AS end UNION ALL SELECT 'Bay Area' , 'WINTER' , '2021-11-27' , '2021-12-15' UNION ALL SELECT 'Bay Area' , 'SPRING' , '2022-03-04' , '2022-03-22' UNION ALL SELECT 'Central Valley' , 'FALL' , '2021-07-31' , '2021-09-01' UNION ALL SELECT 'Central Valley' , 'WINTER' , '2021-11-27' , '2021-12-15' UNION ALL SELECT 'Central Valley' , 'SPRING' , '2022-02-19' , '2022-03-08' UNION ALL SELECT 'Los Angeles' , 'FALL' , '2021-08-14' , '2021-09-15' UNION ALL SELECT 'Los Angeles' , 'WINTER' , '2021-11-27' , '2021-12-15' UNION ALL SELECT 'Los Angeles' , 'SPRING' , '2022-03-04' , '2022-03-22' ) AS seasons ON dtbl_school_dates.local_school_year = '2021-2022' AND dtbl_schools_ext.region = seasons.region AND dtbl_school_dates.date_value BETWEEN seasons.start AND seasons.end WHERE ( (dtbl_school_dates.local_school_year = '2021-2022' AND seasons.season IS NOT NULL) OR dtbl_school_dates.local_school_year <> '2021-2022' );
    P粉718165540

    您可以在 WHERE 子句中重複整個 CASE 語句,如下所示:

    select case when x then y when a then b when c then d else 'NOT IN RANGE' end as foo from table t where case when x then y when a then b when c then d else 'NOT IN RANGE' end <> 'NOT IN RANGE'

    或您可以使用子查詢(或 CTE),如下所示:

    select * from ( select case when x then y when a then b when c then d else 'NOT IN RANGE' end as foo from table t ) where foo <> 'NOT IN RANGE'
      最新下載
      更多>
      網站特效
      網站源碼
      網站素材
      前端模板
      關於我們 免責聲明 Sitemap
      PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!