I want to achieve the following transformation:
Sample Data
SELECT NumWURm,ReportAText,ReportBText,ReportCText,ReportDText,ReportEText,ReportFText
FROM t_SchFacility
WHERE FacID IN (483,485)
Result:
NumWURm ReportAText ReportBText ReportCText ReportDText ReportEText ReportFText
3 Report On venue Warm Up Photo Get Set
2 Report On venue Warm Up Photo
Desired Output
I want to to get the common column values to be shown as it is, in case the values differ, I want to show some default text.
NumWURm ReportAText ReportBText ReportCText ReportDText ReportEText ReportFText
3 Report On venue Warm Up Photo Default Text
This is just the case for my favourite MIN = MAX trick. When MIN and MAX are the same, then there's only one value, and either the MIN or the MAX can be used as THE value.
SELECT
MAX(NumWURm) as NumWURm,
CASE WHEN MIN(ReportAText) = MAX(ReportAText)
THEN MIN(ReportAText)
ELSE 'Default'
END,
CASE WHEN MIN(ReportBText) = MAX(ReportBText)
THEN MIN(ReportBText)
ELSE 'Default'
END,
CASE WHEN MIN(ReportCText) = MAX(ReportCText)
THEN MIN(ReportCText)
ELSE 'Default'
END,
CASE WHEN MIN(ReportDText) = MAX(ReportDText)
THEN MIN(ReportDText)
ELSE 'Default'
END,
CASE WHEN MIN(ReportEText) = MAX(ReportEText)
THEN MIN(ReportEText)
ELSE 'Default'
END,
CASE WHEN MIN(ReportFText) = MAX(ReportFText)
THEN MIN(ReportFText)
ELSE 'Default'
END
FROM t_SchFacility
WHERE FacID IN (483,485)
If you need this to be really specific, you may need to specify a collation option for the string comparison (eg if case difference is significant to you).
The following would produce a single row of output for a non-empty set.
with [Selected] ([NumWURm], [ReportAText], [ReportBText], [ReportCText], [ReportDText], [ReportEText], [ReportFText])
(
select [NumWURm], [ReportAText], [ReportBText], [ReportCText], [ReportDText], [ReportEText], [ReportFText]
from [t_SchFacility]
where [FacID] IN (483, 485)
),
[Number_Selected] ([count])
(
selected count(*)
from [Selected]
),
[ReportAText] ([ReportAText])
(
select s.[ReportAText], n.[count]
from [Selected] as s cross join [Number_Selected] as n
group by s.[ReportAText], n.[count]
having count(*) = n.[count]
),
...
[ReportFText] ([ReportFText])
(
select s.[ReportFText], n.[count]
from [Selected] as s cross join [Number_Selected] as n
group by s.[ReportFText], n.[count]
having count(*) = n.[count]
)
select
max(s.[NumWURm]) as 'NumWuRm',
a.coalesce([ReportAText], 'Default Text') as 'ReportAText',
b.[ReportBText] as 'ReportBText',
c.[ReportCText] as 'ReportCText',
d.[ReportDText] as 'ReportDText',
e.[ReportEText] as 'ReportEText',
f.[ReportFText] as 'ReportFText'
from
[Selected] as s
left outer join
[ReportAText] as a
on (null is null)
left outer join
...
[ReportFText] as f
on (null is null)
This is a code outline and so will require suitable testing and adjustment.
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.