![](/img/trans.png)
[英]Using the NorthWind database, write the SQL statement that reports each employees name and any orders that were taken by that employee
[英]Using SQL case statement to change the output for each employee and each date
使用下表如何在最小值時將值更改為“ in”,在最大值時將值更改為“ out”,同時將中間值更改為“ leave out”(如果其后是第一個值)。 in',但不是最大值,如果不是最大值,則成為其后的記錄。
code val date val2
001 1 2018-08-08 00:00:00.000 NULL
001 2 2018-08-08 00:00:00.000 NULL
001 3 2018-08-08 00:00:00.000 NULL
001 4 2018-08-08 00:00:00.000 NULL
001 1 2018-08-09 00:00:00.000 NULL
001 2 2018-08-09 00:00:00.000 NULL
002 1 2018-08-09 00:00:00.000 NULL
002 2 2018-08-09 00:00:00.000 NULL
002 3 2018-08-09 00:00:00.000 NULL
002 4 2018-08-09 00:00:00.000 NULL
003 1 2018-08-09 00:00:00.000 NULL
003 2 2018-08-09 00:00:00.000 NULL
我設法更改了最小值,但最大值僅按日期更改,並記下了員工代碼。
這是你想要的嗎?
select t.*,
(case when val = 1 then 'in'
when val = max(val) over (partition by code) then 'out'
else 'leave out'
end)
from t;
如果是這樣,則可以輕松地將邏輯合並到更新中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.