Case语句的SQL问题并加入多个表

时间:2017-08-07 15:21:10

标签: sql sql-server select sql-server-2012

这是两个表的示例(请参阅SQLFiddle),我尝试创建一个select语句,其中一个表是查找(Facility),另一个是数据表(WOENTITY),我想选择从中获取facititynames查找并且每当它找到facilityID的多个条目时,只需将文本写为“多个位置”,或者如果它找到null或零,则写入'unknown',否则只需编写facilityname,我试图用单个条目替换多个计数。任何帮助表示赞赏。

链接到SQL小提琴:Click Here

以下是表格外观的截图,表格WOEntity和表格设施 Image showing the table schema and expected end result

我首先考虑从WOEntity表中获取多个位置ID的计数,然后创建一个选择Case语句但没有用,这是草稿代码。

select e.WOID, count(e.ENTITYUID) as CNT, 
    (case   
            when e.ENTITYUID <> '0' and count(e.ENTITYUID) <=1 then
                (Select FACILITYNAME from FACILITY l where e.ENTITYUID = 
                  l.FACILITYID)
            when count(e.ENTITYUID) > 2 then 'MULTIPLE FACILITIES'
            else 'UNKNOWN LOCATIONS'
    end) as facilityName
 From WOENTITY e
group by e.WOID order by WOID;

5 个答案:

答案 0 :(得分:0)

正如声明对你说的那样,你错过了一个由params错过的东西......像是:

select e.WOID, count(e.ENTITYUID) as CNT, 
    (case   
            when e.ENTITYUID <> '0' and count(e.ENTITYUID) <=1 then
                (Select FACILITYNAME from FACILITY l where e.ENTITYUID = 
                  l.FACILITYID) 
            when count(e.ENTITYUID) > 2 then 'MULTIPLE FACILITIES'
            else 'UNKNOWN LOCATIONS'
    end) as facilityName
 From WOENTITY e
group by e.WOID,e.ENTITYUID order by WOID;

答案 1 :(得分:0)

这是对@GarethD答案的修改,其中考虑了类似于&#39; Canada&#39;

的案例
  SELECT  e.WOID,
        CASE WHEN COUNT(f.FACILITYID) = 0 THEN 'UNKNOWN LOCATIONS'
            WHEN COUNT(f.FACILITYID) >0 and  COUNT(coalesce(f.FACILITYID,'x'))>COUNT(f.FACILITYID)  then 'MULTIPLE OFFICES'
            WHEN COUNT(f.FACILITYID) > 1 then 'MULTIPLE OFFICES'
            ELSE MAX(f.FACILITYNAME)
        END AS facilityName
FROM    WOENTITY AS e
        LEFT JOIN FACILITY AS f
            ON f.FACILITYID = e.ENTITYUID
GROUP BY e.WOID
ORDER BY e.WOID;

答案 2 :(得分:0)

请试试这个

Select FACILITYID,
    CASE WHEN (SELECT COUNT(*) 
               FROM  WOEnTITY 
               WHERE WOEnTITY.ENTITYUID = FACILITYID)  > 0 
         THEN 'MULTIPLE LOCATIONS' 
         ELSE '' 
    END
FROM FACILITY

答案 3 :(得分:0)

我个人会使用JOIN而不是相关的子查询来执行此操作,然后在案例表达式中使用COUNT

SELECT  e.WOID,
        CASE WHEN COUNT(f.FACILITYID) = 0 THEN 'UNKNOWN LOCATIONS'
            WHEN COUNT(f.FACILITYID) > 1 THEN 'MULTIPLE FACILITIES'
            ELSE MAX(f.FACILITYNAME)
        END AS facilityName
FROM    WOENTITY AS e
        LEFT JOIN FACILITY AS f
            ON f.FACILITYID = e.ENTITYUID
GROUP BY e.WOID
ORDER BY e.WOID;

<强> SQL Fiddle

由于您处理的是JOIN匹配的多个或零记录的情景,当您到达ELSE时,您只有一条记录,所以尽管您使用的是{{1}聚合函数,你只有一个输入行,所以这并没有真正做任何事情,除了允许你选择设施名称而不用它进行分组。

修改

我刚刚注意到加拿大应该显示为多个位置,即使设施表中只存在其中一个,在这种情况下,您需要稍微更改案例展开,将MAX更改为{{1}对于第二个谓词:

COUNT(f.FACILITYID)

<强> SQL Fiddle

答案 4 :(得分:0)

您可以查询如下:

Select Row_Number() over(order by Id) as Id, Woid, Case when FACILITYID is null then 'UNKNOWN LOCATIONS'
               when RowCnt > 1 then 'MULTIPLE OFFICES' else FACILITYNAME end as FacilityName,
               RowCnt as CountFacilities 
               from (
    Select *, RowCnt = count(EntityUid) over(partition by woid),
    RowN = Row_Number() over(Partition by woid order by Id)
    from WOENTITY we
    left join Facility f
    on we.ENTITYUID = f.FACILITYID
) a
Where a.RowN = 1

输出如下:


+----+---------+-------------------+-----------------+
| Id |  Woid   |   FacilityName    | CountFacilities |
+----+---------+-------------------+-----------------+
|  1 | Canada  | MULTIPLE OFFICES  |               2 |
|  2 | Germany | GREEN             |               1 |
|  3 | India   | GREEN             |               1 |
|  4 | UK      | RED               |               1 |
|  5 | US      | MULTIPLE OFFICES  |               3 |
|  6 | JAPAN   | UNKNOWN LOCATIONS |               1 |
+----+---------+-------------------+-----------------+