Select C.仓库编号,A.仓库名称,D.货品编号,B.货品名称,SUM(D.数量) AS 进货总数量,SUM(金额) AS 进货总金额
From
StoreHouse A INNER JOIN InBase C ON A.仓库编号=C.仓库编号
INNER JOIN InList D ON C. 单据号=D. 单据号
INNER JOIN Goods B ON D. 货品编号=B. 货品编号
GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称
From
StoreHouse A INNER JOIN InBase C ON A.仓库编号=C.仓库编号
INNER JOIN InList D ON C. 单据号=D. 单据号
INNER JOIN Goods B ON D. 货品编号=B. 货品编号
GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称
解决方案 »
- 超出了存储过程、函数、触发器或视图的最大嵌套层数(最大层数为 32)。
- 视图与sql语句的设计问题
- SQLSERVER中更新数据库中某个字段长度
- 求救:触发器问题,一天都没有什么进展,希望高手帮忙?
- 求一模糊查询语句!谢谢!
- 再次一个简单的select查询咨询问题 for xml
- 超急的问题,大虾们进来看看!或许你以后也能遇到!
- SQL查询语句问题。
- 数据库一般如何进行优化?
- 请问ORACLE,SYSBASE,INFORMIX,SQLSERVER的存储管理器,查询处理器和事务管理器的实现分别是什么
- 我用SQL SERVER2000建了一个库,我想把这个数据库放到另一台装了SQL SERVER 的机器上有什么好办法吗??
- 在ASP,VB,PB,DELHI中调用存储过程的写法???????????
我要列出所有的记录,假如StoreHouse中有10个仓库,Goods中有1000个货品,那么对应要有10*1000条记录,一个仓库对应1000个货品
From
StoreHouse A INNER JOIN InBase C ON A.仓库编号=C.仓库编号
INNER JOIN InList D ON C. 单据号=D. 单据号
left JOIN Goods B ON D. 货品编号=B. 货品编号
GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称
AS 进货总数量, SUM(金额) AS 进货总金额
FROM StoreHouse A INNER JOIN
InBase C ON A.仓库编号 = C.仓库编号 INNER JOIN
InList D ON C.单据号 = D .单据号 FULL OUTER JOIN
Goods B ON D .货品编号 = B.货品编号
GROUP BY C.仓库编号, A.仓库名称, D .货品编号, B.货品名称
select *,
(select isnull(sum(数量),0)
from (select 仓库编号,
货品编号,
数量
from InBase INNER JOIN
InList
ON InBase.单据号=InList.单据号
) as B
where B.仓库编号=A.仓库编号
and B.货品编号=A.货品编号
) as 进货总数量,
(select isnull(sum(金额),0)
from (select 仓库编号,
货品编号,
金额
from InBase INNER JOIN
InList
ON InBase.单据号=InList.单据号
) as B
where B.仓库编号=A.仓库编号
and B.货品编号=A.货品编号
) as 进货总金额
from (select StoreHouse.仓库编号,
StoreHouse.仓库名称,
Goods.货品编号,
Goods.货品名称
from Goods,StoreHouse
) as A
order by 仓库编号,货品编号
对InList表,考虑同一单不应该对同一种货物记两笔;
同样,同一个仓库也不应该对同一种货物记两笔,所以PK(FInBasId,FGoodsId)
create table StoreHouse
(
FStoreId int not null PRIMARY KEY IDENTITY(0,1),
FName varchar(255) not null UNIQUE
)create table Goods
(
FGoodsId int not null PRIMARY KEY IDENTITY(0,1),
FName varchar(255) not null UNIQUE
)create table InBas
(
FInBasId int not null PRIMARY KEY IDENTITY(0,1),
FStoreId int not null,
CONSTRAINT FStoreId_FK FOREIGN KEY (FStoreId) REFERENCES StoreHouse(FStoreId)
)create table InList
(
FInBasId int not null,
FGoodsId int not null,
FQuantity int not null DEFAULT 0,
FSum money not null DEFAULT 0,
CONSTRAINT FInBasId_FGoodsId_PK PRIMARY KEY (FInBasId,FGoodsId),
CONSTRAINT FInBasId_FK FOREIGN KEY (FInBasId) REFERENCES InBas(FInBasId),
CONSTRAINT FGoodsId_FK FOREIGN KEY (FGoodsId) REFERENCES Goods(FGoodsId)
)--本来一个SQL就可以搞定,但我在SQL7.0下执行下句时会报意外错,只好先写入临时表(两句完成)
select AllGoods.fstoreid,AllGoods.fgoodsid,FQuantity=isnull(i.FQuantity,0),FSum=isnull(i.Fsum,0) from
(select storel.fstoreid,g.fgoodsid from
(select distinct i2.fstoreid
from inlist i1,inbas i2 where i1.finbasid=i2.finbasid) storeL,goods g
) AllGoods
left outer join (select i2.FStoreid,i1.Fgoodsid,i1.FQuantity,i1.FSum from inlist i1,inbas i2 where i1.Finbasid=i2.finbasid) i
on AllGoods.FGoodsid=i.FGoodsid and Allgoods.FStoreid=i.FStoreid--解:
--1.
select storel.fstoreid,g.fgoodsid into #temp from
(select distinct i2.fstoreid
from inlist i1,inbas i2 where i1.finbasid=i2.finbasid) storeL,goods g
--2.
select s.FStoreid,s.FName,g.fgoodsid,g.FName,e.FQuantity,e.FSum from
(select #temp.fstoreid,#temp.fgoodsid,FQuantity=sum(isnull(i.FQuantity,0)),FSum=sum(isnull(i.Fsum,0)) from
#temp
left outer join (select i2.FStoreid,i1.Fgoodsid,i1.FQuantity,i1.FSum from inlist i1,inbas i2 where i1.Finbasid=i2.finbasid) i
on #temp.FGoodsid=i.FGoodsid and #temp.FStoreid=i.FStoreid
group by #temp.fstoreid,#temp.fgoodsid
) e
left outer join Storehouse s on e.fstoreid=s.fstoreid
left outer join Goods g on g.fgoodsid=e.fgoodsid
order by e.FStoreid,e.FGoodsid以上在SQL7.0下通过;执行过程中会作一次笛卡儿乘积,看来表的设计有问题!
同一个仓库不应该对同一种货物记两笔,这一点你判断正确,但是在InList表中,同一单要对同一种货物记多笔,这是那个变态公司要求的,呵呵,说这是做帐上的要求,因为这样可以看见进货的细节流程。
你所写的我通过不了,我再看看!!
如果是这样的话,那么同一单涉及的各种货物是否允许取至不同的仓库呢?也就说:InBas表是否有PK;请最好给出各个表的键值情况。
Select C.仓库编号,A.仓库名称,D.货品编号,B.货品名称,SUM(D.数量) AS 进货总数量,SUM(金额) AS 进货总金额
From
StoreHouse A,InBase C,InList D,Goods B
where A.仓库编号=C.仓库编号
and C. 单据号=D. 单据号
and D. 货品编号=*B. 货品编号
GROUP BY C.仓库编号,A.仓库名称,D.货品编号,B.货品名称