繁体   English   中英

在where子句中分配select语句

[英]Assigning a select statement inside of a where clause

我想知道这样做是否合法?

select coalesce(myField, (select myfield2 from table1))
from table2

我一直试图让这个声明工作几个小时:

 select 
 coalesce( a.TransactionCurrencyId,

            (
            select c.TransactionCurrencyId
            from CRM_accountbase c
            join crm_pricelevelbase a
            on c.defaultpricelevelid=a.pricelevelid
            where a.pricelevelid=(
                            select a.DefaultPriceLevelId 
                            from
                                 (
                                     select a.DefaultPriceLevelId,c.iCompanyId
                                     from crm_accountbase a
                                     join onyx..company C
                                     on c.iCompanyId=a.accountnumber
                                 ) a
                                 where a.iCompanyId=c.iCompanyId
                                 ) 
            ) TransactionCurrencyId
 from mytable a

问题不在于逻辑。 这是语法。

是否可以在coalesce中使用select语句,在where条件中使用另一个select语句?

是的,可以选择coalesce内部和where语句内部(假设如果你比较相等则它们只返回一行)。

请参阅代码底部附近的内联注释。

此外,它不是语法问题,但最好不要将三个不同的表别名为'a'。

select 
 coalesce( a.TransactionCurrencyId,
            (
                 select c.TransactionCurrencyId
                 from CRM_accountbase c
                 join crm_pricelevelbase a
                 on c.defaultpricelevelid=a.pricelevelid
                 where a.pricelevelid=(
                            select a.DefaultPriceLevelId 
                            from
                                 (
                                     select a.DefaultPriceLevelId,c.iCompanyId
                                     from crm_accountbase a
                                     join onyx..company C
                                     on c.iCompanyId=a.accountnumber
                                 ) a 
                              where a.iCompanyId=c.iCompanyId
                              ) 
            ) --TransactionCurrencyId   <--Need to not alias the subquery here
          )  --< Need to add this parenthesis
 from mytable a

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM