老问题,烦。真的。

iky453 2008-01-02 12:42:12
-- 机构表
CREATE TABLE Agencies
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
AName NVARCHAR(200), -- 机构名称
AOrganId Int , -- 对应"AgencyOrgan"表的PKId
ATopNode INT --父节点
)

-- 人员表
CREATE TABLE Householders
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
AgenciesId INT , --关联"Agencies"表的PKId
HName NVARCHAR(100) NOT NULL,
HCard NVARCHAR(50) , --身份证
HPhone NVARCHAR(50) ,
HAddress NVARCHAR(200)

)
-- 财务表
CREATE TABLE Financial
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
FAccount FLOAT , --金额
FDate DATETIME, -- 发放时间
FReceive BIT ,-- 是否领取
FType INT , -- 财务类型,关联"Items"表
HouseholdersId INT NOT NULL -- 关联"Householders"表
)
-- 财务类型表
CREATE TABLE Items
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
IName NVARCHAR(100) -- 类型名称
)

现在要统计如下:
任意给定一个机构的PKId (Agencies表)
统计出它“直属”下级的财务
如:给定北京大学的PKID

直属机构 | 金额 | 财务类型名称
----------------------------------------
计科学院 5000.00 生活费
法学院 6000.00 生活费
。。。。。。。。。。。。。。。。。。。。。。。


...全文
129 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-01-02
  • 打赏
  • 举报
回复
改左联为右联 或内联
iky453 2008-01-02
  • 打赏
  • 举报
回复
还是不对,所以机构出来了,而且很多冗余
WhyAndAnswer 2008-01-02
  • 打赏
  • 举报
回复
给你一个例子
create table #TS
(
kcode varchar(20),
Name varchar(50),
PCode varchar(20)
)

insert #TStore (kcode,Name,PCode)
select kCode,Name,PCode from Table where Name like @Name
while @@rowcount > 0
insert #TS (kCode,Name,PCode)
select kCode,Name,PCode from Table
where PCode in (select KCode from #TS)
and KCode not in (select KCode from #TS)
WhyAndAnswer 2008-01-02
  • 打赏
  • 举报
回复
机构和财务表怎么关联,这是一个递归处理的
lqiiqqqq 2008-01-02
  • 打赏
  • 举报
回复
select a.name as 直屬機構, c.faccount as 金額, d.iname as 財務類型名稱
from
agencies a left outer join householders b
on a.pkid = b.agenciesid left outer join financial c
on b.pkid = c.householdersid left outer join items d
on c.ftype = d.pkid
JL99000 2008-01-02
  • 打赏
  • 举报
回复
这个问题不是那么好做的
iky453 2008-01-02
  • 打赏
  • 举报
回复
AgencyOrgan.PKId

这里没有关系到AgencyORgan表阿。。
iky453 2008-01-02
  • 打赏
  • 举报
回复
兄弟,还是不对阿???
我一直在等啊
lqiiqqqq 2008-01-02
  • 打赏
  • 举报
回复
上面a.name有誤,改正下...
select a.aname as 直屬機構, c.faccount as 金額, d.iname as 財務類型名稱
from
agencies a left outer join householders b
on a.pkid = b.agenciesid left outer join financial c
on b.pkid = c.householdersid left outer join items d
on c.ftype = d.pkid
where a.AOrganId = AgencyOrgan.PKId
lqiiqqqq 2008-01-02
  • 打赏
  • 举报
回复
select a.name as 直屬機構, c.faccount as 金額, d.iname as 財務類型名稱
from
agencies a left outer join householders b
on a.pkid = b.agenciesid left outer join financial c
on b.pkid = c.householdersid left outer join items d
on c.ftype = d.pkid
where a.AOrganId = AgencyOrgan.PKId
Hobart 2008-01-02
  • 打赏
  • 举报
回复
-- 机构表
CREATE TABLE Agencies
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
AName NVARCHAR(200), -- 机构名称
AOrganId Int , -- 对应"AgencyOrgan"表的PKId
ATopNode INT --父节点
)
insert Agencies select '计科学院',1,1
union all select '法学院',2,2

-- 人员表
CREATE TABLE Householders
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
AgenciesId INT , --关联"Agencies"表的PKId
HName NVARCHAR(100) NOT NULL,
HCard NVARCHAR(50) , --身份证
HPhone NVARCHAR(50) ,
HAddress NVARCHAR(200)

)
insert Householders select 1,'aa','123','123','123'
union all select 2,'bb','456','456','456'
-- 财务表
CREATE TABLE Financial
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
FAccount FLOAT , --金额
FDate DATETIME, -- 发放时间
FReceive BIT ,-- 是否领取
FType INT , -- 财务类型,关联"Items"表
HouseholdersId INT NOT NULL -- 关联"Householders"表
)
insert Financial select '5000.00','2008-1-1',1,1,1
union all select '6000.00','2008-1-1',1,2,2
-- 财务类型表
CREATE TABLE Items
(
PKId INT IDENTITY(1,1) PRIMARY KEY,
IName NVARCHAR(100) -- 类型名称
)
insert Items select '生活费'
union all select '生活费'

select AName as 直属机构,FAccount as 金额,IName as 财务类型名称
from Agencies a,Householders b,Financial c,Items d
where a.PKId=b.AgenciesId and b.PKId=c.HouseholdersId and c.FType=d.PKId

sql server正在学习中,可能有不足之处,请楼下指正
iky453 2008-01-02
  • 打赏
  • 举报
回复
兄弟,没对阿?出来的效果不是要求的

34,590

社区成员

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

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