在“关键字GROUP”附近的语法错误方面需要帮助

时间:2011-09-08 04:59:25

标签: sql group-by subquery union case

我不知道我做错了什么。我一直得到不正确的语法错误,似乎我纠正了一个,然后下一行弹出。现在我得到一个“关键字GROUP附近的语法不正确。我在这个网站上有几个版本的问题。到目前为止,我非常感谢你的帮助。我只需要提升这个报告就可以了。任何人都知道任何问题。明尼苏达州的高级课程。这些书涵盖了基础知识,我需要复杂的sql类。

SELECT fgc, sum(Totpay) as Totpay, Sum(TotChg) as TotChg
from
(                     
    SELECT fgc, sum(pay) as TotPay, 0 as TotChg
    from 
    (
        SELECT fgc, pay,
               CASE 
                   WHEN  [date]<= 30 THEN 'pmt 0-30'      
                   WHEN [date]> 30 AND [date] <= 60 THEN 'pmt 30-60'
                   WHEN [date]> 61 AND [date] <= 90 THEN 'pmt 61-90'
                   WHEN [date]> 91 AND [date] <= 120 THEN 'pmt 91-120'
                   WHEN [date]> 121 AND [date] <= 150 THEN 'pmt 121-150'
                   WHEN [date]> 151 AND [date] <= 180 THEN 'pmt 151-180'
                   ELSE 'pmt 181+' 
               END
        FROM @Pay 
        GROUP BY fgc

        UNION

        SELECT fgc, 0 as TotPay, sum(chg) as TotChg
        from
        (
            SELECT fgc, chg,
                   CASE 
                       WHEN [date]<= 30 THEN 'charge 0-30'      
                       WHEN [date]> 30 AND [date] <= 60 THEN 'charge 30-60' 
                       WHEN [date]> 61 AND [date] <= 90 THEN 'charge 61-90'
                       WHEN [date]> 91 AND [date] <= 120 THEN 'charge 91-120'
                       WHEN [date]> 121 AND [date] <= 150 THEN 'charge 121-150'
                       WHEN [date]> 151 AND [date] <= 180 THEN 'charge 151-180'
                       ELSE 'charge 181+' 
                   END
            FROM @Chg
        )
        GROUP BY fgc 
    )
    GROUP BY fgc

2 个答案:

答案 0 :(得分:3)

您没有为内联查询使用别名。以下没有给我任何语法错误。

        SELECT fgc, sum(Totpay) as Totpay, Sum(TotChg) as TotChg
        from
        (                     
        SELECT fgc, sum(pay) as TotPay, 0 as TotChg
        from 
            (SELECT fgc, pay,
                CASE 
                WHEN  [date]<= 30 THEN 'pmt 0-30'      
                WHEN [date]> 30 AND [date] <= 60 THEN 'pmt 30-60'
                WHEN [date]> 61 AND [date] <= 90 THEN 'pmt 61-90'
                WHEN [date]> 91 AND [date] <= 120 THEN 'pmt 91-120'
                WHEN [date]> 121 AND [date] <= 150 THEN 'pmt 121-150'
                WHEN [date]> 151 AND [date] <= 180 THEN 'pmt 151-180'
                ELSE 'pmt 181+' 
                END
            FROM @Pay 
            GROUP BY fgc 

            UNION

            SELECT fgc, 0 as TotPay, sum(chg) as TotChg
            from
                (SELECT fgc, chg,
                    CASE 
                    WHEN [date]<= 30 THEN 'charge 0-30'      
                    WHEN [date]> 30 AND [date] <= 60 THEN 'charge 30-60' 
                    WHEN [date]> 61 AND [date] <= 90 THEN 'charge 61-90'
                    WHEN [date]> 91 AND [date] <= 120 THEN 'charge 91-120'
                    WHEN [date]> 121 AND [date] <= 150 THEN 'charge 121-150'
                    WHEN [date]> 151 AND [date] <= 180 THEN 'charge 151-180'
                    ELSE 'charge 181+' 
                    END
                 FROM @Chg) as innerTable2
             GROUP BY fgc 
            ) As innertable1
        GROUP BY fgc
    )  as outerTable

答案 1 :(得分:0)

基本上,GROUP BY关键字用于使用聚合函数的情况,并且为了使用它,您必须使用聚合函数对所有列进行分组。

例如:而不是GROUP BY fgc,应使用GROUP BY(fgc,pay,aliasName)。

变化:

SELECT fgc,pay,                 案件                 当[日期]&lt; = 30那么'pmt 0-30'
                当[日期]&gt; 30和[日期]&lt; = 60那么'pmt 30-60'                 当[日期]&gt; 61 AND [date]&lt; = 90 THEN'pmt 61-90'                 当[日期]&gt; 91 AND [date]&lt; = 120 THEN'pmt 91-120'                 当[日期]&gt; 121 AND [date]&lt; = 150 THEN'pmt 121-150'                 当[日期]&gt; 151 AND [date]&lt; = 180 THEN'pmt 151-180'                 ELSE'pmt 181+'                 结束             来自@Pay             GROUP BY fgc

进入

SELECT fgc,pay,                 案件                 当[日期]&lt; = 30那么'pmt 0-30'
                当[日期]&gt; 30和[日期]&lt; = 60那么'pmt 30-60'                 当[日期]&gt; 61 AND [date]&lt; = 90 THEN'pmt 61-90'                 当[日期]&gt; 91 AND [date]&lt; = 120 THEN'pmt 91-120'                 当[日期]&gt; 121 AND [date]&lt; = 150 THEN'pmt 121-150'                 当[日期]&gt; 151 AND [date]&lt; = 180 THEN'pmt 151-180'                 ELSE'pmt 181+'                 END作为别名             来自@Pay             GROUP BY(fgc,pay,alias)

希望得到这个帮助。

P / S:请Gabe纠正我。