我有以下輸入資料(僅限樣本):
| 串列ID | 日期 | 價值 |
|---|---|---|
| 0 | 2022-10-17 | 0 |
| 1 | 2022-10-17 | 43.050430504 |
| 3 | 2022-10-17 | 40.000000000 |
| 4 | 2022-10-17 | 38.636363636 |
| 5 | 2022-10-17 | 20.714285714 |
我對以下兩個查詢結果感到有些困惑。
第一個查詢:
SELECT
ListID,
CASE
WHEN date>'2022-07-22'
THEN avg(value)
ELSE NULL
END AS 'Value_Before_Rate_Change'
FROM
TB01 where date like '2022%' and ListID=1;
輸出第一個查詢:
| Value_Before_Rate_Change |
|---|
| 無效的 |
第二次查詢
select avg(value)
from TB01
where date like '2022%'
and ListID=1
and date>'2022-07-22';
輸出第二個查詢:
| 平均(價值) |
|---|
| 57.773696518595 |
有人可以告訴我為什么我在使用CASE.
更新:
我也使用了下面的分組方式。但同樣的結果
SELECT
ListID,
CASE
WHEN date>'2022-07-22'
THEN avg(value)
ELSE NULL
END AS 'Value_Before_Rate_Change'
FROM
TB01 where date like '2022%' and ListID=1 group by ListID;
uj5u.com熱心網友回復:
select listID
,avg(case when date > '2022-07-22' then value end) as Value_Before_Rate_Change
from t
group by listID
| 串列ID | Value_Before_Rate_Change |
|---|---|
| 0 | 0.0000000000000000 |
| 1 | 43.0504305040000000 |
| 3 | 40.0000000000000000 |
| 4 | 38.6363636360000000 |
| 5 | 20.7142857140000000 |
小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517919.html
標籤:mysqlsql
