日期:2014-05-17 浏览次数:20477 次
DECLARE @t TABLE
(INVNO NVARCHAR(20),Amount DECIMAL(18,2))
INSERT INTO @t
SELECT 'A',300 UNION ALL
SELECT 'B',500
DECLARE @td TABLE
(INVNO NVARCHAR(20),Item NVARCHAR(30),Amount DECIMAL(18,2))
INSERT INTO @td
SELECT 'A','001',50 UNION ALL
SELECT 'A','001',180 UNION ALL
SELECT 'A','002',50 UNION ALL
SELECT 'A','002',100 UNION ALL
SELECT 'B','001',200 UNION ALL
SELECT 'B','001',50 UNION ALL
SELECT 'B','002',100 UNION ALL
SELECT 'B','002',100
'A','001',150
'A','002',150
'B','001',250
'B','002',250
DECLARE @t TABLE
(INVNO NVARCHAR(20),Amount int)
INSERT INTO @t
SELECT 'A',300 UNION ALL
SELECT 'B',500
DECLARE @td TABLE
(INVNO NVARCHAR(20),Item NVARCHAR(30),Amount int)
INSERT INTO @td
SELECT 'A','001',50 UNION ALL
SELECT 'A','001',180 UNION ALL
SELECT 'A','002',50 UNION ALL
SELECT 'A','002',100 UNION ALL
SELECT 'B','001',200 UNION ALL
SELECT 'B','001',50 UNION ALL
SELECT 'B','002',100 UNION ALL
SELECT 'B','002',100
select b.invno,b.item,a.amount/b.num
from @t a ,
(
select invno,item,num = count(1) from @td
group by invno,item
) b
where a.invno = b.invno