I have the following data in a #temp table:
Id code Fname CompanyId FieldName Value
----------------------------------------------------------------
465 00133 JENN WILSON 1 ERA 1573
465 00133 JENN WILSON 1 ESHIFTALLOW 3658
465 00133 JENN WILSON 1 NETPAY 51560
I want to do following operation ie
One Row will be addition on two columns ie ERA + ESHIFTALLOW
Other Row will be subtraction & addition on three columns ie NETPAY - ERA + ESHIFTALLOW
I had tried using case statement in SQL Server.
Following is the output required
where Field1= ERA + ESHIFTALLOW
& Filed2=NETPAY - ERA + ESHIFTALLOW
Id code Fname CompanyId FieldName Value
----------------------------------------------------------------
465 00133 JENN WILSON 1 Field1 5231
465 00133 JENN WILSON 1 Filed2 46329
I had tried using SQL SERVER Case Statement but not getting proper output SQL Query : Aggregate option in SQL Server CASE statement
I see at least 2 methods to get those results. A group by or a pivot
In the example below the 2 methods are shown.
CREATE TABLE #Temp (Id INT, code VARCHAR(5), Fname VARCHAR(20), CompanyId INT, FieldName VARCHAR(20), Value INT);
insert into #Temp (Id, code, Fname, CompanyId, FieldName, Value)
values
(465,00133,'JENN WILSON',1,'ERA',1573),
(465,00133,'JENN WILSON',1,'ESHIFTALLOW',3658),
(465,00133,'JENN WILSON',1,'NETPAY',51560);
with Q AS (
SELECT Id, code, Fname, CompanyId,
sum(case when FieldName = 'ERA' then Value end) as ERA,
sum(case when FieldName = 'ESHIFTALLOW' then Value end) as ESHIFTALLOW,
sum(case when FieldName = 'NETPAY' then Value end) as NETPAY
from #Temp
group by Id, code, Fname, CompanyId
)
select Id, code, Fname, CompanyId, 'Field1' as FieldName, (ERA + ESHIFTALLOW) as Value from Q
union all
select Id, code, Fname, CompanyId, 'Field2', (NETPAY - ERA + ESHIFTALLOW) from Q
;
with Q AS (
SELECT Id, code, Fname, CompanyId,
(ERA + ESHIFTALLOW) as Field1,
(NETPAY - ERA + ESHIFTALLOW) as Field2
FROM (SELECT * FROM #Temp) s
PIVOT ( SUM(VALUE) FOR FieldName IN (ERA, ESHIFTALLOW, NETPAY)) p
)
select Id, code, Fname, CompanyId, 'Field1' as FieldName, Field1 as Value from Q
union all
select Id, code, Fname, CompanyId, 'Field2', Field2 from Q
;
Note that SUM(VALUE) was used instead of MAX(VALUE). In this case it will yield the same results. It's just a choice really.
Building heavily on LukStorms' answer, you can use a PIVOT and an UNPIVOT to get the results you want:
CREATE TABLE #Temp
(Id INT, Code VARCHAR(5), Fname VARCHAR(20), CompanyId INT, FieldName VARCHAR(20), Value INT);
INSERT INTO #Temp
(Id, Code, Fname, CompanyId, FieldName, Value)
VALUES
(465,00133, 'JENN WILSON', 1, 'ERA', 1573),
(465,00133, 'JENN WILSON', 1, 'ESHIFTALLOW', 3658),
(465,00133, 'JENN WILSON', 1, 'NETPAY', 51560);
SELECT Id, Code, Fname, CompanyId, FieldName, Value
FROM (
SELECT Id, Code, Fname, CompanyId,
ERA + ESHIFTALLOW AS Field1,
NETPAY - ERA + ESHIFTALLOW AS Field2
FROM (
SELECT *
FROM #Temp
) AS s
PIVOT (
SUM(Value)
FOR FieldName IN (ERA, ESHIFTALLOW, NETPAY)
) AS p
) AS r
UNPIVOT (
Value
FOR FieldName IN (Field1, Field2)
) AS u
;
I have no idea whether this solution is anywhere near the most efficient, but it should work:
SELECT
BASE.*,
ERA.Value AS ERA,
ESALLOW.Value AS ESHIFTALLOW,
ERA.Value + ESALLOW.Value AS Field1,
etc...
FROM (
SELECT DISTINCT Id, code, Fname, CompanyId
FROM #TEMP ) BASE
LEFT OUTER JOIN (
SELECT Id, Value
FROM #TEMP
WHERE FieldName = 'ERA' ) ERA
ON BASE.Id = ERA.Id
LEFT OUTER JOIN (
SELECT Id, Value
FROM #TEMP
WHERE FieldName = 'ESHIFTALLOW' ) ESALLOW
ON BASE.Id = ESALLOW.Id
This gives you a simple table that has every type of value in a separate column, instead of in separate rows. This makes calculations possible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.