我有如下表格
我想要一個查詢來獲得col3='C'的最大日期。如果第3列中有'V'的最大日期,那么輸出應該是空的
所以在這種情況下,如果我寫 我嘗試了不同的方法,但沒有成功,請幫助我。
uj5u.com熱心網友回復: 一定有一個更干凈的寫法,但它是有效的。
你可以在這里測驗。https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=c2678aec931a95ad3d0ee4c3f8eafe4a uj5u.com熱心網友回復: 我想這是你想要的。 獲取最大日期為 如果具有最大日期的行是 uj5u.com熱心網友回復: 如果條件是帶有'V'的行可以存在,只要其日期在'C'的max(date)之前,那么: 這個替代方法將回傳col3='C'的最大日期,只要有一個不具有相同最大日期的col3='V'的另一行 uj5u.com熱心網友回復: 假設該列中只有 "C "和 "V",你可以直接使用條件邏輯: 當最大日期為 "C "時,這具體回傳最大日期。
標籤:select max(date) from table where col3='C'.
select max(date) from table where col3='C'。然后我們將得到10/22/2020的輸出,但在表中我們還有一個10/23/2020的V值。
select max(date) from table where col3='C',我應該得到輸出為空。
SELECT CASE WHEN(SELECT col3 FROM A WHERE adate=(SELECT max(adate) FROM A))='V'/span> THEN NULL ELSE max(adate) END
FROM A
Col3 = CCol3 = V,則回傳NULL
with
cte as
(
select *,
rn = row_number() over ( order by [Date] desc)
from yourtable
)
select max([Date] )
from cte
where Col3 = 'C'/span>
and not exists
(
select *
from cte
where rn = 1
and Col3 = 'V'/span>
)
select T1.adate_max from
(select max(adate) as adate_max
from A
where col3 = 'C'/span>) T1
left join A on A.adate >= T1。 adate_max and A.col3 = 'V'。
where A.col3 is null
select T1.adate_max from
(select max(adate) as adate_max
from A
where col3 = 'C'/span>) T1
left join A on A.adate = T1。 adate_max and A.col3 = 'V'。
where A.col3 is null
select (case when max(case when Col3 = 'C' then date end) = max(date)
then max(date)
end)
from t。
