SQL语句求助!

1825 2004-10-25 01:13:36
表 a:
________________________
单号 方向 科目
001 借 100002
001 贷 100001
002 借 100002
002 贷 100001
003 借 100003
003 贷 100004
003 贷 100005
________________________
希望结果:
单号 方向 科目
001 借 100002
001 贷 100001
003 借 100003
003 贷 100004
003 贷 100005
________________________
即希望得到表中 不同单据上借贷方科目可能出现的情况。

多谢!!
...全文
276 15 打赏 收藏 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
1825 2004-10-25
  • 打赏
  • 举报
回复
To 邹建:
增加一条:
005就变成 借100002 贷100001 和100002 与001单据情况不同。

结果应改是
单号
001
002
003
004
005

想找出同张单据上借、贷方科目可能出现多少种情况。
即:005没有增加哪一条的话,他的借贷方科目与001是一样的都是借 100002;貸 100001 这种情况只要其中1个单号即可。

而增加一条后005和001单据借贷方科目不同。 这样的话005和001都是要的。
bageer707 2004-10-25
  • 打赏
  • 举报
回复

(select * from a where 科目 not in (select b.科目 from a as b,a as c where (b.科目=c.科目 and b.单号<>c.单号)))
union
(select b.* from a as b,a as c where (b.科目=c.科目 and b.单号<c.单号))

经本人测试已经成功的完成了上述功能
zjcxc 元老 2004-10-25
  • 打赏
  • 举报
回复
哦,那是我理解错了吧,你的意思是要找出借贷都与其他单借贷不同的单据?

create table tb(單號 varchar(10),方向 varchar(5),科目 varchar(10))
Insert tb select '001','借','100002'
union all select '001','貸','100001'
union all select '002','借','100003'
union all select '002','貸','100005'
union all select '003','借','100003'
union all select '003','貸','100003'
union all select '004','借','100003'
union all select '004','貸','100001'
union all select '005','借','100002'
union all select '005','貸','100001'
union all select '005','貸','100002' --如果这样多一条,那按你的要求,结果是怎么样的?
1825 2004-10-25
  • 打赏
  • 举报
回复
?我那个结果对的。

001:借 100002;貸 100001
002:借 100003;貸 100005
003:借 100003;貸 100003
004:借 100003;貸 100001
这4张单据借贷情况不一样

而
005:借 100002;貸 100001 与001是一样的。所以这条不要。

故得到前4张单据号。

ps:多谢邹建老大!!
zjcxc 元老 2004-10-25
  • 打赏
  • 举报
回复
楼主的那个结果不对,如果不管借贷方向,则005的也符合要求. 如果要管借贷方向,则001不满足要求.
zjcxc 元老 2004-10-25
  • 打赏
  • 举报
回复

create table tb(單號 varchar(10),方向 varchar(5),科目 varchar(10))
Insert tb select '001','借','100002'
union all select '001','貸','100001'
union all select '002','借','100003'
union all select '002','貸','100005'
union all select '003','借','100003'
union all select '003','貸','100003'
union all select '004','借','100003'
union all select '004','貸','100001'
union all select '005','借','100002'
union all select '005','貸','100001'
go

--仅查询 單號
select distinct 單號 from tb a
where exists(
select * from tb where 科目=a.科目 and 單號<>a.單號 and 方向<>a.方向)

--查询明细
select a.* from tb a,(
select distinct 單號 from tb a
where exists(
select * from tb where 科目=a.科目 and 單號<>a.單號 and 方向<>a.方向)
)b where a.單號=b.單號
go

--删除测试
drop table tb

/*--测试结果
單號
----------
002
003
004

(所影响的行数为 3 行)


單號 方向 科目
---------- ----- ----------
002 借 100003
002 貸 100005
003 借 100003
003 貸 100003
004 借 100003
004 貸 100001

(所影响的行数为 6 行)
--*/
zjcxc 元老 2004-10-25
  • 打赏
  • 举报
回复
--1.
select distinct 單號 from tb a
where exists(
select * from tb where 科目=a.科目 and 單號<>a.單號 and 方向<>a.方向)


--2.显示明细的

select a.* from tb a,(
select distinct 單號 from tb a
where exists(
select * from tb where 科目=a.科目 and 單號<>a.單號 and 方向<>a.方向)
)b where a.單號=b.單號
1825 2004-10-25
  • 打赏
  • 举报
回复
多谢 hdhai9451 相助!但周晚提问时心中还想着米兰,故问题描述出现了漏洞。
希望找到 表中不同单据(单据号不同)上借贷方科目可能出现的情况。
如:
________________________
单号 方向 科目
001 借 100002
001 貸 100001
002 借 100003
002 貸 100005
003 借 100003
003 貸 100003
004 借 100003
004 貸 100001
005 借 100002
005 貸 100001
________________________
由于前4张单据借贷方情况不同,故希望结果:
单号
001
002
003
004
或:
___________
单号 方向 科目
001 借 100002
001 貸 100001
002 借 100003
002 貸 100005
003 借 100003
003 貸 100003
004 借 100003
004 貸 100001


ps:借用一下好idea :)
create table tb(單號 varchar(10),方向 varchar(5),科目 varchar(10))
Insert into tb
select '001','借','100002'
union all select '001','貸','100001'
union all select '002','借','100003'
union all select '002','貸','100005'
union all select '003','借','100003'
union all select '003','貸','100003'
union all select '004','借','100003'
union all select '004','貸','100001'
union all select '005','借','100002'
union all select '005','貸','100001'
qizhanfeng 2004-10-25
  • 打赏
  • 举报
回复
up
Andy__Huang 2004-10-25
  • 打赏
  • 举报
回复
create table tb(單號 varchar(10),方向 varchar(5),科目 varchar(10))
Insert into tb
select '001','借','100002'
union all select '001','貸','100001'
union all select '002','借','100002'
union all select '002','貸','100001'
union all select '003','借','100003'
union all select '003','貸','100004'
union all select '003','貸','100005'

select * from tb

select a.* from tb a,(select 科目,單號=min(單號) from tb group by 科目)b
where a.科目=b.科目 and a.單號=b.單號

--結果
單號 方向 科目
-------------------------------------
001 貸 100001
001 借 100002
003 借 100003
003 貸 100004
003 貸 100005
Andy__Huang 2004-10-25
  • 打赏
  • 举报
回复
select a.* from tb a,(select 科目,单号=min(单号) from tb group by 科目)b
where a.科目=b.科目 and a.单号=b.单号
xingjilangyu 2004-10-25
  • 打赏
  • 举报
回复
select * from 表a where 单号<>'002'
trumplet 2004-10-25
  • 打赏
  • 举报
回复
select * from 表a where 单号<>'002'
1825 2004-10-25
  • 打赏
  • 举报
回复
帅!酷!强!
为啥俺转不过这个呢? :(
多谢了!!
zjcxc 元老 2004-10-25
  • 打赏
  • 举报
回复

create table tb(單號 varchar(10),方向 varchar(5),科目 varchar(10))
Insert tb select '001','借','100002'
union all select '001','貸','100001'
union all select '002','借','100003'
union all select '002','貸','100005'
union all select '003','借','100003'
union all select '003','貸','100003'
union all select '004','借','100003'
union all select '004','貸','100001'
union all select '005','借','100002'
union all select '005','貸','100001'
--union all select '005','貸','100001'
go

--仅查询 單號
select a.單號
from(
select 單號,cnt=count(*) from tb group by 單號
)a
left join(
select a.單號,cnt=count(b.單號)
from(
select 單號,方向,科目,cnt=count(*)
from tb
group by 單號,方向,科目
)a,(
select 單號,方向,科目,cnt=count(*)
from tb
group by 單號,方向,科目
)b where a.單號>b.單號 and a.方向=b.方向 and a.科目=b.科目 and a.cnt=b.cnt
group by a.單號,b.單號
)b on a.單號=b.單號 and a.cnt=b.cnt
where b.cnt is null

--查询明细
select a.* from tb a,(
select a.單號
from(
select 單號,cnt=count(*) from tb group by 單號
)a
left join(
select a.單號,cnt=count(b.單號)
from(
select 單號,方向,科目,cnt=count(*)
from tb
group by 單號,方向,科目
)a,(
select 單號,方向,科目,cnt=count(*)
from tb
group by 單號,方向,科目
)b where a.單號>b.單號 and a.方向=b.方向 and a.科目=b.科目 and a.cnt=b.cnt
group by a.單號,b.單號
)b on a.單號=b.單號 and a.cnt=b.cnt
where b.cnt is null
)b where a.單號=b.單號
go

--删除测试
drop table tb

/*--测试结果

單號
----------
001
002
003
004

(所影响的行数为 4 行)

單號 方向 科目
---------- ----- ----------
001 借 100002
001 貸 100001
002 借 100003
002 貸 100005
003 借 100003
003 貸 100003
004 借 100003
004 貸 100001

(所影响的行数为 8 行)
--*/
内容概要:本文介绍了COSO企业风险管理框架,阐述了企业风险管理的定义、目标、构成要素及其与内部控制的关系。文章指出,企业风险管理是一个由董事会、管理层及其他人员实施的,应用于战略制定并贯穿于企业之中的过程,旨在识别可能影响主体的潜在事项,管理风险以使其在该主体的风险容量之内,并为主体目标的实现提供合理保证。该框架强调了企业风险管理的八大构成要素:内部环境、目标设定、事项识别、风险评估、风险应对、控制活动、信息与沟通、监控。此外,文章还讨论了企业风险管理的局限性,如人为判断失误、成本与效益的权衡、管理层凌驾等。 适合人群:具备一定管理学基础知识,对风险管理有兴趣的学者、企业管理人员、内部审计师及外部审计师。 使用场景及目标:①帮助组织理解并应用企业风险管理框架,以提高组织的风险管理水平;②指导企业如何通过有效的风险管理来实现其战略、经营、报告和合规目标;③为企业提供一套标准化的风险管理语言,促进内外部沟通与合作。 阅读建议:读者应结合实际案例,深入理解每个构成要素的具体应用,同时关注企业风险管理的局限性,以全面把握企业风险管理的本质。此外,建议定期回顾和更新风险管理策略,以适应不断变化的内外部环境。

34,876

社区成员

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

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