Not sure what you mean, but I think you are looking for something like this:
Case
when A_staff_Service_Type.type = 'v' and pt_service.service_code = 'N06'
then isnull(Indirect_Duration,0)/60.00 + isnull(direct_Duration,0)/60.00
else null
end
I also don't know if the AND
is correct for your use-case. Maybe you want an OR
- but based on the little information you have provided this is impossible to answer.
The obvious answer to "I want the results added" is:
Case when A_staff_Service_Type.type = 'v'
then isnull(Indirect_Duration,0)/60.00 else 0 end
+
Case when pt_service.service_code = 'N06 '
then isnull(direct_Duration,0)/60.00 else 0 End
This query uses both UNPIVOT and then PIVOT:
SELECT piv.[Data], piv.x, piv.y
FROM (
SELECT [Type], ColA = CAST(ColA as varchar(10)), ColB = CAST(ColB as varchar(10)), ColC = CAST(ColC as varchar(10)), ColD = CAST(ColD as varchar(10))
FROM @data
) d
UNPIVOT (
[value] FOR [Data] IN (ColA, ColB, ColC, ColD)
) as unpiv
PIVOT (
MAX([value])
FOR [Type] IN ([x], [y])
) as piv
;
- It first unpivots your data in order to get a regular table
- It then pivots this table back to the new required format
I have to cast everything as varchar. Based on your data model, you may have to cast them to another type. An aggregate function is required by PIVOT. Since this is a 1 to 1 match, it works with either of MAX or MIN.
With SQL Server >= 2008 you can also replace UNPIVOT by Table Value Constructor and CROSS APPLY along with PIVOT:
SELECT piv.[Data], piv.x, piv.y
FROM (
SELECT v.[Type], [value], [Data]
FROM @data d
CROSS APPLY (values
(d.[type], CAST(d.ColA as varchar(10)), 'ColA')
, (d.[type], CAST(d.ColB as varchar(10)) , 'ColB')
, (d.[type], CAST(d.ColC as varchar(10)), 'ColC')
, (d.[type], CAST(d.ColD as varchar(10)), 'ColD')
) as v([type], [value], [Data])
) unpiv
PIVOT (
MAX([value])
FOR [Type] IN ([x], [y])
) as piv
;
Without PIVOT/UNPIVOT you can use this query with any (old) version of SQL Server:
SELECT [Data]
, [x] = MAX(CASE WHEN [type] = 'x' THEN [value] END)
, [y] = MAX(CASE WHEN [type] = 'y' THEN [value] END)
FROM (
SELECT [type], [value] = CAST(ColA as varchar(10)), [Data] = 'ColA' FROM @data
UNION ALL
SELECT [type], [value] = CAST(ColB as varchar(10)), [Data] = 'ColB' FROM @data
UNION ALL
SELECT [type], [value] = CAST(ColC as varchar(10)), [Data] = 'ColC' FROM @data
UNION ALL
SELECT [type], [value] = CAST(ColD as varchar(10)), [Data] = 'ColD' FROM @data
) as v
GROUP BY [Data]
Output:
Data x y
ColA 123456 654321
ColB $500 $200
ColC 6 36
ColD 30 90
Data:
Declare @data table([Type] char(1), ColA bigint, ColB varchar(10), ColC int, ColD int);
INSERT INTO @data([Type], ColA, ColB, ColC, ColD) VALUES
('x', 123456, '$500', 6, 30)
, ('Y', 654321, '$200', 36, 90);
Best Answer
You have transposed your desired result so it is rather straightforward to achieve what you want:
Now you can map each storenum / notes combination to a text, something like: