Select 根据两个不同的列,哪个值更大
Select which value is greater based on two different columns
我想select根据UNION ALL 的结果将其输入到单个查询结果中。例如,员工 200 每小时的基本工资为 25 美元,但他从事的工作的基本工资为 10.00。那时他应该得到每小时 25.00 美元。员工 100 的基本工资率为 10.00,但工作的基本工资率为 25.00。所以他也应该得到每小时 25.00 美元。我想select每位员工的最高费率。类似于这个想法。 SELECT EmployeeID,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
使用 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
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
这样的事情怎么样...
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
要在没有 CASE 逻辑的情况下执行此操作:
查询:
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 |
我想select根据UNION ALL 的结果将其输入到单个查询结果中。例如,员工 200 每小时的基本工资为 25 美元,但他从事的工作的基本工资为 10.00。那时他应该得到每小时 25.00 美元。员工 100 的基本工资率为 10.00,但工作的基本工资率为 25.00。所以他也应该得到每小时 25.00 美元。我想select每位员工的最高费率。类似于这个想法。 SELECT EmployeeID,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
使用 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
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
这样的事情怎么样...
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
要在没有 CASE 逻辑的情况下执行此操作:
查询:
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 |