SQL按数据分组条件筛选
我有一个表数据如下:SQL按数据分组条件筛选,sql,sql-server,tsql,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 条件: 如果级别=2且
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
条件:
如果级别=2且部门=公司
用户部门是公司的
如果级别为3且(部门=教育或部门=体育),则为其他
用户部门是教育部门
如果级别为2且部门=销售和级别为3且部门=医疗保健,则为else
使用部门是医疗保健部门
期望输出
Customer Department
AAA Corporate
BBB Education
CCC Healthcare
实际上,您在这里根本并没有分组,而是根据现有列的条件创建一个新列。您要使用
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
子句。您实际上根本没有在此处分组-您正在根据现有列的条件创建一个新列。您要使用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
子句。简单的案例陈述应该可以做到这一点。。。我把最后一个逻辑子句改为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)
简单的案例陈述就可以了。。。我把最后一个逻辑子句改为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)
在这些条件上构建一些东西:
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
在这些条件上构建一些东西:
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
你可以在需要的时候用一个箱子
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
你可以在需要的时候用一个箱子
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
根据你的逻辑,它是这样的:
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]
根据你的逻辑,它是这样的:
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]
另一个选择是通过枢轴 示例
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
另一个选择是通过枢轴 示例
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
你的问题到底是什么?您是否尝试自己编写查询?查看
SQl案例
语句提示提示您的问题到底是什么?您是否尝试自己编写查询?查看SQl Case
语句提示否则将隐含null
(您不必将其放入,默认情况下它将在那里)。只是FYSA——虽然我确实喜欢看它的可读性。否则就意味着null(你不必把它放进去,它默认会在那里)。只是FYSA——虽然我确实喜欢看它的可读性。谢谢@scsimon,但我在OP中没有看到任何筛选请求。WHERE
子句包含什么?好吧,您的查询将返回类似AAA 1 Parent的内容,而它不在预期结果中。检查我的answer@scsimon你说得对——他没有说,但很明显这正是他需要的。我更新了答案。谢谢你的帮助。我想在这里设置条件,当(Level=2和Dept='Sales')和(Level=3和Dept='HealthCare')然后是“HealthCare”谢谢@scsimon,但我在OP中没有看到任何筛选请求。WHERE
子句包含什么,您的查询将返回类似AAA 1 Parent的内容,但它不在预期结果中。检查我的answer@scsimon你说得对——他没有说,但很明显这正是他需要的。我更新了答案。谢谢你的帮助。我想在这里设置条件,当(Level=2和Dept='Sales')和(Level=3和Dept='HealthCare')时,那么“HealthCare”我可能是错的,但我认为你需要更新你的WHERE
子句。你的将在那里为“SmallBusiness”记录重新注册AAA 3空+1,尽管如此。我想在这里设置条件,当(级别=2和Dept='Sales')和(级别=3和Dept='Healthcare')然后是“Healthcare”时,如何做到这一点?它不能@SrinivasaRao,如何满足该标准?行是1-5级而不是3-2级。我可能错了,但我认为您需要更新WHERE
子句。你的将在那里为“SmallBusiness”记录重新注册AAA 3空+1,尽管如此。我想在这里设置条件,当(级别=2和Dept='Sales')和(级别=3和Dept='Healthcare')然后是“Healthcare”时,如何做到这一点?它不能@SrinivasaRao,如何满足该标准?一排是1-5级,不是3-2级。简直太棒了。第三个条件对我来说很难实现,这不是其他人所说的简单案例陈述。@SrinivasaRao谢谢。这适用于样本数据,但我有点担心未预期的组合。。。但我认为你得到的是非常棒的。第三个条件对我来说很难实现,这不是其他人所说的简单案例陈述。@SrinivasaRao谢谢。这适用于样本数据,但我有点担心未预期的组合。。。但我想你明白了