![](/img/trans.png)
[英]Join two tables on multiple columns and return only records from first table that are not in the second
[英]SQL: Join 2 tables and return multiple rows from second table based on key of first table
我有一个表“ Customers”,其中有一个customerid键。
还有另一个表PaymentTotals,其中也有一个customerid列。 该表存储给定星期(周数字段)中客户支付的金额(PaymentAmount)。 这意味着在PaymentTotals表中,任何一个customerid可能有几行,而差是这些行中任何一个的星期数。
我正在尝试在MSSQL中建立一个查询,该查询将两个表连接起来,并将为给定的customerid返回每个不同的weeknumber的PaymentAmount。
我不清楚如何建立此查询。 有什么建议吗? 谢谢。
SELECT *
FROM Customers C INNER JOIN PaymentTotals PT
ON C.customerid = PT.customerid
如果您在给定的一周内有一位客户进行了多次付款,并且想要按周获取总额,则可以执行类似...的操作。
SELECT C.customerid
,PT.WeekNumber
,SUM(PT.Payment_Column) AS TotalPayment
FROM Customers C INNER JOIN PaymentTotals PT
ON C.customerid = PT.customerid
GROUP BY C.customerid, PT.WeekNumber
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.