方案::我有一个用于薪金头的主表,即Basic,HRA,Cnveyance ...等。[Columns are : ID, SalaryHeadName]
和1个交易表,用于保存每个候选人的薪水记录。 [Columns are : CandidateID, SalaryHeadID, Amount]
现在,我想在候选人的个人资料页面上列出所有薪水头,用户将在其中看到“所有头”以及在这些头前面的可编辑文本框。
如果交易表中包含该特定候选人的特定薪金总额,我将填写该金额,否则文本框将为空白。
直到现在我的查询:
WITH cte as
(
Select ID,SalaryHeadName, Amount, DENSE_RANK () over (order by SalaryHeadName desc) as DenseRank
from VW_GetSalaryStructureData
where CandidateID = 145 or CandidateID is null
GROUP BY ID,SalaryHeadName,Amount
)
SELECT
ID,SalaryHeadName, Amount, COUNT(Amount) as cnt
FROM
cte
GROUP BY
ID,SalaryHeadName, Amount
从两个表中采样数据:
ID SalaryHeadName MainHead Description CreatedDate CreatedBy
1 BASIC BASIC GROSS 2018-05-25 16:40:06.080 1
2 HRA HRA GROSS 2018-05-25 16:40:06.080 1
3 Conveyance Conveyance GROSS 2018-05-25 16:40:06.080 1
4 Medical Allowance Medical Allowance GROSS 2018-05-25 16:40:06.080 1
5 City Compensation Allowance City Compensation Allowance GROSS 2018-05-25 16:40:06.083 1
6 Books N Periodicals Allowance Books N Periodicals Allowance GROSS 2018-05-25 16:40:06.083 1
7 Entertainment Allowance Entertainment Allowance GROSS 2018-05-25 16:40:06.083 1
8 Attire Allowance Attire Allowance GROSS 2018-05-25 16:40:06.083 1
9 Mobile Allowance Mobile Allowance GROSS 2018-05-25 16:40:06.087 1
10 Supplementary Allowance Supplementary Allowance GROSS 2018-05-25 16:40:06.087 1
11 Leave Allowance Leave Allowance GROSS 2018-05-25 16:40:06.087 1
12 Bonus Bonus GROSS 2018-05-25 16:40:06.087 1
13 Child Education Allowance Child Education Allowance GROSS 2018-05-25 16:40:06.090 1
14 Washing Allowance Washing Allowance GROSS 2018-05-25 16:40:06.090 1
15 Other Allowance Other Allowance GROSS 2018-05-25 16:40:06.090 1
16 Telephone Allowance Telephone Allowance GROSS 2018-05-25 16:40:06.090 1
17 Bonus (ESIC) Bonus (ESIC) GROSS 2018-05-25 16:40:06.090 1
18 Daily Allowance Daily Allowance GROSS 2018-05-25 16:40:06.093 1
19 Educational Allowance Educational Allowance GROSS 2018-05-25 16:40:06.093 1
20 Performance Allowance Performance Allowance GROSS 2018-05-25 16:40:06.093 1
21 Transport Allowance Transport Allowance GROSS 2018-05-25 16:40:06.093 1
22 Vehicle N Maintenance Allowance Vehicle N Maintenance Allowance GROSS 2018-05-25 16:40:06.097 1
23 Driver Salary Allowance Driver Salary Allowance GROSS 2018-05-25 16:40:06.097 1
24 Business Development Allowance Business Development Allowance GROSS 2018-05-25 16:40:06.097 1
25 LTA LTA GROSS 2018-05-25 16:40:06.097 1
26 Gross Gross GROSS 2018-05-25 16:40:06.097 1
27 Gratuity CTC Gratuity CTC CTC 2018-05-25 16:40:06.100 1
28 Leave Salary Allowance Leave Salary Allowance CTC 2018-05-25 16:40:06.100 1
29 Bonus CTC Bonus CTC CTC 2018-05-25 16:40:06.100 1
30 GPA GPA CTC 2018-05-25 16:40:06.100 1
31 EPF EPF CTC 2018-05-25 16:40:06.100 1
32 ESI ESI CTC 2018-05-25 16:40:06.103 1
33 CTC CTC CTC 2018-05-25 16:40:06.103 1
34 PF PF NET 2018-05-25 16:40:06.103 1
35 ESI ESI NET 2018-05-25 16:40:06.103 1
36 PT PT NET 2018-05-25 16:40:06.107 1
ID CandidateID SalaryHeadID Amount CreatedDate CreatedBy
1 151 1 15201 2018-06-11 11:03:07.587 1
2 151 2 1520 2018-06-11 11:03:07.587 1
3 151 3 2510 2018-06-11 11:03:07.587 1
4 151 6 1500 2018-06-11 11:03:07.587 1
5 151 26 50151 2018-06-11 11:03:07.587 1
6 151 33 434 2018-06-11 11:03:07.587 1
7 184 1 23233 2018-06-21 11:09:12.627 1
8 184 2 5455 2018-06-21 11:09:12.627 1
9 184 3 4343 2018-06-21 11:09:12.627 1
10 184 26 50151 2018-06-21 11:09:12.627 1
11 184 33 434 2018-06-21 11:09:12.627 1
12 154 1 23233 2018-06-21 11:10:37.263 1
13 154 2 5455 2018-06-21 11:10:37.263 1
14 154 3 4343 2018-06-21 11:10:37.263 1
15 154 26 50151 2018-06-21 11:10:37.263 1
16 154 33 434 2018-06-21 11:10:37.263 1
17 145 1 23233 2018-06-21 11:09:12.627 1
18 145 2 5455 2018-06-21 11:09:12.627 1
19 145 3 4343 2018-06-21 11:09:12.627 1
20 145 26 50151 2018-06-21 11:09:12.627 1
21 145 33 434 2018-06-21 11:09:12.627 1
该查询的结果是:
Please click to see the Result:
现在,我要删除具有NULL
值的重复行,并且仅列出所有SalaryHeads一次。
答案 0 :(得分:0)
以下查询应返回预期结果。
BEGIN
WITH cte as ( Select ID,SalaryHeadName, Amount, DENSE_RANK () over (order by SalaryHeadName desc) as DenseRank from VW_GetSalaryStructureData where CandidateID = 145 or CandidateID is null GROUP BY ID,SalaryHeadName,Amount
)
SELECT
ID,SalaryHeadName, Amount, COUNT(Amount) as cnt INTO #RESULT_TABLE FROM
cte GROUP BY
ID,SalaryHeadName, Amount;
WITH RM_DUPL AS
(SELECT ROW_NUMBER OVER(PARTITION BY ID ORDER BY AMOUNT NULLS LAST) AS RN, ID,SalaryHeadName, Amount, cnt FROM #RESULT_TABLE) SELECT FROM RM_DUPL WHERE RN = 1 or AMOUNT IS NOT NULL; END;
答案 1 :(得分:0)
并非所有人都知道UNION运算符删除了重复项。当您想删除重复数据时,这很方便。如下:
SELECT ColA, ColB, ColC
FROM TableWithDuplicates
UNION
SELECT ColA, ColB, ColC
FROM TableWithDuplicates
Result是TableWithDuplicates的重复数据删除结果集。
答案 2 :(得分:0)
我希望以下查询能够满足您在“期望的输出图像”中提到的需求。
SELECT SH.Id SalaryHeadId, SH.SalaryHeadName, ISNULL(Tr.Amount, 0.00) PerMonth,
ISNULL(Tr.Amount, 0.00)*12 PerAnnum
FROM Sal_Head SH
LEFT JOIN Transaction_Table Tr ON SH.Id = Tr.SalaryHeadId
WHERE Tr.CandidateId = 145