惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
罗磊的独立博客
量子位
Jina AI
Jina AI
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
美团技术团队
雷峰网
雷峰网
爱范儿
爱范儿
S
SegmentFault 最新的问题
小众软件
小众软件
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - Gerald1983

[转载]AJAX 框架 用 Asp.net ajax 还是 Jquery ? [转]触发器 Excel导出报错 Delete File - Gerald1983 - 博客园 SQLServer Service Can't Start - Gerald1983 DataSource of GridView is Excel - Gerald1983 SQL SERVER 与ACCESS、EXCEL的导入导出(转载) Asp.net页面的生命周期 Asp.net页面的生命周期 DataGrid /GridView分页(sqlserver/oracle)-----转载 sql语句中包括单引号和双引号的问题 XmlDocument操作xml文档 (转) Ajax实例转载 一个Ajax实例(成本项目) Oracle存储过程编写经验和优化措施(转) GridView的美化 数据绑定时的前台页面上的逻辑判断 (转) 如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便) (转) C#代码与javaScript函数的相互调用(转)
SQL - Using CASE in a JOIN
Gerald1983 · 2008-09-22 · via 博客园 - Gerald1983

We have constantly issues with different kinds of customers and based on their status or payment history, you want to join them to the loyalty tables. The focus was to come up with a solution that minimises the extra reads on the other tables but also to add this to a stored proc to minimise modifications to the procedure if it arises

So at the end of the day

 JOIN  dbSecurity.dbo.AccountInstSecurityRole s
  ON s.InstitutionID =  
     CASE
       WHEN (RecordCount) <= 1
         THEN v.ParentInstitutionID
       ELSE v.InstitutionID
     END

Here is the full example

USE dbTechnikons

-- Gets all the child records for the intitution
SELECT
  v.InstitutionID,
  v.Name,
  v.HierarchyLevelID,
  v.HierarchyLevelName,
  v.Disabled,
  v.CompanyID,
  v.ParentInstitutionID
FROM
    vInstitution v
  JOIN
    dbSecurity.dbo.AccountInstSecurityRole s
  ON s.InstitutionID =
     -- If count is 1 or less, then is unrestricted,
     -- otherwise, different join
     CASE
       WHEN (SELECT
               COUNT(*)
             FROM
                 vInstitution v
               JOIN
                 dbSecurity.dbo.AccountInstSecurityRole s
               ON (v.InstitutionID = s.InstitutionID) 
             WHERE
                 v.ParentInstitutionID = @ProviderInstitution
               AND
                 s.AccountID = @LoginID
               AND
                 v.HierarchyLevelID > 1
               AND
                 v.Disabled = 0
            ) <= 1
         THEN v.ParentInstitutionID
       ELSE v.InstitutionID
     END

-- Based this on the Technikon ID that was passed through, LoginID
WHERE
    v.ParentInstitutionID = @ProviderInstitution
  AND
    s.AccountID = @LoginID
  AND
    v.HierarchyLevelID > 1
  AND
    v.Disabled = 0
ORDER BY
   v.Name