从select语句和union all中选择

时间:2011-09-15 14:50:32

标签: sql sql-server tsql

当我执行查询时

SELECT Settlement_Fees.Participant_Name, Settlement_Fees.Account, Settlement_Fees.Billing_Account, Settlement_Fees.Descr1, Settlement_Fees.Market, Settlement_Fees.Instrum
--, IIf(Settlement_Fees.Instr_Type='Internal','bsinternal',Settlement_Fees.Instr_Type) AS Expr1
,(case when Settlement_Fees.Instr_Type='Internal' then 'bsinternal'
  else Settlement_Fees.Instr_Type
 end )
, Settlement_Fees.Country, Settlement_Fees.Nr_Instr_Business_Unit, Settlement_Fees.Nr_Instr_Account, Settlement_Fees.Avg_EUR_Rate, Settlement_Fees.Fee_Amount_EUR, Settlement_Fees.Value_Date_Adj
FROM Settlement_Fees)
union all
 (select '','',Billing_Account,'','','',
( case when Instr_Type like '%Bridge%' or Instr_Type = '%Internal%' then 'Bszridge/Internal'
else  Instr_Type
end )
 ,'','',Nr_Instr_Account  ,'',Fee_Amount_EUR ,'' 
from Settlement_Fees group by Settlement_Fees.Billing_Account,Settlement_Fees.Instr_Type
,Settlement_Fees.Nr_Instr_Account,Fee_Amount_EUR)
union all 
(select '','',Billing_Account,'','','','Total','','',sum(Nr_Instr_Account),'',sum(Fee_Amount_EUR) ,''
from Settlement_Fees group by Billing_Account

它工作正常

但当我用select * from()执行时,它给我错误“Msg 102,Level 15,State 1,Line 33 ')'。附近的语法不正确。“用于以下查询

SELECT *
FROM  ((SELECT settlement_fees.participant_name,
               settlement_fees.ACCOUNT,
               settlement_fees.billing_account,
               settlement_fees.descr1,
               settlement_fees.market,
               settlement_fees.instrum
               --, IIf(Settlement_Fees.Instr_Type='Internal','bsinternal',Settlement_Fees.Instr_Type) AS Expr1
               ,
               ( CASE
                   WHEN settlement_fees.instr_type = 'Internal' THEN
                   'bsinternal'
                   ELSE settlement_fees.instr_type
                 END ),
               settlement_fees.country,
               settlement_fees.nr_instr_business_unit,
               settlement_fees.nr_instr_account,
               settlement_fees.avg_eur_rate,
               settlement_fees.fee_amount_eur,
               settlement_fees.value_date_adj
        FROM   settlement_fees)
       UNION ALL
       (SELECT '',
               '',
               billing_account,
               '',
               '',
               '',
               ( CASE
                   WHEN instr_type LIKE '%Bridge%'
                         OR instr_type = '%Internal%' THEN 'Bszridge/Internal'
                   ELSE instr_type
                 END ),
               '',
               '',
               nr_instr_account,
               '',
               fee_amount_eur,
               ''
        FROM   settlement_fees
        GROUP  BY settlement_fees.billing_account,
                  settlement_fees.instr_type,
                  settlement_fees.nr_instr_account,
                  fee_amount_eur)
       UNION ALL
       (SELECT '',
               '',
               billing_account,
               '',
               '',
               '',
               'Total',
               '',
               '',
               SUM(nr_instr_account),
               '',
               SUM(fee_amount_eur),
               ''
        FROM   settlement_fees
        GROUP  BY billing_account)) 

4 个答案:

答案 0 :(得分:2)

您需要为派生表提供表别名。例如将as t添加到查询的最后。

另外修复了评论中提出的问题

( CASE
                   WHEN settlement_fees.instr_type = 'Internal' THEN
                   'bsinternal'
                   ELSE settlement_fees.instr_type
                 END )

   CASE
       WHEN settlement_fees.instr_type = 'Internal' THEN
       'bsinternal'
       ELSE settlement_fees.instr_type
     END AS Foo

答案 1 :(得分:2)

您应该命名您的子查询:

选择* from()子查询名称

 select * from(
(SELECT Settlement_Fees.Participant_Name, Settlement_Fees.Account, Settlement_Fees.Billing_Account, Settlement_Fees.Descr1, Settlement_Fees.Market, Settlement_Fees.Instrum
--, IIf(Settlement_Fees.Instr_Type='Internal','bsinternal',Settlement_Fees.Instr_Type) AS Expr1
,(case when Settlement_Fees.Instr_Type='Internal' then 'bsinternal'
  else Settlement_Fees.Instr_Type
 end )
, Settlement_Fees.Country, Settlement_Fees.Nr_Instr_Business_Unit, Settlement_Fees.Nr_Instr_Account, Settlement_Fees.Avg_EUR_Rate, Settlement_Fees.Fee_Amount_EUR, Settlement_Fees.Value_Date_Adj
FROM Settlement_Fees)
union all
 (select '','',Billing_Account,'','','',
( case when Instr_Type like '%Bridge%' or Instr_Type = '%Internal%' then 'Bszridge/Internal'
else  Instr_Type
end )
 ,'','',Nr_Instr_Account  ,'',Fee_Amount_EUR ,'' 
from Settlement_Fees group by Settlement_Fees.Billing_Account,Settlement_Fees.Instr_Type
,Settlement_Fees.Nr_Instr_Account,Fee_Amount_EUR)
union all 
(select '','',Billing_Account,'','','','Total','','',sum(Nr_Instr_Account),'',sum(Fee_Amount_EUR) ,''
from Settlement_Fees group by Billing_Account
)) subqueryName

答案 2 :(得分:1)

您有一组额外的()并且别名您的内联视图/派生表也有助于例如

select * from(
    SELECT 
        Settlement_Fees.Participant_Name, 
        Settlement_Fees.Account, 
        Settlement_Fees.Billing_Account, 
        Settlement_Fees.Descr1, 
        Settlement_Fees.Market, 
        Settlement_Fees.Instrum,
        (case when Settlement_Fees.Instr_Type='Internal' then 'bsinternal'
            else Settlement_Fees.Instr_Type
        end ) as SomthingHere, 
        Settlement_Fees.Country, 
        Settlement_Fees.Nr_Instr_Business_Unit, 
        Settlement_Fees.Nr_Instr_Account, 
        Settlement_Fees.Avg_EUR_Rate, 
        Settlement_Fees.Fee_Amount_EUR, 
        Settlement_Fees.Value_Date_Adj
    FROM Settlement_Fees
    UNION ALL select 
        '',
        '',
        Billing_Account,
        '',
        '',
        '',
        ( case when Instr_Type like '%Bridge%' or Instr_Type = '%Internal%' then 'Bszridge/Internal'
        else  Instr_Type
        end ),
        '',
        '',
        Nr_Instr_Account  ,
        '',
        Fee_Amount_EUR ,
        '' 
    from 
        Settlement_Fees 
    group by 
        Settlement_Fees.Billing_Account,
        Settlement_Fees.Instr_Type,
        Settlement_Fees.Nr_Instr_Account,
        Fee_Amount_EUR
    union all select 
        '',
        '',
        Billing_Account,
        '',
        '',
        '',
        'Total',
        '',
        '',
        sum(Nr_Instr_Account),
        '',
        sum(Fee_Amount_EUR) ,
        ''
    from 
        Settlement_Fees 
    group by 
        Billing_Account
) as foo

答案 3 :(得分:1)

您需要为派生表命名。无所谓。例如:

SELECT
   ...
FROM
   (...derived table) myDerivedTableName