Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT AS problem

Tags:

sql

I tried to perform the following query:

SELECT t1.[user1], t1.[user2],
    (CAST(t1.[total_event_duration] AS DECIMAL)) / (CAST (t2.[total_events_duration] AS DECIMAL)) AS buddy_strength 
FROM [CDRs].[dbo].[aggregate_monthly_events] AS t1 
INNER JOIN [CDRs].[dbo].[user_monthly_stats] AS t2 
    ON t1.[user1] = t2.[user1]  
WHERE buddy_strength > 0.02

But it returns an error "Invalid column name 'buddy_strength'"

Does anyone know how to fix the query above?

like image 484
Niko Gamulin Avatar asked Dec 04 '22 10:12

Niko Gamulin


2 Answers

SELECT * 
FROM
    (
    SELECT
        t1.[user1], t1.[user2],(CAST(t1.[total_event_duration] AS DECIMAL))/(CAST (t2.[total_events_duration] AS DECIMAL)) AS buddy_strength 
        FROM [CDRs].[dbo].[aggregate_monthly_events] AS t1 
            INNER JOIN [CDRs].[dbo].[user_monthly_stats] AS t2 
                ON t1.[user1] = t2.[user1]  


   ) foo
        WHERE foo.buddy_strength > 0.02
like image 140
gbn Avatar answered Jan 02 '23 08:01

gbn


You cannot use aliases in WHERE clause. You need to repeat the whole expression (CAST(t1.[total_event_duration] AS DECIMAL))/(CAST (t2.[total_events_duration] AS DECIMAL)>0.02).

like image 34
a1ex07 Avatar answered Jan 02 '23 06:01

a1ex07