2016-10-24 213 views
0

我想修改彙總查詢,我發現使用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 

我想用下面這樣來代替「從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) 

,但它仍然沒有工作...

感謝您的幫助 -

+0

您的示例查詢沒有'with',所以我看不出如何生成該錯誤。 –

+0

在結尾處有'with rollup' – Gross

+0

您是否在您的父親後添加了別名?所以你從'從視圖作爲v'到'從(sql)作爲v' – Beth

回答

0

試試這個:

group by rollup(lut_XXXXX_type_name, lut_YYYYY_status_name) 

with rollup在一些其他數據庫被使用。

+0

'我得到'關鍵字'組'' – Gross

+0

'附近的語法不正確,並且所有字段名都加下劃線,因爲它不知道表是什麼? – Gross

+0

@Gross。 。 。如果它不能識別'group',那麼在查詢中會出現一些錯誤。 –