sql语句练习
现有两个表:计划表A
product aqty mdate
pa 100 2013-8-1
pb 28 2013-8-2
pc 50 2013-8-1
生产表B
product bqty mdate
pa 50 2013-8-1
pa 30 2013-8-2
pb 28 2013-8-2
pc 50 2013-8-1
pc 9 2013-8-2
要求出 分别汇总aqty,bqty 的值,以以下的列表显示
product aqty bqty
pa 100 80
pb 28 28
pc 50 59
请问如果写语句呀。 --------------------编程问答-------------------- select sum(A.aqty) as aqty,sum(B.bqty) as bqty from A inner join B on A.product=B.product --------------------编程问答-------------------- 谢谢,但请考虑更多的日期的时候的记录。
以上的代码如果记录很多的时候结果不正确。 --------------------编程问答-------------------- select A.product,A.aqty,C.bqty from A inner join ( select product,sum(bqty) bqty from B group by product) as C
on A.product = C.product --------------------编程问答-------------------- 请看看结果,不对呀。
aqty算多了。 --------------------编程问答--------------------
A和B表中的数据一对多 你应该先对B表groupby 在内联 --------------------编程问答-------------------- 如果A表中没有的产品却在B表中出现了或者A表中有但B表中没有,也要统计出来,哪么又自动么办?
计划表A
product aqty mdate
pa 100 2013-8-1
pb 28 2013-8-2
pc 50 2013-8-1
生产表B
product bqty mdate
pa 50 2013-8-1
pa 30 2013-8-2
pb 28 2013-8-2
pc 50 2013-8-1
pd 90 2013-8-2
--------------------编程问答-------------------- 那就用right join
product列取B表的 --------------------编程问答-------------------- 如果我写成存储过程,先对表A进行 行列 对换(日期与数量),最后汇总表B的数量,则如何办呢?
下面的如何修改?谢谢hk08807!!!!
ALTER ProceDure [dbo].[plantest](@SDate DateTime,@EDate DateTime)
As
Begin
Declare @S Varchar(8000)
declare @firstdate datetime
--
set @firstdate=@SDATE
Set @S=''
While @SDate<=@EDate
Begin
Set @S=@S+', max(Case [mdate] When ''' +
Convert(Varchar(12),@SDate,112)+
''' Then aqty Else 0 End) As ['+Convert(Varchar(12),@SDate,112)+ ']'
Set @SDate=DateAdd(dd,1,@SDate)
End
SET @S=@S+', SUM(aqty) as 合计 '
Set @S='Select product, '+Stuff(@S,1,1,'') +
' From A '+
' Where [mdate] between '''+
Convert(Varchar(12),@firstdate,112)+''' and '''+Convert(Varchar(12),@edate,112)+''' group by product'
EXEC(@S)
End --------------------编程问答--------------------
Declare @tableA table(product varchar(10) ,aqty int ,madate datetime)
Declare @tableB table(product varchar(10) ,bqty int ,madate datetime)
insert @tableA
select 'pa',100,'2013-8-1' union all
select 'pb',28 ,'2013-8-2' union all
select 'pc',50 ,'2013-8-1'
insert @tableB
select 'pa', 50, '2013-8-1' union all
select 'pa', 30, '2013-8-2' union all
select 'pb', 28, '2013-8-2' union all
select 'pc', 50, '2013-8-1' union all
select 'pd', 90, '2013-8-2'
--select * from @tableA
--select * from @tableB
select distinct t.product
,Aqty=(select Sum(a.aqty) from @tableA a where a.product=t.product )
,Bqty=(select Sum(b.bqty) from @tableB b where b.product=t.product )
from
(
select product from @tableA
union
select product from @tableB
) t
/*
product Aqty Bqty
pa 100 80
pb 28 28
pc 50 50
pd NULL 90
*/
看看有木有更简单的写法吧 --------------------编程问答-------------------- 例如下图的结果要加入对B表的数量汇总。
--------------------编程问答-------------------- HK08807,很忙吧! --------------------编程问答--------------------
--------------------编程问答-------------------- 果然是个很好的练习啊。 --------------------编程问答-------------------- 第一个问题已经解决了,谢谢各位,但上面第八层的问题还有待各位继续努力。这问题纠得我二天了。谢谢 。
select a.product,a.aqty,sum(b.bqty) bqty from plana a join planb b on a.product=b.product group by a.product,a.aqty order by a.product
补充:.NET技术 , C#