从两个不同的表中使用不同的数据

时间:2017-11-20 16:35:15

标签: sql-server

我有这个问题,我有两个不同值的表。

表1:

Description    Qty   Amount
Proc 1          1     100
Proc 2          1      50
Proc 3          1      60

表2:

Description     Payment
  Proc 1          60
  Proc 1          30
  Proc 2          20
  Proc 3          60
  Proc 2          20

所以,结果应该是:

Description     Balance
  Proc 1          10
  Proc 2          40
  Proc 3           0

如何使用选择查询执行此操作?感谢。

我已经尝试过这段代码,但它应该首先获取TABLE1的不同行,并在减去两个表之前对TABLE2中的不同值求和。

SELECT FEESList.[Fee Description], sum(StudentBILLING.Quantity* StudentBILLING.Total- isnull(StudReceipts.Amount,0)) as Balance
FROM StudentBILLING INNER JOIN FEESList ON StudentBILLING.FeeID = FEESList.FeeID INNER JOIN SREGStudentInformation ON StudentBILLING.StudentID = SREGStudentInformation.ID INNER JOIN
      SemesterList ON StudentBILLING.SemesterID = SemesterList.SemID INNER JOIN SchoolYear ON StudentBILLING.SYid = SchoolYear.[SY ID] FULL JOIN StudReceipts ON FEESList.FeeID = StudReceipts.FeeID
WHERE (SREGStudentInformation.[Student ID] = @StudentNumber) AND (SemesterList.[Sem.] = @Sem) AND (SchoolYear.[School Year] = @SchoolYear) AND 
      (FEESList.[Type of Fee] = 2) AND (StudentBILLING.Quantity* StudentBILLING.Total- ISNULL(StudReceipts.Amount,0))>0
GROUP BY FEESList.[Fee Description]

结果来自于此;

Description    Amount   Payments
    Proc 1      100       60
    Proc 1      100       30
    Proc 2      50        20
    Proc 2      50        20
    Proc 3      60        60

这个结果不应该是。

Description    Amount   
    Proc 1      100     
    Proc 2      50   

你能帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

你的数学并不正确 Proc 2 = Tabl1 50,Tab2 20+ 20 = balance = 10

DECLARE @tab1 TABLE ([Description] NVARCHAR(15), Qty INT, Amount INT )
INSERT INTO @tab1
([Description], Qty, Amount)
SELECT 'Proc 1',1,100 UNION ALL
SELECT 'Proc 2',1,50 UNION ALL
SELECT 'Proc 3',1,60

DECLARE @tab2 TABLE ([Description] NVARCHAR(15), Payment INT)
INSERT INTO @tab2
([Description], Payment)
SELECT 'Proc 1', 60 UNION ALL
SELECT 'Proc 1', 30 UNION ALL
SELECT 'Proc 2', 20 UNION ALL
SELECT 'Proc 3', 60 UNION ALL
SELECT 'Proc 2', 20


SELECT
      T.[Description]
    , Balance = (T.Qty * T.Amount) - X.Payment
FROM @tab1 T
INNER JOIN
(
    SELECT
          T.[Description]
        , Payment = SUM(T.Payment) 
    FROM @tab2 T    
    GROUP BY T.[Description]
) X ON X.[Description] = T.[Description]

答案 1 :(得分:0)

从派生表中左加入将解决您的问题。或者,您可以通过在cte中进行聚合然后使用连接

来完成稍长的路径
select t1.description
       ,t1.amount - t2.sum as balance
from table1 t1
left join 
(select description
       ,sum(payment) as sum
from table2
group by description) t2 on t1.description = t2.description

只是一些值得深思的东西:

您想使用left join代替inner joinLeft join如果它在第一个表中而不是第二个表示,则显示为null。 Inner join表示只有在两个表中才显示它。而且我猜你想要显示余额,即使还没有付款(只是一个猜测,但这取决于你的报告)。

以下是您可以使用的rextester示例。

答案 2 :(得分:0)

您可以尝试下面提供的示例数据

首先,它将使用row_number()Description列的Payment功能分区生成行号,该列具有重复付款和&如果付款重复,则sum()

;WITH cte AS (
     SELECT *,
            ROW_NUMBER() OVER(PARTITION BY [Description],
                                           Payment ORDER BY [Description]) rn
     FROM <TABLE 2>)
     SELECT t.[Description],
            CASE
                WHEN COUNT(DISTINCT c.rn) > 1 THEN SUM(c.Payment)
                ELSE MAX(t.amount) - SUM(c.Payment)
            END [Balance]
     FROM cte c
          JOIN <TABLE 1> t ON t.[Description] = c.[Description]
     GROUP BY t.[Description];

结果:

Description     Balance
  Proc 1          10
  Proc 2          40
  Proc 3           0

注意,但您还可以查看您的示例数据,如果一个说明同时付款,哪一个是重复而另一个是唯一的?