February 21, 2011 at 2:08 pm
Before anyone asks, I did use search before deciding to post this but I didn't find anything to solve what I was looking for.
I have data that I want to see in the tabular format (hence the Pivot hehe) but the issue I'm having is I've been unable to have the aggregate column contain the data as I need to present it. I'm attempting to combine two different aggregated functions (MIN and MAX) in the pivot with a divider in the middle of each function ("~")
Here is the code I use in a non-tabular format and I wish to keep the format I'm trying to get the same in the Pivot query.
CONVERT(VARCHAR(3),MIN(InDay)) + '~' + CONVERT(VARCHAR(3),MAX(InDay))
So it would look like 1~4 or 8~8
Any suggestion? Or additional information you need please let me know
February 21, 2011 at 2:15 pm
Darundo (2/21/2011)
...Any suggestion? Or additional information you need please let me know
Please provide table definition, sample data and expected results based on those sample.
February 21, 2011 at 3:34 pm
Darundo (2/21/2011)
Before anyone asks, I did use search before deciding to post this but I didn't find anything to solve what I was looking for.I have data that I want to see in the tabular format (hence the Pivot hehe) but the issue I'm having is I've been unable to have the aggregate column contain the data as I need to present it. I'm attempting to combine two different aggregated functions (MIN and MAX) in the pivot with a divider in the middle of each function ("~")
Here is the code I use in a non-tabular format and I wish to keep the format I'm trying to get the same in the Pivot query.
CONVERT(VARCHAR(3),MIN(InDay)) + '~' + CONVERT(VARCHAR(3),MAX(InDay))
So it would look like 1~4 or 8~8
Any suggestion? Or additional information you need please let me know
The use of a CROSS-TAB would be faster than PIVOT and probably just as easy. See the following article...
http://www.sqlservercentral.com/articles/T-SQL/63681/
--Jeff Moden
Change is inevitable... Change for the better is not.
February 21, 2011 at 4:07 pm
I was a little bored, so I'm going to throw a fish...
declare @sample table (class Char(1), value int)
insert into @sample
select 'A',3 union all
select 'A',19 union all
select 'B',8 union all
select 'A',3 union all
select 'A',12 union all
select 'B',1 union all
select 'B',21 union all
select 'A',14 union all
select 'A',6 union all
select 'B',11 union all
select 'B',6
-- cross-tab of result column returned from subquery "dt"
select max(case when class = 'A' then result else null end) as Class_A
,max(case when class = 'B' then result else null end) as Class_B
from (select class, CAST(min(value) as varchar)+'~'+CAST(max(value) as varchar) as result
from @sample
group by class
)dt
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply