根据每个 ID 的另一列的值查找一列的两个值
Find two values of one Column based on value of another column per ID
我有一个 sqlite 数据库,其中 table 称为贷款。 table 示例数据在这里:
Loan Table at sqlfiddle.com
此 table 包含以下栏目:
mainindex | brindx | YearMonth | EmpID | VarNo | Name | LastName | CodepayID | CodepaySH | Lval | Lint | Lrmn
现在,我需要一个查询来显示所需的结果,包含
[empid],[Codepayid],[Lval-1],[Lval-2],[Sum(Lint)],[Lrmn-1],[Lrmn-2]
,
有了这个条件:
[lVal-1] as Value of Lval Column of each Employee Correspond to their Lowest YearMonth
[lVal-2] as Value of Lval Column of each Employee Correspond to their Highest YearMonth
[Sum(Lint)] Sum of Lint Column for each Employee.
[Lrmn-1] as Value of Lrmn Column of each Emplyee Correspond to that Lowest YearMonth
[Lrmn-2] as Value of Lrmn Column of each Emplyee Correspond to that Highest YearMonth
例如:
select empid, Codepayid, Lval1, Lval2, Sum(Lint), Lrmn1, Lmrn2
from Loan
where CodepayID=649 and EmpID=12450400
group by EmpID
结果:
员工 ID
CodepayID
Lval1
Lval2
总和(林特)
Lrmn1
Lrmn2
12450400
649
405480
405485
270320
337900
202740
要获得您想要的结果,首先您必须从每个员工的 table 中找到列 YearMonth 的最小值和最大值,并使用这些值来获取最小和最大 YearMonth 的相应 lval 和 lrmn。
您可以在存储过程中或在单个查询中执行此操作,具体取决于您的选择。以下是将提供您想要的输出的查询
SELECT l.empid, l.CodepayID, min(a.lval1) as lval1, max(b.lval2) as lval2 ,sum(l.lint) as lint,min(a.lrmn1) as lrmn1,max(b.lrmn2) as lrmn2
FROM loan l inner join
( SELECT empid,CodepayID, lval lval1, Lrmn lrmn1
FROM loan ln
WHERE YEARMonth = (SELECT MIN(YearMonth)
FROM loan
WHERE empid = ln.EmpID and CodepayID = ln.CodepayID
group by empid,CodepayID)
) a on l.empid = a.EmpID and l.CodepayID = a.codepayid
inner join
( SELECT empid, CodepayID, YearMonth,lval lval2, Lrmn lrmn2
FROM loan ln
WHERE YEARMonth = (SELECT MAX(YearMonth)
FROM loan
WHERE empid = ln.EmpID and CodepayID = ln.CodepayID
GROUP BY empid,CodepayID)
) b on l.empid = b.EmpID and l.CodepayID = b.codepayid
GROUP BY l.EmpID,l.Codepayid
使用FIRST_VALUE()
和SUM()
window函数:
SELECT DISTINCT EmpID, CodepayID,
FIRST_VALUE(Lval) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth) LVal1,
FIRST_VALUE(Lval) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth DESC) LVal2,
SUM(Lint) OVER (PARTITION BY EmpID, CodepayID) sum_Lint,
FIRST_VALUE(Lrmn) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth) Lrmn1,
FIRST_VALUE(Lrmn) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth DESC) Lrmn2
FROM loan
参见demo。
接受的答案不必要地过于复杂。即使您不想使用 window 函数,以下使用与接受的答案相同的逻辑,对数据库的工作要少得多,完全避免了相关的子查询...
SELECT
l.empid,
l.CodepayID,
min(case when l.yearmonth = s.yearmonth_min then l.lval end) as lval1,
max(case when l.yearmonth = s.yearmonth_max then l.lval end) as lval2,
sum(l.lint) as lint,
min(case when l.yearmonth = s.yearmonth_min then l.lrmn end) as lrmn1,
max(case when l.yearmonth = s.yearmonth_max then l.lrmn end) as lrmn2
FROM
loan l
inner join
(
select
empid,
CodepayID,
min(yearmonth) as yearmonth_min,
max(yearmonth) as yearmonth_max
from
loan
group by
empid,
CodepayID
)
s
on l.empid = s.empid
and l.CodepayID = s.codepayid
group by
l.EmpID,
l.Codepayid
我有一个 sqlite 数据库,其中 table 称为贷款。 table 示例数据在这里: Loan Table at sqlfiddle.com
此 table 包含以下栏目:
mainindex | brindx | YearMonth | EmpID | VarNo | Name | LastName | CodepayID | CodepaySH | Lval | Lint | Lrmn
现在,我需要一个查询来显示所需的结果,包含
[empid],[Codepayid],[Lval-1],[Lval-2],[Sum(Lint)],[Lrmn-1],[Lrmn-2]
,
有了这个条件:
[lVal-1] as Value of Lval Column of each Employee Correspond to their Lowest YearMonth
[lVal-2] as Value of Lval Column of each Employee Correspond to their Highest YearMonth
[Sum(Lint)] Sum of Lint Column for each Employee.
[Lrmn-1] as Value of Lrmn Column of each Emplyee Correspond to that Lowest YearMonth
[Lrmn-2] as Value of Lrmn Column of each Emplyee Correspond to that Highest YearMonth
例如:
select empid, Codepayid, Lval1, Lval2, Sum(Lint), Lrmn1, Lmrn2
from Loan
where CodepayID=649 and EmpID=12450400
group by EmpID
结果:
员工 ID | CodepayID | Lval1 | Lval2 | 总和(林特) | Lrmn1 | Lrmn2 |
---|---|---|---|---|---|---|
12450400 | 649 | 405480 | 405485 | 270320 | 337900 | 202740 |
要获得您想要的结果,首先您必须从每个员工的 table 中找到列 YearMonth 的最小值和最大值,并使用这些值来获取最小和最大 YearMonth 的相应 lval 和 lrmn。
您可以在存储过程中或在单个查询中执行此操作,具体取决于您的选择。以下是将提供您想要的输出的查询
SELECT l.empid, l.CodepayID, min(a.lval1) as lval1, max(b.lval2) as lval2 ,sum(l.lint) as lint,min(a.lrmn1) as lrmn1,max(b.lrmn2) as lrmn2
FROM loan l inner join
( SELECT empid,CodepayID, lval lval1, Lrmn lrmn1
FROM loan ln
WHERE YEARMonth = (SELECT MIN(YearMonth)
FROM loan
WHERE empid = ln.EmpID and CodepayID = ln.CodepayID
group by empid,CodepayID)
) a on l.empid = a.EmpID and l.CodepayID = a.codepayid
inner join
( SELECT empid, CodepayID, YearMonth,lval lval2, Lrmn lrmn2
FROM loan ln
WHERE YEARMonth = (SELECT MAX(YearMonth)
FROM loan
WHERE empid = ln.EmpID and CodepayID = ln.CodepayID
GROUP BY empid,CodepayID)
) b on l.empid = b.EmpID and l.CodepayID = b.codepayid
GROUP BY l.EmpID,l.Codepayid
使用FIRST_VALUE()
和SUM()
window函数:
SELECT DISTINCT EmpID, CodepayID,
FIRST_VALUE(Lval) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth) LVal1,
FIRST_VALUE(Lval) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth DESC) LVal2,
SUM(Lint) OVER (PARTITION BY EmpID, CodepayID) sum_Lint,
FIRST_VALUE(Lrmn) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth) Lrmn1,
FIRST_VALUE(Lrmn) OVER (PARTITION BY EmpID, CodepayID ORDER BY YearMonth DESC) Lrmn2
FROM loan
参见demo。
接受的答案不必要地过于复杂。即使您不想使用 window 函数,以下使用与接受的答案相同的逻辑,对数据库的工作要少得多,完全避免了相关的子查询...
SELECT
l.empid,
l.CodepayID,
min(case when l.yearmonth = s.yearmonth_min then l.lval end) as lval1,
max(case when l.yearmonth = s.yearmonth_max then l.lval end) as lval2,
sum(l.lint) as lint,
min(case when l.yearmonth = s.yearmonth_min then l.lrmn end) as lrmn1,
max(case when l.yearmonth = s.yearmonth_max then l.lrmn end) as lrmn2
FROM
loan l
inner join
(
select
empid,
CodepayID,
min(yearmonth) as yearmonth_min,
max(yearmonth) as yearmonth_max
from
loan
group by
empid,
CodepayID
)
s
on l.empid = s.empid
and l.CodepayID = s.codepayid
group by
l.EmpID,
l.Codepayid