Dbms

從數據庫中選擇正確值的問題

  • December 6, 2017

我有以下數據庫,但在編寫正確的選擇片語時遇到問題。我是 SQL 新手,希望您能通過向我展示選擇所需數據的正確方法來幫助我:

  • Employee( eid, ename, salary, did, classification)
  • Department( did, dname, floor, head)
  • dbudget( did, byear, budget)
  • Project( pid, pname, did, budget, ddate)
  • Onproject( pid, eid, fdate)

數據庫說明:

  • Department.did= 項目的部門 id(他們在做什麼)
  • dbudget.byear = 預算年份

我的任務:

  1. 顯示收入最高的第 4 類工人的姓名和工資。

我的查詢:

select ename ,salary from Employee where classification='4' 

我不知道如何選擇最賺錢的人…… 2. 顯示只有兩名員工的部門 id ( did) 及其名稱 ( ),分類為 3dname

我的查詢:

select did,dname from Department where classification='3'

我不知道如何選擇,以便它只顯示那些有兩個員工的分類 3. did顯示連續兩年工資低於 700000 的部門的部門 id ( )、第一年和之後的一年(hard)

我不知道如何創建一個代表第一年和之後一年的查詢。

select TOP (1) ename,salary from Employee where classification='4'
order by salary desc
select did,dname from Department
where exists
(select 1  from Employee 
group by did,classification
having count(*) = 2
and classification = '3' and Department.did = Employee.did)
with cte as(
select did,byear,budget,LAG(byear) OVER (partition by did order by budget) as preceeding, 
LEAD(byear) OVER (partition by did order by budget) as next from budget
where budget < 70000 
)
select distinct did,coalesce(preceeding,byear) as FirstYear,coalesce(next,byear) as NextYear from cte where cte.byear = preceeding + 1 or cte.byear = next - 1

希望這可以幫助!

引用自:https://dba.stackexchange.com/questions/192511