SQL Group by Data条件过滤

时间:2017-07-10 19:21:27

标签: sql sql-server tsql

我有一个表格数据如下:

Customer    Level   Dept
AAA 1   Parent
AAA 2   Corporate
AAA 3   SmallBusiness
BBB 1   Parent
BBB 2   StateLevel
BBB 3   Education
BBB 4   RegionLevel
CCC 1   Parent
CCC 2   Sales
CCC 3   Healthcare
CCC 4   Online
CCC 5   Web

条件: 如果Level = 2且Dept = Corporate

  

用户部门是公司

否则,如果Level = 3且(Dept = Education或Dept = Sport)

  

用户部门是教育

否则,如果Level = 2且Dept = Sales&& Level = 3,Dept = HealthCare

  

用户部门是HealthCare

DesiredOutput

Customer Department
AAA  Corporate
BBB  Education
CCC  Healthcare

6 个答案:

答案 0 :(得分:2)

简单的case语句应该这样做...并且我将最后一个逻辑子句更改为OR,因为行不能是级别2和级别3 ...

select distinct
customer,
case 
   when Level=2 and Dept='Corporate' then 'Corporate'
   when Level=3 and (Dept='Education' or Dept='Sport') then 'Education'
   when (Level = 2 and Dept='Sales') or (Level=3 and Dept='Healthcare') then 'Healthcare'
end as 'Department'
from
   SomeTable
where
   Level in (2,3)

答案 1 :(得分:2)

另一种选择是通过PIVOT

示例

Declare @YourTable Table ([Customer] varchar(50),[Level] int,[Dept] varchar(50))
Insert Into @YourTable Values 
 ('AAA',1,'Parent')
,('AAA',2,'Corporate')
,('AAA',3,'SmallBusiness')
,('BBB',1,'Parent')
,('BBB',2,'StateLevel')
,('BBB',3,'Education')
,('BBB',4,'RegionLevel')
,('CCC',1,'Parent')
,('CCC',2,'Sales')
,('CCC',3,'Healthcare')
,('CCC',4,'Online')
,('CCC',5,'Web')

Select Customer
      ,Dept     = concat(
                         case when [2] in ('Corporate') then 'Corporate' end
                        ,case when [3] in ('Education','Sport') then 'Education' end
                        ,case when [2] in ('Sales') and [3]='Healthcare' then 'Healthcare' end
                        )
 From (
        Select *
         From  @YourTable
         Pivot (max([Dept]) For [Level] in ([2],[3]) ) p
      ) A

<强>返回

Customer    Dept
AAA         Corporate
BBB         Education
CCC         Healthcare

答案 2 :(得分:1)

您实际上根本没有分组 - 您正在根据现有列的条件创建新列。您想使用CASE语句:

SELECT CASE WHEN Level = 2 AND Dept = 'Corporate' THEN 'Corporate' 
            WHEN Level = 3 AND (Dept = 'Education' OR Dept = 'Sport') THEN 'Education'
            WHEN (Level = 2 AND Dept = 'Sales') OR (Level = 3 AND Dept = 'HealthCare') THEN 'HealthCare'
            ELSE NULL END AS [Department]
FROM [YourTableName]
WHERE (Level = 2 AND Dept = 'Corporate')
OR    (Level = 3 AND (Dept = 'Education' OR Dept = 'Sport'))
OR    (Level = 2 AND Dept = 'Sales') 
OR (Level = 3 AND Dept = 'HealthCare') 

相应地调整你的逻辑 - 你的问题格式化的方式很难知道括号应该去哪里 - 而且它们很重要。

编辑:根据@scsimon的建议添加了WHERE条款。

答案 3 :(得分:1)

根据这些条款构建内容:

Case
when (level = 2 AND Dept = 'Corporate') Then Corporate
when (level = 3 AND Dept in ('Education','Sport)) Then Education
when (level = 2 AND Dept = 'Sales') Then Healthcare
when (level = 3 AND Dept = 'Healthcare') Then Healthcare
Else Null
End as col

答案 4 :(得分:0)

时可以使用案例
   select customer, 
        case when dept = 'Corporate' and level = 2 then 'Corporate'
             when ( dept =  'Education' or dept ='Sport' and level = 2 then 'Education'
             when  (level = 2 and dept ='Sales') and ( level=3 and dept = 'HealthCare' ) then 'HealthCare'
             else 'Unknow' end my_dept
  from table 

答案 5 :(得分:0)

根据你的逻辑,它就像:

SELECT Customer,
    CASE WHEN Level=2 AND Dept='Corporate' THEN 'Corporate'
         WHEN Level=3 AND (Dept='Education' OR Dept='Sort') THEN 'Education'
         WHEN Level=2 AND (Dept='Sales' AND Level=3) AND Dept='HealthCare'
         END AS [Department] FROM [YourTable]