使用汇总的功能摘要 -

时间:2016-10-24 22:43:34

标签: sql sql-server-2008 ssms

我正在尝试修改我发现使用select语句而不是视图的汇总查询。汇总是:

select case when (grouping(lut_XXX_type_name) = 1) then 'ALL'
    else ISNULL(lut_XXX_type_name, 'unknown')
    end as lut_XXX_type_name,
    case when (GROUPING(lut_YYY_status_name) = 1)then 'ALL'
        ELSE ISNULL(lut_YYY_status_name, 'UNKNOWN')
        end as lut_YYY_status_name,
        SUM (ZZZ_quantity) as qtySum
    from database.dbo.View_permitted
    group by lut_XXX_type_name,lut_YYY_status_name with rollup

我想用以下内容替换'from database.dbo.View_permitted',所以我把它放在括号中:

SELECT      dbo.WWW.WWW_id, dbo.WWW.WWW_grantee, dbo.YYY_entity.YYY_entity_apn, dbo.lut_YYY_status.lut_YYY_status_name, 
                      dbo.lut_entity_type.lut_entity_type_name, dbo.lut_XXX_type.lut_XXX_type_name, dbo.county.county_name, dbo.ZZZ.ZZZ_quantity, 
                      dbo.YYY.YYY_city, dbo.YYY.YYY_term, dbo.YYY.YYY_date_executed
FROM         dbo.WWW INNER JOIN
                      dbo.YYY ON dbo.WWW.WWW_id = dbo.YYY.YYY_WWW_id INNER JOIN
                      dbo.lut_YYY_status ON dbo.YYY.YYY_lut_YYY_status_id = dbo.lut_YYY_status.lut_YYY_status_id INNER JOIN
                      dbo.ZZZ ON dbo.WWW.WWW_id = dbo.ZZZ.ZZZ_WWW_id INNER JOIN
                      dbo.lut_XXX_type ON dbo.ZZZ.ZZZ_lut_XXX_type_id = dbo.lut_XXX_type.lut_XXX_type_id INNER JOIN
                      dbo.YYY_entity ON dbo.YYY.YYY_id = dbo.YYY_entity.YYY_entity_YYY_id INNER JOIN
                      dbo.lut_entity_type ON dbo.YYY_entity.YYY_entity_lut_entity_type_id = dbo.lut_entity_type.lut_entity_type_id INNER JOIN
                      dbo.county ON dbo.WWW.WWW_county_id = dbo.county.county_id
WHERE     (dbo.ZZZ.ZZZ_lut_XXX_type_id IN (20, 21, 22, 23, 24, 65, 66)) AND (dbo.county.county_name IN ('County1', 'county2')) AND 
                      (dbo.YYY_entity.YYY_entity_WWW <> 0) AND (dbo.lut_YYY_status.lut_YYY_status_name IN ('Active', 'permitted’))

执行时,我收到以下错误:

   Msg 156, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'group'.
Msg 319, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

我已阅读有关使用分号的线程,我将结尾修改为:

group by rollup(lut_XXX_type_name,lut_YYY_status_name)

但它仍无效...

感谢您的帮助 -

1 个答案:

答案 0 :(得分:0)

试试这个:

group by rollup(lut_XXXXX_type_name, lut_YYYYY_status_name)

with rollup用于其他一些数据库。