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
________________________
即希望得到表中 不同单据上借贷方科目可能出现的情况。

多谢!!
...全文
235 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 行)
--*/

34,575

社区成员

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

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