商品名称 单价 数量 金额 日期
桃子 12.2 100 1220 2005-09-03
李子 20 50 1000 2005-09-30
比如我上面发的那个例子,如果2条记录的数量加起来刚好为150,我就不在需要其他的记录了
DECLARE @tb table(prod_name CHAR(20),prod_price DECIMAL(5,2),
prod_num INT,prod_totalprice AS prod_num * prod_price,
dt DATETIME)
INSERT INTO @tb SELECT '桃子',12.2,70,'2003-03-12'
UNION ALL SELECT '桃子',12.2,70,'2001-08-02'
UNION ALL SELECT '李子',5.5,80,'2003-03-12'
UNION ALL SELECT '西瓜',6.3,100,'2006-11-21'
UNION ALL SELECT '苹果',3.2,50,'2004-10-15'
UNION ALL SELECT '黄瓜',4.9,70,'2003-12-18'
UNION ALL SELECT '南瓜',3.7,60,'2005-11-10'
UNION ALL SELECT '桔子',4.3,90,'2007-05-17'
UNION ALL SELECT '龙眼',6.6,40,'2002-09-13'
UNION ALL SELECT '杏子',16.4,110,'2001-12-15'
UNION ALL SELECT '木瓜',1.8,120,'2006-12-02'
UNION ALL SELECT '青菜',1.1,30,'2007-06-12'
SELECT DISTINCT t1.prod_name AS '商品名称',t1.prod_price AS '单价',t1.prod_num AS '数量',
t1.prod_totalprice AS '金额',t1.dt AS '日期',t2.prod_name AS '商品名称',
t2.prod_num AS '数量',t1.prod_num+t2.prod_num AS '数量合计'
FROM @tb AS t1,@tb AS t2
WHERE 150=t1.prod_num+t2.prod_num
ORDER BY 1
不知道是不是你想要的结果