首页 > 数据库技术 > 详细

SQL

时间:2016-09-09 16:42:56      阅读:265      评论:0      收藏:0      [点我收藏+]

--查询Positions中不存在,ProductAddCountTemp中存在,并且将ProductAddCountTemp中CustomerIdheProductId相同的的行,合并为一行
select CustomerId,CustomerName,ProductId,ProductName,SUM(Count)Count,SUM(Count*Price)Cost from
(
select * from ProductAddCountTemp AS Temp
where (select COUNT(1) as num from Positions where Positions.CustomerId=Temp.CustomerId AND Positions.ProductId=Temp.ProductId) = 0
) as newForm
group by CustomerId,CustomerName,ProductId,ProductName

--查询Posistion中不存在的,但ProductAddCountTemp中存在的
--select * from ProductAddCountTemp AS Temp
--where (select COUNT(1) as num from Positions where Positions.CustomerId=Temp.CustomerId AND Positions.ProductId=Temp.ProductId) = 0

--更新Positions,依据ProductAddCountTemp中的数据,在Positions中存在
--UPDATE Positions SET Count += Temp.TCount, Cost += Temp.Cost
--FROM
--Positions,
--(
-- SELECT CustomerId,ProductId,SUM(Count)TCount,SUM(Count*Price)Cost
-- FROM ProductAddCountTemp
-- GROUP BY CustomerId,ProductId
--) AS Temp
--WHERE Positions.CustomerId=Temp.CustomerId AND Positions.ProductId=Temp.ProductId

SQL

原文:http://www.cnblogs.com/lowy/p/5857081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!