## Derived Column with Rank and Qualify

## Derived Column with Rank and Qualify

(OP)

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 [table]

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?

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 [table]

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?

## RE: Derived Column with Rank and Qualify

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 [table]

GROUP BY Name

)

ORDER BY Total_Sum DESC

QUALIFY Total_Sum_Rank <= 10

;

## RE: Derived Column with Rank and Qualify

\

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 [table]

GROUP BY Name

)

ORDER BY Total_Sum DESC

QUALIFY Total_Sum_Rank <= 10

;

i'll tinker with it some more.

## RE: Derived Column with Rank and Qualify

I forgot to give a name to the derived table.

Only add "as XXX" between ")" and "ORDER" and it will be fixed !