2017-08-30 103 views
3

我想選擇哪個比率更大,並根據UNION ALL的結果將其輸入到單個查詢結果中。例如,員工200每小時賺取25美元作爲基本費率,但他工作的工作基礎費率爲10.00。那麼他每小時應該得到25.00。員工100的基本費率爲10.00,但該職位的基準費率爲25.00。所以他也應該每小時25.00。我想選擇每個員工的最高費率。類似於這個想法的東西。 SELECT僱員,RATE_A或RATE_B ....下面是一些我的數據放在一起根據兩個不同列選擇哪個值更大

CREATE Table WageRate(
[ID] [int] IDENTITY(1,1) NOT NULL, 
[RateCode] int NULL, 
[Rate] Decimal (10,2) NULL 
) 

INSERT INTO WageRate(RateCode,Rate) 
Values (1,10.00), (2,15.00), (3,20.00), (4,25.00) 

Create Table Employee(
[ID] [int] IDENTITY(1,1) NOT NULL, 
[EmployeeID] int NULL, 
[RateCode] int NULL 
) 

Insert Into Employee (EmployeeID,RateCode) 
Values (100,1), (200,4) 

Create Table TimeCards(
[ID] [int] IDENTITY(1,1) NOT NULL, 
[EmployeeID] int NULL, 
[Hours] Decimal (10,2) NULL, 
[JobRateCode] int NULL 
) 

Insert Into TimeCards (EmployeeID,[Hours],JobRateCode) 
Values (100,8.00,4), (200,8.00,1) 


SELECT t1.Employeeid ,(t0.Rate) as [Rate_A] ,Null FROM WageRate t0 
INNER JOIN Employee t1 ON t1.RateCode= t0.RateCode 
INNER JOIN TimeCards t2 on t1.EmployeeID = t2.EmployeeID 

UNION ALL 

SELECT t4.Employeeid ,Null,(t3.Rate) As [Rate_B] FROM WageRate t3 
INNER JOIN TimeCards t4 on t4.JobRateCode = t3.RateCode 
INNER JOIN Employee t5 ON t4.EmployeeID= t5.EmployeeID 
+1

如果您不使用它們作爲代理鍵,那麼表中的標識列有什麼意義?我的意思是,如果你的'Employee'表中已經有'EmployeeId',爲什麼你還有一個'ID'列? –

回答

2

使用CASE表達式,你可以用一個單一的查詢做沒有工會:

SELECT e.EmployeeID, 
     CASE WHEN ISNULL(ew.Rate, 0.0) > ISNULL(jw.Rate, 0.0) THEN 
      ew.Rate 
     ELSE 
      jw.Rate 
     END As Rate 
FROM Employee e 
LEFT JOIN TimeCards t On e.EmployeeID = t.EmployeeID 
LEFT JOIN WageRate ew ON e.RateCode = ew.RateCode 
LEFT JOIN WageRate jw ON t.JobRateCode = jw.RateCode 

See a live demo on rextester.

1
SELECT e.employeeid, 
CASE 
    WHEN wr_tc.rate > wr_emp.rate 
     THEN wr_tc.rate 
    ELSE wr_emp.rate 
    END AS rate 
FROM Employee e 
INNER JOIN wagerate wr_emp ON wr_emp.ratecode = e.RateCode 
INNER JOIN timecards tc ON tc.employeeid = e.employeeid 
INNER JOIN wagerate wr_tc ON wr_tc.RateCode = tc.jobratecode 
0

如何這樣的事情...

SELECT 
    e.EmployeeID, 
    tc.Hours, 
    PayRate = CASE WHEN wr1.Rate > wr2.Rate THEN wr1.Rate ELSE wr2.Rate END, 
    TotalPay = tc.Hours * CASE WHEN wr1.Rate > wr2.Rate THEN wr1.Rate ELSE wr2.Rate END 
FROM 
    #Employee e 
    JOIN #WageRate wr1 
     ON e.RateCode = wr1.RateCode 
    JOIN #TimeCards tc 
     ON e.EmployeeID = tc.EmployeeID 
    JOIN #WageRate wr2 
     ON tc.JobRateCode = wr2.RateCode; 

結果...

EmployeeID Hours         PayRate         TotalPay 
----------- --------------------------------------- --------------------------------------- --------------------------------------- 
200   8.00         25.00         200.0000 
100   8.00         25.00         200.0000 
0

要做到這一點不區分邏輯:

SQL Fiddle

查詢:

SELECT s.EmployeeID, s.Hours, s.jobRate, s.wageRate 
    , (SELECT max(r) FROM (VALUES (s.jobRate),(s.wageRate)) AS value(r)) AS maxRate 
FROM (
    SELECT e.EmployeeID, t.Hours 
     , COALESCE(w1.Rate,0) AS jobRate 
     , COALESCE(w2.Rate,0) AS wageRate 
    FROM TimeCards t 
    INNER JOIN Employee e ON t.EmployeeID = e.EmployeeID 
    LEFT OUTER JOIN WageRate w1 ON e.RateCode = w1.RateCode   
    LEFT OUTER JOIN WageRate w2 ON t.jobRateCode = w2.RateCode 
) s 
; 

結果:

| EmployeeID | Hours | jobRate | wageRate | maxRate | 
|------------|-------|---------|----------|---------| 
|  100 |  8 |  10 |  25 |  25 | 
|  200 |  8 |  25 |  10 |  25 | 
相關問題