这个SQL怎么写。。不知道怎么描述~

落阳 2011-07-29 01:43:26
已经有数据集结果如下:
FunctionID ModuleID FunctionCode FunctionName HasRole
01aa565b-8ef4-4ad3-9d06-3357a0a5abbf 8 X1-M8-01 浏览 0
01aa565b-8ef4-4ad3-9d06-3357a0a5abbf 8 X1-M8-01 浏览 01aa565b-8ef4-4ad3-9d06-3357a0a5abbf
1b07359d-7199-4e68-b487-8dea02a52b5c 8 X1-M8-04 修改 0
9e672f45-fda9-4bcc-b938-bae2faabfa1c 8 X1-M8-02 新增 0
df6d92d9-b6e5-4ef3-aba7-a78e3bdeae02 8 X1-M8-03 删除 0

现在我要去掉第一行,因为这是同一个权限数据。
列HasRole,0表示没有该功能权限,非0则表示有该功能权限。

期望数据集结果:
FunctionID ModuleID FunctionCode FunctionName HasRole
01aa565b-8ef4-4ad3-9d06-3357a0a5abbf 8 X1-M8-01 浏览 01aa565b-8ef4-4ad3-9d06-3357a0a5abbf
1b07359d-7199-4e68-b487-8dea02a52b5c 8 X1-M8-04 修改 0
9e672f45-fda9-4bcc-b938-bae2faabfa1c 8 X1-M8-02 新增 0
df6d92d9-b6e5-4ef3-aba7-a78e3bdeae02 8 X1-M8-03 删除 0

怎么这中间的变化,怎么通过SQL语句去实现呢?
我自己的思路是这样的:
select *
from Table
where FunctionCode <> (select FunctionCode from Table where FunctionCode有重复)
and HasRole <> '0'

可是怎么在条件里判读FunctionCode有重复呢。。
~~
...全文
48 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
闹铃 2011-07-29
  • 打赏
  • 举报
回复

create table #table
(FunctionID nvarchar(100),
ModuleID int,
FunctionCode nvarchar(50),
FunctionName nvarchar(10),
HasRole nvarchar(100))
insert #table
select '01aa565b-8ef4-4ad3-9d06-3357a0a5abbf', 8,'X1-M8-01','浏览','0' union all
select '01aa565b-8ef4-4ad3-9d06-3357a0a5abbf', 8,'X1-M8-01','浏览','01aa565b-8ef4-4ad3-9d06-3357a0a5abbf' union all
select '1b07359d-7199-4e68-b487-8dea02a52b5c', 8,'X1-M8-04','修改','0' union all
select '9e672f45-fda9-4bcc-b938-bae2faabfa1c', 8,'X1-M8-02','新增','0' union all
select 'df6d92d9-b6e5-4ef3-aba7-a78e3bdeae02', 8,'X1-M8-03','删除','0'

delete A
from #table A
where A.HasRole='0'
and exists(select 1 from #table B
where A.ModuleID=B.ModuleID
and A.FunctionCode=B.FunctionCode
and A.FunctionName=B.FunctionName
and B.HasRole<>'0')

select * from #table

FunctionID ModuleID FunctionCode FunctionName HasRole
---------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------- ------------ ----------------------------------------------------------------------------------------------------
01aa565b-8ef4-4ad3-9d06-3357a0a5abbf 8 X1-M8-01 浏览 01aa565b-8ef4-4ad3-9d06-3357a0a5abbf
1b07359d-7199-4e68-b487-8dea02a52b5c 8 X1-M8-04 修改 0
9e672f45-fda9-4bcc-b938-bae2faabfa1c 8 X1-M8-02 新增 0
df6d92d9-b6e5-4ef3-aba7-a78e3bdeae02 8 X1-M8-03 删除 0

(4 行受影响)
落阳 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liang145 的回复:]
这样?

SQL code

create table #table
(FunctionID nvarchar(100),
ModuleID int,
FunctionCode nvarchar(50),
FunctionName nvarchar(10),
HasRole nvarchar(100))
insert #table
select '01aa565b-8……
[/Quote]

4楼的朋友,Max能这样用?这个。。。会有可能存在的副作用么
落阳 2011-07-29
  • 打赏
  • 举报
回复
谢谢2楼的朋友,解决了。这样的写法没写过。。学习了
liang145 2011-07-29
  • 打赏
  • 举报
回复
这样?

create table #table
(FunctionID nvarchar(100),
ModuleID int,
FunctionCode nvarchar(50),
FunctionName nvarchar(10),
HasRole nvarchar(100))
insert #table
select '01aa565b-8ef4-4ad3-9d06-3357a0a5abbf', 8,'X1-M8-01','浏览','0' union all
select '01aa565b-8ef4-4ad3-9d06-3357a0a5abbf', 8,'X1-M8-01','浏览','01aa565b-8ef4-4ad3-9d06-3357a0a5abbf' union all
select '1b07359d-7199-4e68-b487-8dea02a52b5c', 8,'X1-M8-04','修改','0' union all
select '9e672f45-fda9-4bcc-b938-bae2faabfa1c', 8,'X1-M8-02','新增','0' union all
select 'df6d92d9-b6e5-4ef3-aba7-a78e3bdeae02', 8,'X1-M8-03','删除','0'

select FunctionID,ModuleID,FunctionCode,FunctionName,max(HasRole) as HasRole from #table
group by FunctionID,ModuleID,FunctionCode,FunctionName

--FunctionID ModuleID FunctionCode FunctionName HasRole
--------------------------------------- ----------- -------------- -------------- ------------------------
--01aa565b-8ef4-4ad3-9d06-3357a0a5abbf 8 X1-M8-01 浏览 01aa565b-8ef4-4ad3-9d06-3357a0a5abbf
--1b07359d-7199-4e68-b487-8dea02a52b5c 8 X1-M8-04 修改 0
--9e672f45-fda9-4bcc-b938-bae2faabfa1c 8 X1-M8-02 新增 0
--df6d92d9-b6e5-4ef3-aba7-a78e3bdeae02 8 X1-M8-03 删除 0
落阳 2011-07-29
  • 打赏
  • 举报
回复
同时达到2个条件哇
1,functionCode 不等于 已经重复过的functioncode
2,HasRole 不等于 '0'
光满足2,不满足1,无效的哇。不会影响下面的3条。

这样有没有错?
闹铃 2011-07-29
  • 打赏
  • 举报
回复


delete A
from table_test A
where A.HasRole=0
and exists(select 1 from table_test B
where A.ModuleID=B.ModuleID
and A.FunctionCode=B.FunctionCode
and A.FunctionName=B.FunctionName
and B.HasRole<>0)

AcHerat 元老 2011-07-29
  • 打赏
  • 举报
回复
楼主具体什么意思呢? <> 0 了,下边的三条不是也不出来么?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧