主题:[原创]求一sql查询语句:从三个有关联的表中统计数据
bang
[专家分:0] 发布于 2008-04-12 19:23:00
求一sql查询语句:从三个有关联的表中统计数据,表如下:
商品表,出库单表,销售单
商品表的字段如下:入库号,总重,石重,类型
出库单表字段如下:入库号,单号
销售单表字段如下:单号,实付金额,成本,销售日期
他们的关联是:商品表.入库号关联出库单表.入库号,出库单表.单号关联销售单表.单号
问题:如何求得:销售单.销售日期为今天 and 商品表.类型等于1的总记录数,sum(实付金额),sum(成本),
sum(总重),sum(石重)呢?
回复列表 (共1个回复)
沙发
a97191 [专家分:4040] 发布于 2008-04-12 22:54:00
declare @a table(入库号 char(10),总重 int,石重 int,类型 int)
insert @a select '001',103, 245, 1
union all select '002',105, 245, 2
union all select '003',109, 245, 1
union all select '004',108, 166, 3
union all select '005',107, 166, 1
declare @b table(入库号 char(10),单号 char(10))
insert @b select '001','103'
union all select '002','105'
union all select '003','109'
union all select '004','108'
union all select '005','107'
declare @c table(单号 char(10),实付金额 int,成本 int,销售日期 datetime)
insert @c select '103',2223,2220,'2008-4-12'
union all select '105',30,28,'2008-4-11'
union all select '109',309,289,'2008-4-12'
union all select '108',300,280,'2008-4-12'
union all select '107',3033,2822,'2008-4-11'
select count(*),sum(c.实付金额),sum(c.成本),
sum(a.总重),sum(a.石重) from @a a,@b b,@c c where a.入库号=b.入库号 and b.单号=c.单号 and c.销售日期=convert(varchar(10),getdate(),120) and a.类型=1
我来回复