码迷,mamicode.com
首页 > 数据库 > 详细

SQL

时间:2016-09-09 16:44:22      阅读:159      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!