进货表:
名称 代码 数量
艾叶 AA 1
艾叶 AA 2
艾叶 AB 3
白芍 BB 3
白芍 BA 4
苍术 CC 4
当归 DD 5
当归 DD 6退货表:
名称 代码 数量
白芍 BB 1
白芍 BB 4
苍术 CC 3
当归 DD 5
黄芩 HH 6
田七 TQ 2进-退表:
名称 代码 进货数量 退货数量 差量
艾叶 AA 3 0 3
艾叶 AB 3 0 3
白芍 BB 3 5 -2
白芍 BA 4 0 4
苍术 CC 4 3 1
当归 DD 11 5 6
黄芩 HH 0 6 -6
田七 TQ 0 2 -2已知“进货表”和“退货表”,现在要写SQL语句得到“进-退表”,该怎样写?
名称 代码 数量
艾叶 AA 1
艾叶 AA 2
艾叶 AB 3
白芍 BB 3
白芍 BA 4
苍术 CC 4
当归 DD 5
当归 DD 6退货表:
名称 代码 数量
白芍 BB 1
白芍 BB 4
苍术 CC 3
当归 DD 5
黄芩 HH 6
田七 TQ 2进-退表:
名称 代码 进货数量 退货数量 差量
艾叶 AA 3 0 3
艾叶 AB 3 0 3
白芍 BB 3 5 -2
白芍 BA 4 0 4
苍术 CC 4 3 1
当归 DD 11 5 6
黄芩 HH 0 6 -6
田七 TQ 0 2 -2已知“进货表”和“退货表”,现在要写SQL语句得到“进-退表”,该怎样写?
from (select 名称,代码,sum(数量) 数量 from 进货表 group by 名称,代码) aleft join
(select 名称,代码,sum(数量) 数量 from 退货表 group by 名称,代码) bon a.名称=b.名称 and a.代码=b.代码
from (
select 名称,代码,sum(数量) from 进货表 group by 名称,代码
) a left join(
select 名称,代码,sum(数量) from 进货表 group by 名称,代码
) b on a.代码=b.代码
from (
select 名称,代码,sum(数量) from 进货表 group by 名称,代码
) a left join(
select 名称,代码,sum(数量) from 进货表 group by 名称,代码
) b on a.代码=b.代码
isnull(a.[数量],0) as [进货数量],
isnull(b.[数量],0) as [退货数量],
isnull(a.[数量],0) - isnull(b.[数量],0) as [差量]
from (select [名称], [代码], [数量]=sum([数量]) from [进货表] group by [名称], [代码]) as a
left join (select [名称], [代码], [数量]=sum([数量]) from [退货表] group by [名称], [代码]) as b
on a.[名称]=b.[名称] and a.[代码]=b.[代码]
create table jh (名称 varchar(10),代码 varchar(10),数量 int)insert into jh
select '艾叶','AA',1 union all
select '艾叶', 'AA', 2 union all
select '艾叶', 'AB', 3 union all
select '白芍 ','BB' ,3 union all
select '白芍', 'BA', 4 union all
select '苍术', 'CC', 4 union all
select '当归', 'DD', 5 union all
select '当归', 'DD', 6 select *
from th
create table th (名称 varchar(10),代码 varchar(10),数量 int)insert into th
select '白芍' ,'BB' ,1 union all
select '白芍' ,'BB' ,4 union all
select '苍术' ,'CC' ,3 union all
select '当归' ,'DD' ,5 union all
select '黄芩' ,'HH' ,6 union all
select '田七' ,'TQ' ,2
select max(a.名称) as 名称,a.代码,sum(isnull(a.数量,0)) as 进货数量,sum(isnull(b.数量,0)) as 退货数量,sum(isnull(a.数量,0)-isnull(b.数量,0)) as 差量
from jh a left join th b on (a.代码=b.代码)
group by a.代码名称 代码 进货数量 退货数量 差量
---------- ---------- ----------- ----------- -----------
艾叶 AA 3 0 3
艾叶 AB 3 0 3
白芍 BA 4 0 4
白芍 BB 6 5 1
苍术 CC 4 3 1
当归 DD 11 10 1(所影响的行数为 6 行)
create table jh (名称 varchar(10),代码 varchar(10),数量 int)insert into jh
select '艾叶','AA',1 union all
select '艾叶', 'AA', 2 union all
select '艾叶', 'AB', 3 union all
select '白芍 ','BB' ,3 union all
select '白芍', 'BA', 4 union all
select '苍术', 'CC', 4 union all
select '当归', 'DD', 5 union all
select '当归', 'DD', 6 select *
from th
create table th (名称 varchar(10),代码 varchar(10),数量 int)insert into th
select '白芍' ,'BB' ,1 union all
select '白芍' ,'BB' ,4 union all
select '苍术' ,'CC' ,3 union all
select '当归' ,'DD' ,5 union all
select '黄芩' ,'HH' ,6 union all
select '田七' ,'TQ' ,2
select max(a.名称) as 名称,a.代码,sum(isnull(a.数量,0)) as 进货数量,sum(isnull(b.数量,0)) as 退货数量,sum(isnull(a.数量,0)-isnull(b.数量,0)) as 差量
from jh a left join th b on (a.代码=b.代码)
group by a.代码名称 代码 进货数量 退货数量 差量
---------- ---------- ----------- ----------- -----------
艾叶 AA 3 0 3
艾叶 AB 3 0 3
白芍 BA 4 0 4
白芍 BB 6 5 1
苍术 CC 4 3 1
当归 DD 11 10 1(所影响的行数为 6 行)
from
(select 代码,max(名称) as 名称,sum(数量) as 数量
from jh group by 代码)a left join
(select 代码,max(名称) as 名称,sum(数量) as 数量
from th group by 代码) b on (a.代码=b.代码)
group by a.代码/*
名称 代码 进货数量 退货数量 差量
---------- ---------- ----------- ----------- -----------
艾叶 AA 3 0 3
艾叶 AB 3 0 3
白芍 BA 4 0 4
白芍 BB 3 5 -2
苍术 CC 4 3 1
当归 DD 11 5 6(所影响的行数为 6 行)*/
insert into jh
select '艾叶','AA',1 union all
select '艾叶', 'AA', 2 union all
select '艾叶', 'AB', 3 union all
select '白芍 ','BB' ,3 union all
select '白芍', 'BA', 4 union all
select '苍术', 'CC', 4 union all
select '当归', 'DD', 5 union all
select '当归', 'DD', 6
create table th (名称 varchar(10),代码 varchar(10),数量 int)
insert into th
select '白芍' ,'BB' ,1 union all
select '白芍' ,'BB' ,4 union all
select '苍术' ,'CC' ,3 union all
select '当归' ,'DD' ,5 union all
select '黄芩' ,'HH' ,6 union all
select '田七' ,'TQ' ,2
select 名称,代码,MAX(进货数量) as 进货数量,MAX(退货数量)as 退货数量 ,MAX(差量) AS 差量
from (
select 名称,代码,SUM(数量) as 进货数量,0 as 退货数量 ,差量=(SUM(数量))from jh group by 名称,代码
union all
select 名称,代码,0 as 进货数量,SUM(数量) as 退货数量 ,差量=(-SUM(数量))from th group by 名称,代码) t
group by 名称,代码
/*--------------------------
艾叶 AB 3 0 3
白芍 BA 4 0 4
白芍 BB 3 5 3
苍术 CC 4 3 4
当归 DD 11 5 11
黄芩 HH 0 6 -6
田七 TQ 0 2 -2
-----------------*/
insert into jh
select '艾叶','AA',1 union all
select '艾叶', 'AA', 2 union all
select '艾叶', 'AB', 3 union all
select '白芍 ','BB' ,3 union all
select '白芍', 'BA', 4 union all
select '苍术', 'CC', 4 union all
select '当归', 'DD', 5 union all
select '当归', 'DD', 6
create table th (名称 varchar(10),代码 varchar(10),数量 int)
insert into th
select '白芍' ,'BB' ,1 union all
select '白芍' ,'BB' ,4 union all
select '苍术' ,'CC' ,3 union all
select '当归' ,'DD' ,5 union all
select '黄芩' ,'HH' ,6 union all
select '田七' ,'TQ' ,2
select 名称,代码,MAX(进货数量) as 进货数量,MAX(退货数量)as 退货数量 ,sum(差量) AS 差量
from (
select 名称,代码,SUM(数量) as 进货数量,0 as 退货数量 ,差量=(SUM(数量))from jh group by 名称,代码
union all
select 名称,代码,0 as 进货数量,SUM(数量) as 退货数量 ,差量=(-SUM(数量))from th group by 名称,代码) t
group by 名称,代码
/*
艾叶 AA 3 0 3
艾叶 AB 3 0 3
白芍 BA 4 0 4
白芍 BB 3 5 -2
苍术 CC 4 3 1
当归 DD 11 5 6
黄芩 HH 0 6 -6
田七 TQ 0 2 -2*/
isnull(b.[数量],0) as [进货数量],
isnull(c.[数量],0) as [退货数量],
isnull(b.[数量],0) - isnull(c.[数量],0) as [差量]
from (select [名称], [代码] from [进货表] group by [名称], [代码]) as a
left join (select [名称], [代码], [数量]=sum([数量]) from [进货表] group by [名称], [代码]) as b on a.[名称]=b.[名称] and a.[代码]=b.[代码]
left join (select [名称], [代码], [数量]=sum([数量]) from [退货表] group by [名称], [代码]) as c on a.[名称]=c.[名称] and a.[代码]=c.[代码]