牵涉到两个表,这样一个SQL语句怎样写?

amfer 2003-09-01 01:32:21
SQL Server:
表1:
存货编号,其它字段
001 ,...
002 ,...
...
表2:
存货编号,仓库名,数量
001,仓库1,100
001,仓库2,200
002,仓库1,20
002,仓库2,50

我现在想用SQL语句得到这样的结果:
存货编号,...,仓库1数量,仓库2数量,总数量
001,...,100,200,300
002,...,20,50,70

这条SQL语句怎么写?
如果我这个数据库的结构不合理,那该怎么改?因为仓库名称有可能增加删改,所以不能把仓库1,仓库2固定在表1中.
...全文
27 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
是记录集(select distinct Storage from stodet)的别名
amfer 2003-09-01
  • 打赏
  • 举报
回复
OK,搞定。

不知道from (select distinct Storage from stodet) a這句中最後一個'a'是啥意思?
eastpond 2003-09-01
  • 打赏
  • 举报
回复
up
pengdali 2003-09-01
  • 打赏
  • 举报
回复
select *,(select sum(数量) from 表2 where 存货编号=表1.存货编号 and 仓库名='仓库1') 仓库1数量,(select sum(数量) from 表2 where 存货编号=表1.存货编号 and 仓库名='仓库2') 仓库2数量,(select sum(数量) from 表2 where 存货编号=表1.存货编号) 总数量 from 表1

就可以了。
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select b.stockid'
select @sql=@sql+',sum(case c.Storage when '''+Storage+''' then Qty else 0 end) ['+Storage+'数量]'
from (select distinct Storage from stodet) a
select @sql=@sql+',sum(c.Qty) as 总数量 from Stock b left join Stodet c on b.stockid =c.stockid group by b.stockid '
--print @sql
exec(@sql)
amfer 2003-09-01
  • 打赏
  • 举报
回复
还有一点
1.查询时并不能提供“仓库名“,只能提供"Stockid".
2.表2中的Storage有可能增加,比如增加一行:
stockid Storage Qty
---------------------------
S001 坏料仓 10
amfer 2003-09-01
  • 打赏
  • 举报
回复
可能你们误会我意思了。

我其实就是要把表2中的仓库名称当成表一中的字段,把表2中和表1中相同编号的数量放在相应的仓库中。在举个例子:
表1:Stock

stockid ...
-----------------
S001 ...
S002 ...
...

表2:Stodet

stockid Storage Qty
---------------------------
S001 成品仓 100
S001 次品仓 20
S002 成品仓 200
S002 次品仓 5
...

执行SQL后:

stockid ... 成品仓数量 次品仓数量 总数量
----------------------------------------------------
S001 ... 100 20 120
S002 ... 200 5 205
...
viptiger 2003-09-01
  • 打赏
  • 举报
回复
--step1 CreateFunction
create function SumFunction(@ColValue varchar(10))
returns varchar(1000)
as
begin
declare @strSum varchar(1000)
set @strSum=''
select @strSum = convert(varchar(10),数量)+@strSum from 表2
where 存货编号 = @ColValue
return @strSum
end
--step2 Select
select 存货编号,SumFunction(存货编号),仓库数量 from 表1
txlicenhe 2003-09-01
  • 打赏
  • 举报
回复
同上。
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 存货编号'
select @sql=@sql+',sum(case 仓库名 when '''+仓库名+''' then 数量 else 0 end) ['+仓库名+'数量]'
from (select distinct 仓库名 from wwww) a
select @sql=@sql+',sum(数量) as 总数量 from wwww group by 存货编号'
--print @sql
exec(@sql)
愉快的登山者 2003-09-01
  • 打赏
  • 举报
回复
declare @s varchar(3000)
set @s = 'select 表1.存货编号,表1.其它字段'
select @s = @s + ',sum(case when 表2.仓库名 ='''+ 仓库名 + ''' then 表2.数量 else 0 end) as [' +仓库名+']'
from (select distinct 仓库名 from 表2) A order by 仓库名
set @s = @s + ',sum(表2.数量) 总数量'
set @s = @s + ' from 表1 left join 表2 on 表1.存货编号 = 表2.存货编号 group by 表1.存货编号,表1.其它字段'
exec (@s)



愉快的登山者


◢◣◢◣◢◣
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 存货编号'
select @sql=@sql+',sum(case 仓库名 when '''+仓库名+''' then 数量 else 0 end) ['+仓库名+'数量]'
from (select distinct 仓库名 from 表2) a
select @sql=@sql+' from 表2 group by 存货编号'
--print @sql
exec(@sql)
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 存货编号'
select @sql=@sql+',sum(case 仓库名 when '''+仓库名+''' then 数量 else 0 end) ['+仓库名+'数量]'
from (select distinct 仓库名 from 表2) a
select @sql=@sql+' from 表2 group by 存货编号'
--print @sql
exec(@sql)
愉快的登山者 2003-09-01
  • 打赏
  • 举报
回复
declare @s varchar(3000)
set @s = 'select 表1.存货编号,表1.其它字段'
select @s = @s + ',sum(case when 表2.仓库名 ='''+ 仓库名 + ''' then 表2.数量 else 0 end) as [' +仓库名+']'
from (select distinct 仓库名 from 表2) A order by 仓库名
set @s = @s + ' from 表1 left join 表2 on 表1.存货编号 = 表2.存货编号 group by 表1.存货编号,表1.其它字段'
exec (@s)



愉快的登山者


◢◣◢◣◢◣
sdhdy 2003-09-01
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select 存货编号'
select @sql=@sql+',sum(case 仓库名 when '''+仓库名+''' then 数量 else 0 end) ['+仓库名+'数量]'
from (select distinct 仓库名 from 表2) a
select @sql=@sql+' from 表2 group by 存货编号'
--print @sql
exec(@sql)
pengdali 2003-09-01
  • 打赏
  • 举报
回复
create function getstr(@content varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+rtrim(仓库名) from 表2 where 存货编号=@content
select @str=right(@str,len(@str)-1) where @str<>''
return @str
end
go

--调用:
select *,dbo.getstr(存货编号) 存货编号 from 表1


-------------------------------
你还是要这个?
declare @sql varchar(8000)
set @sql = 'select 存货编号'
select @sql = @sql + ',sum(case 季度 when '''+仓库名+''' then 数量 else 0 end) ['+仓库名+'数量]'
from (select distinct 仓库名 from 有一表) as a
select @sql = @sql+' from 有一表 group by 存货编号'

exec(@sql)
go
jiezhi 2003-09-01
  • 打赏
  • 举报
回复
只有兩個倉庫嗎?
select 存货编号,...,sum(b.数量) as 仓库1数量,
sum(c.数量) as 仓库2数量,sum(d.数量) as 总数量
from 表1 a left join 表2 b on a.存货编号=b.存货编号 and b.仓库名=仓库1
left join 表2 c on a.存货编号=c.存货编号 and c.仓库名=仓库2
left join 表2 d on a.存货编号=d.存货编号

34,590

社区成员

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

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