Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Derived Column with Rank and Qualify

Status
Not open for further replies.

Novs

Technical User
Nov 2, 2005
2
US
I am using teradata.

This is what I want to do:

SELECT

'Total ' as Row_One
, Name
, Sum (Column2) Column2_Sum
, Sum (Column3) Column3_Sum
, Sum (Column4) Column4_Sum
, (Column2_Sum + Column3_Sum + Column4_Sum) Total_Sum
, Rank (Total_Sum) Total_Sum_Rank

FROM


GROUP BY Name

ORDER BY Total_Sum DESC

Qualify Total_Sum_Rank <= 10

i know this code won't work because you can't + in a select. how can i duplicate the logic?
 
Try this :
SELECT 'Total ' AS Row_One
, Name
, Column2_Sum
, Column3_Sum
, Column4_Sum
, Total_Sum
, RANK(Total_Sum) Total_Sum_Rank
FROM
( SELECT Name
, SUM (Column2) AS Column2_Sum
, SUM (Column3) AS Column3_Sum
, SUM (Column4) AS Column4_Sum
, (Column2_Sum + Column3_Sum + Column4_Sum) Total_Sum
FROM

GROUP BY Name
)
ORDER BY Total_Sum DESC
QUALIFY Total_Sum_Rank <= 10
;
 
al1024
\
thanks a lot. Your fix looks excellent. I try to run it, but teradata gives me a 'expecting something like a UDFCALLNAME keyword error between the ) and ORDER. So now I need to work that out. Also, I changed the name of the first column from ROW_ONE to COLUMN_ONE and tried adding that column to the subquery.

so it looks like this:

SELECT 'Total ' AS Column_One
, Name
, Column2_Sum
, Column3_Sum
, Column4_Sum
, Total_Sum
, RANK(Total_Sum) Total_Sum_Rank
FROM
( SELECT 'Total ' AS Column_One
, Name
, SUM (Column2) AS Column2_Sum
, SUM (Column3) AS Column3_Sum
, SUM (Column4) AS Column4_Sum
, (Column2_Sum + Column3_Sum + Column4_Sum) Total_Sum
FROM

GROUP BY Name
)
ORDER BY Total_Sum DESC
QUALIFY Total_Sum_Rank <= 10
;

i'll tinker with it some more.
 
I'm confused...
I forgot to give a name to the derived table.
Only add "as XXX" between ")" and "ORDER" and it will be fixed !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top