不是要找重覆的資料,但很類似 |
答題得分者是:st33chen
|
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
真的沒法了
搞了好久都想不出來 先說明一下我的需求條件 是員工檔(PER1), 員編(E_NO)是Key 值 同一人可能離職又回來 所以會有身份證(E_ID)重覆的問題 現在要撈身份證唯一值 若有兩筆以上同身份證, 要到職日(E_IN)最大的一筆就好 我SQL 都寫好嘍, 在 SQL Explorer 可以run 可是到了 Delphi 的 Query open 時, 會出現 第 2 行有錯誤, 它不接受兩個欄位的 IN =========================================== select E_NO,E_NA,E_ID,E_IN,E_OU from PER1 where (E_ID,E_IN) in (select E_ID,max(E_IN) from PER1 group by E_ID) order by E_NO ========================================= 請問有其他解決方法嗎? 希望一個SQL 可以解決 若不行, 用2 個SQL 迴圈也可以啦! 謝謝!!謝謝!!! |
christie
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
|
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
===================引 用 christie 文 章=================== { 請試試看 } select * from per1,( select e_no eno,max(e_in) ein from per1 group by e_no ) where e_no=eno and e_in = ein ======================== 因為你這樣身份證有重覆 所以我改成了以下: select * from per1,(select e_id eid,max(e_in) ein from per1 group by e_id) where e_id=eid and e_in = ein ======== SQL explorer 可以執行喔 可是又被Delphi 擋掉了 Delphi 是怎麼啦.. 一直擋掉我的SQL 它這次是說 where 附近語法不正確 想必是不接受 逗點 後面那一串 :( p.s. 忘了說, 我是 Delphi 6 |
christie
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
Query1.SQL.Text:='select per1.* from per1,(' +
' select e_id eid,max(e_in) ein from per1 group by e_id' ' )' 'where e_id=eid and e_in = ein order by e_no'; Query1.Open { Try It }
------
What do we live for if not to make life less difficult for each other? |
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
|
christie
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
Please Check BDE
Configuration Drivers Native Oracle DLL32 = SQLORA32.DLL(or SQLORA8.DLL) Vendor INIT = OCI.DLL ===================引 用 hungoo 文 章=================== 一樣的錯誤訊息呢...
------
What do we live for if not to make life less difficult for each other? |
st33chen
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
您原本的寫法應是對的, 我記得我曾寫過類似的,
如果再不行, 如果是我我會試試 : select E_NO,E_NA,E_ID,E_IN,E_OU from PER1 where (E_ID || E_IN) in (select E_ID || max(E_IN) from PER1 group by E_ID) order by E_NO 兩個欄位不成, 改成一個欄位試試,
------
IS IT WHAT IT IS 我是 李慕白 請倒著唸. 又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦); 都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲. |
st33chen
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
如果是 oracle 的話, 還有一種 analytic function 可以用
select * from ( select E_NO,E_NA,E_ID,E_IN,E_OU, row_number() over (partition by E_ID order by E_IN DESC) TOP_N from PER1 ) WHERE TOP_N<=1 ORDER BY E_NO 請試試看, 對了, 可否把錯誤訊息 SHOW 一下, 說不定不是 sql 語法的問題
------
IS IT WHAT IT IS 我是 李慕白 請倒著唸. 又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦); 都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲. |
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
|
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
對於之前給錯資訊感到很抱歉!
雖然目前還不知怎麼正確的從SQL Server 撈資料 不過很感謝christie 和 st33chen 兩位大大的協助! 我找了一下前輩的文章 自己亂寫一通 ================ SELECT E_NO,E_NA,E_ID,E_IN,E_OU FROM PER1 T WHERE T.E_IN = (SELECT MAX(E_IN) FROM PER1 T1 WHERE T1.E_ID = T.E_ID) order by E_ID ================ 覺得怪怪的,不知這樣有沒有少了什麼條件... 請前輩指教~~ 謝謝!! |
st33chen
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:15 回覆:591 積分:1201 註冊:2005-09-30 發送簡訊給我 |
|
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
|
yubad2000
中階會員 ![]() ![]() ![]() 發表:0 回覆:44 積分:78 註冊:2007-09-30 發送簡訊給我 |
Based on the textbook, the formal way to sovle the MAX question is to use "not exists".
The SQL statements will be: Select E_NO,E_NA,E_ID,E_IN,E_OU from PER1 t1 where not exists ( Select E_ID from PER1 t2 where t1.E_ID = t2.E_ID and t2.E_IN > t1.E_IN) ===================引 用 hungoo 文 章=================== 真的沒法了 搞了好久都想不出來 先說明一下我的需求條件 是員工檔(PER1), 員編(E_NO)是Key 值 同一人可能離職又回來 所以會有身份證(E_ID)重覆的問題 現在要撈身份證唯一值 若有兩筆以上同身份證, 要到職日(E_IN)最大的一筆就好 我SQL 都寫好嘍, 在 SQL Explorer 可以run 可是到了 Delphi 的 Query open 時, 會出現 第 2 行有錯誤, 它不接受兩個欄位的 IN =========================================== select E_NO,E_NA,E_ID,E_IN,E_OU from PER1 where (E_ID,E_IN) in (select E_ID,max(E_IN) from PER1 group by E_ID) order by E_NO ========================================= 請問有其他解決方法嗎? 希望一個SQL 可以解決 若不行, 用2 個SQL 迴圈也可以啦! 謝謝!!謝謝!!!
------
===波士頓夜未眠=== What a wonderful world!! Jazz up the world with jazz!! ================== When I am not programming... you can find me here: http://www.holy-war.de/EN/World3/bin/?advertiser=63190 |
hungoo
一般會員 ![]() ![]() 發表:3 回覆:7 積分:2 註冊:2002-05-27 發送簡訊給我 |
===================引 用 yubad2000 文 章=================== Based on the textbook, the formal way to sovle the MAX question is to use "not exists". The SQL statements will be: Select E_NO,E_NA,E_ID,E_IN,E_OU from PER1 t1 where not exists ( Select E_ID from PER1 t2 where t1.E_ID = t2.E_ID and t2.E_IN > t1.E_IN) =========== 這方法也可查出我要的執行結果(抓出到職日大的) 可是我無法理解.. 子查詢的 t2 已查出到職日比較大的資料 外部查詢的 t1 加了 not exists 不是應該反向 變成只剩下 到職日 小的 資料? 是我哪裡理解錯了嗎? |
yubad2000
中階會員 ![]() ![]() ![]() 發表:0 回覆:44 積分:78 註冊:2007-09-30 發送簡訊給我 |
Yes, It's hard to understand at the beginning.
Here is the logic. We use t1 as the main table, t2 is the reference table. So, in the sub query, we select out all records which has the same ID as t1's record and has the date greater than t1's date. In other word, if the sub query is not null, then the date of t1's record is not MAX. if the sub query is null, then then the date of t1's record is MAX since no record in t2 greater then it. ===================引 用 hungoo 文 章=================== ===================引 用 yubad2000 文 章=================== Based on the textbook, the formal way to sovle the MAX question is to use "not exists". The SQL statements will be: Select E_NO,E_NA,E_ID,E_IN,E_OU from PER1 t1 where not exists ( Select E_ID from PER1 t2 where t1.E_ID = t2.E_ID and t2.E_IN > t1.E_IN) =========== 這方法也可查出我要的執行結果(抓出到職日大的) 可是我無法理解.. 子查詢的 t2 已查出到職日比較大的資料 外部查詢的 t1 加了 not exists 不是應該反向 變成只剩下 到職日 小的 資料? 是我哪裡理解錯了嗎?
------
===波士頓夜未眠=== What a wonderful world!! Jazz up the world with jazz!! ================== When I am not programming... you can find me here: http://www.holy-war.de/EN/World3/bin/?advertiser=63190 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |