Get The Employee who has taken Minimum days of leave

Khanjan

Recently in one of the interviews I had given , I was asked question to

write a query to find the employee with min days of leave in previous 3 months department wise.

The table structure was

EMPID | TO_DATE | FROM_DATE | LEAVE _TYPE | DEPT_NO

I was able to write the query

SELECT 
min(days) FROM (SELECT id ,(sum((TO_DATE-FROM_DATE)+1) ) days ,dept  
FROM emp_leave  
WHERE to_date  between  ADD_MONTHS(sysdate,-3)  AND sysdate group by id,dept) 
group by dept  ;

but when I try to select the emp_id I have to add it in group by statement.

I was stuck there.

Steven

I think the query should have been something like

 select dept, min(id) keep (dense_rank last order by days)
from (  SELECT  id ,
            (sum((TO_DATE-FROM_DATE)+1) ) days ,
            dept 
    FROM emp_leave 
    WHERE to_date between ADD_MONTHS(sysdate,-3) 
    AND sysdate group by id,dept) 
group by dept
;

Well of course, in SQL you have a lot of different way to do this, but when it is about ranking stuff, the first/last function is very useful

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

How to check if an XML file has a minimum structure (Java)?

Get username of user who has file open on network drive - Microsoft Office Style

How to get the number of days between two dates

Get list of contributors who have made commits to a particular file

mysql -get data who complete within a year

How to sort the data that has been taken from an xml file to a html table using php?

Query for employee who has maximum leaves

Image taken from camera and encoded to Base64 has bad quality

Get the Minimum value from Consecutive dataframe values

Get users who renew the package after expiry

Get difference between two days (minus method)

SQL query to get the record which is not a maximum or a minimum

Git Fork how to find who has forked into our repositories

How to set minimum days between selected start and end day on daterange picker?

SQL query to select students who have taken all subjects from subjects table

Get number of days from today JQuery

How to count who has most hits in MySQL

Python - Cant get minimum value

How would I specifically target users who have not used my app in several days?

Get minimum value in linq query

Get minimum hours and maximum hours from mysql

how to get MONTHS, DAYS and weeks (android datepicker)

PostgreSQL get minimum of count(*)?

Get short days of week in android

If systemd has replaced SystemV, why is it still there and who is running the show?

Get days of week between two selected dates

Java - Calling a variable from a different class which's value is taken from a JTextField when a JButton has been pressed

Get all days from a month

Oracle - Count of number of days a value has changed

TOP 一覧

  1. 1

    三項演算子良い練習の代わりとしてOptional.ofNullableを使用していますか?

  2. 2

    Spring Boot Filter is not getting invoked if remove @component in fitler class

  3. 3

    STSでループプロセス「クラスパス通知の送信」のループを停止する方法

  4. 4

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  5. 5

    ビュー用にサイズ変更した後の画像の高さと幅を取得する方法

  6. 6

    画像変更コードを実行してもボタンの画像が変更されない

  7. 7

    Three.js indexed BufferGeometry vs. InstancedBufferGeometry

  8. 8

    VisualStudioコードの特異点/ドッカー画像でPythonインタープリターを使用するにはどうすればよいですか?

  9. 9

    Python / SciPyのピーク検出アルゴリズム

  10. 10

    Ansibleで複数行のシェルスクリプトを実行する方法

  11. 11

    __init__。pyファイルの整理中に循環インポートエラーが発生しました

  12. 12

    二次導関数を数値計算するときの大きな誤差

  13. 13

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  14. 14

    androidsoongビルドシステムによるネイティブコードカバレッジ

  15. 15

    Reactでclsxを使用する方法

  16. 16

    値間の一致を見つける最も簡単な方法は何ですか

  17. 17

    Using Angular's UI-router, how can we make sure the new version of the html partial views are used, rather than the cached version?

  18. 18

    reCAPTCHA-エラーコード:ユーザーの応答を検証するときの「missing-input-response」、「missing-input-secret」(POSTの詳細がない)

  19. 19

    PyTesseractを使用した背景色のため、スクリーンショットからテキストを読み取ることができません

  20. 20

    エンティティIDを含む@RequestBody属性をSpringの対応するエンティティに変換します

  21. 21

    How to access json value by key value in freemarker?

ホットタグ

アーカイブ