[在线等]求一联合查询SQL,可行立即给分,

ron20008 2007-12-06 12:51:34
Unit表(单位表)

UnitID(单位ID) UnitName(单位名称)

---------------------------------

Bill表(帐单表)

BillId(帐单ID) UnitId(单位ID) Date(帐单填写日期)

----------------------------------
BillVsUnit表达(帐单与项目挂勾)

id(流水号) BillId(帐单ID) ItemId(项目ID) money(项目的金额)

-----------------------------------
Item表(项目表)

id(流水号) ItemName(项目名称) FundClass(资金类型:专户/一般) IncomeType(收入类型:类型1,类型2,类型3)



-------------------------------------

生成如下表:
类型1 类型2 类型3

一般 专户 一般 专户 一般 专户
单位名称 单位编码 项目名称 项目编码



说明:1.按日期条件从Bill表里找出UnitID,BillID(还有根据UnitID找到单位名称从UNIT表里)
2.从BillVsUnit表里找出此单位的BillID所对应的项目,金钱.
3.然后根据ItemID去Item表里找到项目名称,项目编码,再看它的FundClass和IncomeType,对应的填到相应位置

一个单位可以对应多个帐单,一个帐单最多对应两个项目.
单位对应项目相同的应该把对应的金额累加.

可以用视图,但尽量工整些.

谢谢,有些复杂,如果满意,另开贴再加分....
...全文
350 52 打赏 收藏 转发到动态 举报
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
楼主看我那例子,是复合表头,我不是抢分的,
ron20008 2007-12-06
  • 打赏
  • 举报
回复
那是复合表头,请楼上的不要乱灌水好不好..我这分不是偷来的.
lishijie910123 2007-12-06
  • 打赏
  • 举报
回复
生成如下表:
类型1 类型2 类型3

一般 专户 一般 专户 一般 专户
单位名称 单位编码 项目名称 项目编码
这是什么意思啊?
这都是一列吗?
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
太长了给一个例子:

要做一张难度较大的报表,请大家帮写组MS SQL语句,情况如下

Info 表(说明:Info 表记录为动态添加Code字段内容不会重复,可能还会添加更多)
Code IncDes
1009 天染公司
1005 海丰公司
1023 采诗公司

MaInfo 表(说明:MaInfo 表为库存表)
Code Barcode MaterialsCn number
1009 1111111 可口可乐 10
1009 1111112 芬达可乐 12
1009 1111113 花生果 15
1005 1111112 芬达可乐 20
1005 1111114 出前一丁 30
1023 1111112 芬达可乐 22
1005 1111113 花生果 40

SaleDetail 表(说明:SaleDetail 表为零售表)
Code Barcode MaterialsCn sellnum1
1009 1111111 可口可乐 2
1005 1111114 出前一丁 3
1023 1111112 芬达可乐 1

SaleOrder 表(说明:SaleOrder 表为销售表)
Code Barcode MaterialsCn sellnum2
1009 1111111 可口可乐 2
1023 1111112 芬达可乐 1

要的结果如下
天染公司 海丰公司 采诗公司 .....(公司是动态的)
总销量 库存 总销量 库存 总销量 库存 销量合计 库存合计
1111111 可口可乐 4 10 0 0 0 0 4 10
1111112 芬达可乐 0 12 0 20 2 22 2 54
1111113 花生果 0 15 0 40 0 0 0 55
1111114 出前一丁 0 0 3 30 0 0 3 30

结果说明:(如在此看结果排列的不整齐,麻烦大家把结果COPY到记事本看)
以MaInfo 库存表的Barcode为唯一依据,重复的商品不列出,
并以MaInfo 库存表的Barcode和Code为唯一关系SaleDetail 表和SaleOrder 表,
Info 表的Code关联其它三张表,由于过程有点复杂,麻烦大伙看清结果帮写组MS SQL语句,3Q

if object_id('pubs..Info') is not null
drop table Info
go
create table Info(Code varchar(10),IncDes varchar(10))
insert into Info(Code,IncDes) values('1009', '天染公司')
insert into Info(Code,IncDes) values('1005', '海丰公司')
insert into Info(Code,IncDes) values('1023', '采诗公司')
go

if object_id('pubs..MaInfo') is not null
drop table MaInfo
go
create table MaInfo(Code varchar(10),Barcode varchar(10),MaterialsCn varchar(10),number int)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1009', '1111111', '可口可乐', 10)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1009', '1111112', '芬达可乐', 12)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1009', '1111113', '花生果 ', 15)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1005', '1111112', '芬达可乐', 20)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1005', '1111114', '出前一丁', 30)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1023', '1111112', '芬达可乐', 22)
insert into MaInfo(Code,Barcode,MaterialsCn,number) values('1005', '1111113', '花生果 ', 40)
go

if object_id('pubs..SaleDetail') is not null
drop table SaleDetail
go
create table SaleDetail(Code varchar(10),Barcode varchar(10),MaterialsCn varchar(10),sellnum1 int)
insert into SaleDetail(Code,Barcode,MaterialsCn,sellnum1) values('1009', '1111111', '可口可乐', 2)
insert into SaleDetail(Code,Barcode,MaterialsCn,sellnum1) values('1005', '1111114', '出前一丁', 3)
insert into SaleDetail(Code,Barcode,MaterialsCn,sellnum1) values('1023', '1111112', '芬达可乐', 1)
go

if object_id('pubs..SaleOrder') is not null
drop table SaleOrder
go
create table SaleOrder(Code varchar(10),Barcode varchar(10),MaterialsCn varchar(10),sellnum2 int)
insert into SaleOrder(Code,Barcode,MaterialsCn,sellnum2) values('1009', '1111111', '可口可乐', 2)
insert into SaleOrder(Code,Barcode,MaterialsCn,sellnum2) values('1023', '1111112', '芬达可乐', 1)
go

select info.incdes,o.* into test from info,
(
select isnull(m.Code,n.code) code , isnull(m.Barcode,n.barcode) barcode, isnull(m.MaterialsCn,n.MaterialsCn) MaterialsCn ,isnull(m.number,0) number ,isnull(n.sellnum,0) sellnum from
(
select Code , Barcode , MaterialsCn , sum(number) number from MaInfo
group by Code , Barcode , MaterialsCn
) m
full join
(
select code , barcode , materialscn , sum(sellnum) sellnum from
(
select SaleDetail.Code,SaleDetail.Barcode,SaleDetail.MaterialsCn,SaleDetail.sellnum1 sellnum from SaleDetail
union all
select SaleOrder.Code,SaleOrder.Barcode,SaleOrder.MaterialsCn,SaleOrder.sellnum2 sellnum from SaleOrder
) t
group by code , barcode , materialscn
) n
on m.code = n.code and m.Barcode = n.Barcode and m.MaterialsCn = n.MaterialsCn
) o
where info.code = o.code

--select * from test

declare @sql varchar(8000)
set @sql = 'select Barcode,MaterialsCn '
select @sql = @sql + ' , sum(case when IncDes = ''' + IncDes + ''' then sellnum else 0 end) [' + IncDes + '总销量' + ']'+
' , sum(case when IncDes = ''' + IncDes + ''' then number else 0 end) [' + IncDes + '库存' + ']'
from (select distinct IncDes from test) as a
set @sql = @sql + ' ,sum(sellnum) 销量合计 , sum(number) 库存合计 from test group by Barcode,MaterialsCn order by barcode,materialscn'
exec(@sql)

drop table Info,MaInfo,SaleDetail,SaleOrder,test


Barcode MaterialsCn 采诗公司总销量 采诗公司库存 海丰公司总销量 海丰公司库存 天染公司总销量 天染公司库存 销量合计 库存合计
------- ----------- -------------- ------------ -------------- ------------ ----------- ----------- ----------- -----------
1111111 可口可乐 0 0 0 0 4 10 4 10
1111112 芬达可乐 2 22 0 20 0 12 2 54
1111113 花生果 0 0 0 40 0 15 0 55
1111114 出前一丁 0 0 3 30 0 0 3 30


zhuwen999 2007-12-06
  • 打赏
  • 举报
回复
非常感谢楼上的两位达人!
dawugui 2007-12-06
  • 打赏
  • 举报
回复
回楼上.
动态sql语句基本语法
1 :普通SQL语句可以用Exec执行

eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N

2:字段名,表名,数据库名之类作为变量时,必须用动态SQL

eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格

当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名

declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确

3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?

declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
多个' 时前一个'就是转义,表示紧跟后的是单引号字符
zhuwen999 2007-12-06
  • 打赏
  • 举报
回复
还是字符串一定要三个?
zhuwen999 2007-12-06
  • 打赏
  • 举报
回复
引号' = 引号
''引号'' = '引号'

那 ''' + newitem + '''=' + newitem + '么?
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
'引号' = 引号
''引号'' = '引号'
zhuwen999 2007-12-06
  • 打赏
  • 举报
回复
' , sum(case FundClass + IncomeType when ''' + newitem + ''' then money else 0 end) [' + newitem + ']'
单引号的对应规则是什么?????
..很简单的问题 请哪位解释以下..
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
奶奶的,龟找抽,我也耗费30.1分钟
dawugui 2007-12-06
  • 打赏
  • 举报
回复
这帖得给我200分.耗费我30分钟.

另外那个两星的,就是叫石头的,给个十分八分就行了.
-狙击手- 2007-12-06
  • 打赏
  • 举报
回复
这帖得给我200分.耗费我30分钟.

---

你太狠了吧,全给你呀
gzlzj 2007-12-06
  • 打赏
  • 举报
回复
good
dawugui 2007-12-06
  • 打赏
  • 举报
回复
这帖得给我200分.耗费我30分钟.
dawugui 2007-12-06
  • 打赏
  • 举报
回复
--两种都写了,搞定,收工.
create table Unit(UnitID int,UnitName varchar(20))
insert into unit values(1 , '中国第一集团')
insert into unit values(2 , '中国第二集团')
create table Bill(BillId int,UnitId int,Date datetime)
insert into Bill values(1 , 1 , '2007-01-01')
insert into Bill values(2 , 1 , '2007-01-02')
insert into Bill values(3 , 2 , '2007-02-01')
insert into Bill values(4 , 2 , '2007-02-02')
create table BillVsUnit(id int,BillId int,ItemId int,money int)
insert into BillVsUnit values(1 , 1 , 1 , 100)
insert into BillVsUnit values(2 , 2 , 2 , 200)
insert into BillVsUnit values(3 , 3 , 3 , 300)
insert into BillVsUnit values(4 , 4 , 4 , 400)
create table Item(id int,ItemName varchar(10),FundClass varchar(10),IncomeType varchar(10))
insert into item values(1 , '一' , '一般' , '类型1')
insert into item values(2 , '二' , '专户' , '类型2')
insert into item values(3 , '三' , '一般' , '类型3')
insert into item values(4 , '四' , '专户' , '类型1')
go

--静态SQL,指d.FundClass , d.IncomeType 值固定.
select a.UnitName 单位名称,a.UnitID 单位编码 ,d.itemname,d.id 项目编码,
sum(case when d.IncomeType = '类型1' and d.FundClass = '一般' then c.money else 0 end) '类型1_一般',
sum(case when d.IncomeType = '类型1' and d.FundClass = '专户' then c.money else 0 end) '类型1_专户',
sum(case when d.IncomeType = '类型2' and d.FundClass = '一般' then c.money else 0 end) '类型1_一般',
sum(case when d.IncomeType = '类型2' and d.FundClass = '专户' then c.money else 0 end) '类型1_专户',
sum(case when d.IncomeType = '类型3' and d.FundClass = '一般' then c.money else 0 end) '类型1_一般',
sum(case when d.IncomeType = '类型3' and d.FundClass = '专户' then c.money else 0 end) '类型1_专户'
from unit a,bill b , billvsunit c,item d
where a.unitid = b.unitid and b.billid = c.billid and c.itemid = d.id
group by a.UnitName,a.UnitID,d.itemname,d.id
order by a.UnitName,a.UnitID,d.itemname,d.id
/*
单位名称 单位编码 itemname 项目编码 类型1_一般 类型1_专户 类型1_一般 类型1_专户 类型1_一般 类型1_专户
-------------------- ----------- ---------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
中国第二集团 2 三 3 0 0 0 0 300 0
中国第二集团 2 四 4 0 400 0 0 0 0
中国第一集团 1 二 2 0 0 0 200 0 0
中国第一集团 1 一 1 100 0 0 0 0 0

(所影响的行数为 4 行)
*/

--动态SQL,指d.FundClass , d.IncomeType 值不固定.
declare @sql varchar(8000)
set @sql = 'select a.UnitName 单位名称,a.UnitID 单位编码 ,d.itemname,d.id 项目编码'
select @sql = @sql + ' , sum(case IncomeType +' + '''_''' + '+ FundClass when ''' + newitem + ''' then money else 0 end) [' + newitem + ']'
from (select distinct IncomeType + '_' + FundClass newitem from item) as t
set @sql = @sql + ' from unit a,bill b , billvsunit c,item d
where a.unitid = b.unitid and b.billid = c.billid and c.itemid = d.id
group by a.UnitName,a.UnitID,d.itemname,d.id
order by a.UnitName,a.UnitID,d.itemname,d.id'
exec(@sql)
/*
单位名称 单位编码 itemname 项目编码 类型1_一般 类型1_专户 类型2_专户 类型3_一般
-------------------- ----------- ---------- ----------- ----------- ----------- ----------- -----------
中国第二集团 2 三 3 0 0 0 300
中国第二集团 2 四 4 0 400 0 0
中国第一集团 1 二 2 0 0 200 0
中国第一集团 1 一 1 100 0 0 0
*/

--drop table unit,bill,billvsunit,item

winjay84 2007-12-06
  • 打赏
  • 举报
回复
谢谢乌龟大哥,明白动态的意思了。
dawugui 2007-12-06
  • 打赏
  • 举报
回复
--上面类型和用户写反了.
create table Unit(UnitID int,UnitName varchar(20))
insert into unit values(1 , '中国第一集团')
insert into unit values(2 , '中国第二集团')
create table Bill(BillId int,UnitId int,Date datetime)
insert into Bill values(1 , 1 , '2007-01-01')
insert into Bill values(2 , 1 , '2007-01-02')
insert into Bill values(3 , 2 , '2007-02-01')
insert into Bill values(4 , 2 , '2007-02-02')
create table BillVsUnit(id int,BillId int,ItemId int,money int)
insert into BillVsUnit values(1 , 1 , 1 , 100)
insert into BillVsUnit values(2 , 2 , 2 , 200)
insert into BillVsUnit values(3 , 3 , 3 , 300)
insert into BillVsUnit values(4 , 4 , 4 , 400)
create table Item(id int,ItemName varchar(10),FundClass varchar(10),IncomeType varchar(10))
insert into item values(1 , '一' , '一般' , '类型1')
insert into item values(2 , '二' , '专户' , '类型2')
insert into item values(3 , '三' , '一般' , '类型3')
insert into item values(4 , '四' , '专户' , '类型1')
go

--静态SQL,指d.FundClass , d.IncomeType 值固定.
select a.UnitName 单位名称,a.UnitID 单位编码 ,d.itemname,d.id 项目编码,
sum(case when d.IncomeType = '类型1' and d.FundClass = '一般' then c.money else 0 end) '类型1_一般',
sum(case when d.IncomeType = '类型1' and d.FundClass = '专户' then c.money else 0 end) '类型1_专户',
sum(case when d.IncomeType = '类型2' and d.FundClass = '一般' then c.money else 0 end) '类型1_一般',
sum(case when d.IncomeType = '类型2' and d.FundClass = '专户' then c.money else 0 end) '类型1_专户',
sum(case when d.IncomeType = '类型3' and d.FundClass = '一般' then c.money else 0 end) '类型1_一般',
sum(case when d.IncomeType = '类型3' and d.FundClass = '专户' then c.money else 0 end) '类型1_专户'
from unit a,bill b , billvsunit c,item d
where a.unitid = b.unitid and b.billid = c.billid and c.itemid = d.id
group by a.UnitName,a.UnitID,d.itemname,d.id
order by a.UnitName,a.UnitID,d.itemname,d.id
/*
单位名称 单位编码 itemname 项目编码 类型1_一般 类型1_专户 类型1_一般 类型1_专户 类型1_一般 类型1_专户
-------------------- ----------- ---------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
中国第二集团 2 三 3 0 0 0 0 300 0
中国第二集团 2 四 4 0 400 0 0 0 0
中国第一集团 1 二 2 0 0 0 200 0 0
中国第一集团 1 一 1 100 0 0 0 0 0

(所影响的行数为 4 行)
*/

--动态SQL,指d.FundClass , d.IncomeType 值不固定.
declare @sql varchar(8000)
set @sql = 'select a.UnitName 单位名称,a.UnitID 单位编码 ,d.itemname,d.id 项目编码'
select @sql = @sql + ' , sum(case IncomeType + FundClass when ''' + newitem + ''' then money else 0 end) [' + newitem + ']'
from (select distinct IncomeType + FundClass newitem from item) as t
set @sql = @sql + ' from unit a,bill b , billvsunit c,item d
where a.unitid = b.unitid and b.billid = c.billid and c.itemid = d.id
group by a.UnitName,a.UnitID,d.itemname,d.id
order by a.UnitName,a.UnitID,d.itemname,d.id'
exec(@sql)
/*
单位名称 单位编码 itemname 项目编码 类型1一般 类型1专户 类型2专户 类型3一般
-------------------- ----------- ---------- ----------- ----------- ----------- ----------- -----------
中国第二集团 2 三 3 0 0 0 300
中国第二集团 2 四 4 0 400 0 0
中国第一集团 1 二 2 0 0 200 0
中国第一集团 1 一 1 100 0 0 0
*/

--drop table unit,bill,billvsunit,item

dawugui 2007-12-06
  • 打赏
  • 举报
回复
create table Unit(UnitID int,UnitName varchar(20))
insert into unit values(1 , '中国第一集团')
insert into unit values(2 , '中国第二集团')
create table Bill(BillId int,UnitId int,Date datetime)
insert into Bill values(1 , 1 , '2007-01-01')
insert into Bill values(2 , 1 , '2007-01-02')
insert into Bill values(3 , 2 , '2007-02-01')
insert into Bill values(4 , 2 , '2007-02-02')
create table BillVsUnit(id int,BillId int,ItemId int,money int)
insert into BillVsUnit values(1 , 1 , 1 , 100)
insert into BillVsUnit values(2 , 2 , 2 , 200)
insert into BillVsUnit values(3 , 3 , 3 , 300)
insert into BillVsUnit values(4 , 4 , 4 , 400)
create table Item(id int,ItemName varchar(10),FundClass varchar(10),IncomeType varchar(10))
insert into item values(1 , '一' , '一般' , '类型1')
insert into item values(2 , '二' , '专户' , '类型2')
insert into item values(3 , '三' , '一般' , '类型3')
insert into item values(4 , '四' , '专户' , '类型1')
go

--静态SQL,指d.FundClass , d.IncomeType 值固定.
select a.UnitName 单位名称,a.UnitID 单位编码 ,d.itemname,d.id 项目编码,
sum(case when d.FundClass = '一般' and d.IncomeType = '类型1' then c.money else 0 end) '一般_类型1',
sum(case when d.FundClass = '一般' and d.IncomeType = '类型2' then c.money else 0 end) '一般_类型2',
sum(case when d.FundClass = '一般' and d.IncomeType = '类型3' then c.money else 0 end) '一般_类型3',
sum(case when d.FundClass = '专户' and d.IncomeType = '类型1' then c.money else 0 end) '专户_类型1',
sum(case when d.FundClass = '专户' and d.IncomeType = '类型2' then c.money else 0 end) '专户_类型2',
sum(case when d.FundClass = '专户' and d.IncomeType = '类型3' then c.money else 0 end) '专户_类型3'
from unit a,bill b , billvsunit c,item d
where a.unitid = b.unitid and b.billid = c.billid and c.itemid = d.id
group by a.UnitName,a.UnitID,d.itemname,d.id
order by a.UnitName,a.UnitID,d.itemname,d.id
/*
单位名称 单位编码 itemname 项目编码 一般_类型1 一般_类型2 一般_类型3 专户_类型1 专户_类型2 专户_类型3
-------------------- ----------- ---------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
中国第二集团 2 三 3 0 0 300 0 0 0
中国第二集团 2 四 4 0 0 0 400 0 0
中国第一集团 1 二 2 0 0 0 0 200 0
中国第一集团 1 一 1 100 0 0 0 0 0

(所影响的行数为 4 行)
*/

--动态SQL,指d.FundClass , d.IncomeType 值不固定.
declare @sql varchar(8000)
set @sql = 'select a.UnitName 单位名称,a.UnitID 单位编码 ,d.itemname,d.id 项目编码'
select @sql = @sql + ' , sum(case FundClass + IncomeType when ''' + newitem + ''' then money else 0 end) [' + newitem + ']'
from (select distinct FundClass + IncomeType newitem from item) as t
set @sql = @sql + ' from unit a,bill b , billvsunit c,item d
where a.unitid = b.unitid and b.billid = c.billid and c.itemid = d.id
group by a.UnitName,a.UnitID,d.itemname,d.id
order by a.UnitName,a.UnitID,d.itemname,d.id'
exec(@sql)
/*
单位名称 单位编码 itemname 项目编码 一般类型1 一般类型3 专户类型1 专户类型2
-------------------- ----------- ---------- ----------- ----------- ----------- ----------- -----------
中国第二集团 2 三 3 0 300 0 0
中国第二集团 2 四 4 0 0 400 0
中国第一集团 1 二 2 0 0 0 200
中国第一集团 1 一 1 100 0 0 0
*/

drop table unit,bill,billvsunit,item

加载更多回复(32)

34,587

社区成员

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

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