代码之家  ›  专栏  ›  技术社区  ›  Niko Gamulin

选择为问题

sql
  •  4
  • Niko Gamulin  · 技术社区  · 14 年前

    我试图执行以下查询:

    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
    

    有人知道如何解决上面的问题吗?

    4 回复  |  直到 14 年前
        1
  •  7
  •   gbn    14 年前
    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
    
        2
  •  5
  •   a1ex07    14 年前

    不能在中使用别名 WHERE 条款。你需要重复整个表达( CAST(t1.[total_event_duration] AS DECIMAL))/(CAST (t2.[total_events_duration] AS DECIMAL)>0.02 ).

        3
  •  3
  •   Ian Henry    14 年前

    where , group by ,或 having 条款。可以通过将其包装在子查询中来解决此问题:

    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]
    ) a
    WHERE a.buddy_strength > 0.02
    

        4
  •  2
  •   rosscj2533    14 年前

    不能在where子句中使用别名列。我认为您必须在where子句中重新生成派生字段的值,如下所示:

    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 (CAST(t1.[total_event_duration] AS DECIMAL))/(CAST (t2.[total_events_duration] AS DECIMAL)) > 0.02