August 15, 2014 at 2:58 pm
I can't speak for anyone else but I consider MAX to be a form of "aggregation" even when it applies to character based values.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 15, 2014 at 3:35 pm
Jeff Moden (8/15/2014)
I can't speak for anyone else but I consider MAX to be a form of "aggregation" even when it applies to character based values.
I agree, anything one can supply a value without a group by in as grouped set is an aggregate. To make things even more twisted, in this case the aggregation order is set by the collation. Of course the function domain is sparsely populated missing simple things like first, next or last (by a chosen order directive) leaving one to use the MAX as NULL is funny enough considered the lowest value by SQL Server. The extended OVER clause in 2012 and later still has the implied lowest value for NULLs although it provides the first/next/last functionality.
😎
August 15, 2014 at 3:43 pm
Of course max() is aggregation; I don't think that was ever in question. However for the purposes of what's trying to be accomplished here, using max() in the context of a pivot gives you the same results if you avoid using it and utilized something like a cross-tab pivot.
August 15, 2014 at 4:07 pm
Yes, in the sense, MAX is in fact Aggregating. I think it's because of WHAT you can do with it is why it's looked at / considered as "not" really aggregating.
Ok JeeLee, I'll try your suggestion.
Thank you.
August 15, 2014 at 4:09 pm
JeeTee (8/15/2014)
Of course max() is aggregation; I don't think that was ever in question. However for the purposes of what's trying to be accomplished here, using max() in the context of a pivot gives you the same results if you avoid using it and utilized something like a cross-tab pivot.
The reason I brought it up is because the title of this post is "Pivot without aggregate function" and the link the OP provided is titled "Pivoting without aggregation". I just want to make sure that neophytes browsing this thread and the OP understand that MAX is, indeed, an aggregate.
Also, a CROSS-TAB pivot uses aggregations to do its job if you want items to appear on the same line.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 15, 2014 at 4:12 pm
Why on Earth do you need to do this without aggregates? As JeeTee and some of the others have pointed out, it's one of the more efficient methods in T-SQL whether you use a PIVOT or a CROSS-TAB to do it. If I knew the reason, I might give it a shot.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 15, 2014 at 4:17 pm
I don't. I used the title that the link I found I posted states.
Just went with it.
I know that in Access you can do first or last as Value in Cross-tab and get the coach name. BUT your limited to one value column.
I just want to be able to produce the results sought.
List the coaches, when there is more than one, for each Team (TeamID)
HOW is not that big a deal to me as getting that result.:-P
August 15, 2014 at 4:24 pm
serviceaellis (8/15/2014)
I don't. I used the title that the link I found I posted states.Just went with it.
I know that in Access you can do first or last as Value in Cross-tab and get the coach name. BUT your limited to one value column.
I just want to be able to produce the results sought.
List the coaches, when there is more than one, for each Team (TeamID)
HOW is not that big a deal to me as getting that result.:-P
Ah. Got it. Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 18, 2014 at 10:05 am
So close.
Found info and got this far with a stored prodecure
DECLARE @SQL nvarchar(max)
SET @SQL = N'SELECT TeamID'
SELECT @SQL = @SQL + ',MAX(CASE WHEN rownum='+CAST(a.rownum AS CHAR(5))+' THEN CoachFN ELSE '''' END) AS [CoachFN'+CAST(a.rownum AS CHAR(5))+']'
FROM dbo.vTeamCoaches a GROUP BY a.rownum
ORDER BY a.rownum
SET @SQL = @SQL + N'
FROM vTeamCoaches
GROUP BY Teamid
ORDER BY Teamid'
PRINT @SQL
EXEC sp_executesql @SQL
[/code]
When I run it get the results I need EXCEPT
1) I need to also have the CoachLN and CoachEmail to show as columns just like the CoachFN
2) How do I do the above in a SQL View? I need it to run reports on the results.
I saved the stored procedure but can't seem to call it out? In fact I don't see in under Programmability | Stored Procedures?
See attached Function result, result comparison to SQL View having the data set, result, and result compare -02
How do I create a view calling out the stored procedure?
I want to generate a view with the results the stored procedure is producing.
August 20, 2014 at 10:29 am
For anyone else needing a solution
Got this to save as a SQL View thanks to Brian
NOTE: the ColName fields must be the same data type.
WITH Base AS
(SELECT RowNum, TeamID, PersonID, CONVERT(varchar(100),CoachFN) AS CoachFN, CONVERT(varchar(100),CoachLN) AS CoachLN, CoachEmail
FROM vTeamCoaches), norm AS
(SELECT TeamID, ColName + CONVERT(varchar, RowNum) AS ColName, ColValue
FROM Base
UNPIVOT (ColValue FOR ColName IN ([CoachFN], [CoachLN],[CoachEmail])) AS pvt)
SELECT *
FROM norm
PIVOT (MIN(ColValue) FOR ColName IN ([CoachFN1], [CoachLN1],[CoachEmail1],
[CoachFN2], [CoachLN2],[CoachEmail2],
[CoachFN3], [CoachLN3],[CoachEmail3],
[CoachFN4], [CoachLN4],[CoachEmail4])) AS pvt
Viewing 10 posts - 16 through 24 (of 24 total)
You must be logged in to reply to this topic. Login to reply