表1 cid bid 都是自增
cid name
1 A公司
2 b公司
3 c公司
... ....
表2 bid cid name
1 1 小王
2 2 小明
3 3 小张
4 2 小王
... ... ....
表三
时间 bid 应付金额 以付金额 cid
2008-08-08 1 1000 800 1
2008-09-08 1 0 200 1
2008-10-08 3 1200 600 3
2008-10-08 3 1000 0 3
2008-10-08 3 0 600 3
2008-11-08 4 800 600 2
==========================================================得到下面这个表
name A公司 B公司 C公司 ... 未付金额
小王 0.00 200 0.00 ... 200
小张 0.00 200 600 ... 800
cid name
1 A公司
2 b公司
3 c公司
... ....
表2 bid cid name
1 1 小王
2 2 小明
3 3 小张
4 2 小王
... ... ....
表三
时间 bid 应付金额 以付金额 cid
2008-08-08 1 1000 800 1
2008-09-08 1 0 200 1
2008-10-08 3 1200 600 3
2008-10-08 3 1000 0 3
2008-10-08 3 0 600 3
2008-11-08 4 800 600 2
==========================================================得到下面这个表
name A公司 B公司 C公司 ... 未付金额
小王 0.00 200 0.00 ... 200
小张 0.00 200 600 ... 800
解决方案 »
- 求方法从excel导入数据到页面上
- 交叉表问题,不知如何下手好~`请各位指教
- 请问怎么实现这样的查询结果呢?
- 大哥们小弟这个存储过程怎么编译不过,帮忙看一下啊在线 等
- 求高手指点,解决方案和数据库语句
- 这种计算每一类的总记录数的报表怎么做?
- 关于SQL数据库数据更新的问题!!!!
- 用java(JDBC)写代码在sql server里读写图片的代码怎么做?
- 如何将SQL SERVER 数据库转成Oracle8i数据库
- 关于@@ERROR判断
- 求助:较难的行列转换问题,只能使用Pivot或者case
- sql server 2005完成发布和订阅后,web系统运行时提示“要复制的LOB 数据的长度(88342)超出了配置的最大值65536。语句已终止。”,这个怎么解决
if object_id('[表1]') is not null drop table [表1]
create table [表1] (cid int,name varchar(5))
insert into [表1]
select 1,'A公司' union all
select 2,'b公司' union all
select 3,'c公司'
--> 测试数据: [表2]
if object_id('[表2]') is not null drop table [表2]
create table [表2] (bid int,cid int,name varchar(4))
insert into [表2]
select 1,1,'小王' union all
select 2,2,'小明' union all
select 3,3,'小张' union all
select 4,2,'小王'
--> 测试数据: [表三]
if object_id('[表三]') is not null drop table [表三]
create table [表三] (时间 datetime,bid int,应付金额 int,以付金额 int,cid int)
insert into [表三]
select '2008-08-08',1,1000,800,1 union all
select '2008-09-08',1,0,200,1 union all
select '2008-10-08',3,1200,600,3 union all
select '2008-10-08',3,1000,0,3 union all
select '2008-10-08',3,0,600,3 union all
select '2008-11-08',4,800,600,2
declare @sql varchar(8000)
set @sql='select a.name'
select @sql=@sql+',['+name+']=sum(case a.cid when '''+ltrim(cid)+''' then c.应付金额-c.以付金额 else 0 end)'
from 表1
set @sql=@sql+',未付金额=sum(c.应付金额-c.以付金额) from 表2 a,表三 c where a.bid=c.bid and a.cid=c.cid group by a.name'
exec(@sql)
INSERT TA
SELECT 1, N'A公司' UNION ALL
SELECT 2, N'b公司' UNION ALL
SELECT 3, N'c公司'CREATE TABLE TB([bid] INT, [cid] INT, [name] NVARCHAR(2))
INSERT TB
SELECT 1, 1, N'小王' UNION ALL
SELECT 2, 2, N'小明' UNION ALL
SELECT 3, 3, N'小张' UNION ALL
SELECT 4, 2, N'小王'CREATE TABLE TC([时间] DATETIME, [bid] INT, [应付金额] INT, [以付金额] INT, [cid] INT)
INSERT TC
SELECT '2008-08-08', 1, 1000, 800, 1 UNION ALL
SELECT '2008-09-08', 1, 0, 200, 1 UNION ALL
SELECT '2008-10-08', 3, 1200, 600, 3 UNION ALL
SELECT '2008-10-08', 3, 1000, 0, 3 UNION ALL
SELECT '2008-10-08', 3, 0, 600, 3 UNION ALL
SELECT '2008-11-08', 4, 800, 600, 2
DECLARE @STR NVARCHAR(4000)
SET @STR=N''
SELECT @STR=@STR+N','+N'SUM(CASE WHEN A.[name]=N'''+[name]+N''' THEN [应付金额]-[以付金额] ELSE 0 END) AS '+[name]
FROM TA
GROUP BY [name]EXEC(N'SELECT B.name'+@STR+N',总金额 =SUM([应付金额]-[以付金额])
FROM TC AS C JOIN TB AS B ON C.bid=B.bid
JOIN TA AS A ON C.[cid]=A.[cid]
GROUP BY B.name')DROP TABLE TA,TB,TC
/*
name A公司 b公司 c公司 总金额
---- ----------- ----------- ----------- -----------
小张 0 0 1000 1000
小王 0 200 0 200
*/