SQL Server 2008查询组总和

时间:2018-01-15 12:37:33

标签: sql sql-server sql-server-2008

我正在尝试创建一个SQL查询,该查询将显示在vb.net中的Windows应用程序上,然后解压缩到excel文件中。我在SQL Server 2008中有两个表。下面是我的两个表的图像。

表1:enter image description here

和表2:enter image description here

我想要实现的目标是:

enter image description here

但是在1个查询中执行此操作并包含Table2.Date。

我现在的2个问题是:



SELECT Table1.Id AS Z_Number,SUM(Table2.Amount) AS Cash_Payments, COUNT(Table2.TicketId)AS No_of_Tickets
FROM Table2,Table1
where Table2.Name='Cash' and Table2.Date between  Table1.StartDate and Table1.EndDate and Table1.Id=1
GROUP BY Table1.Id
SELECT Table1.Id AS Z_Number,SUM(Table2.Amount) AS Credit_Card_Payments, COUNT(Table2.TicketId)AS No_of_Tickets
FROM Table2,Table1
where Table2.Name='Credit Card' and Table2.Date between  Table1.StartDate and Table1.EndDate and Table1.Id=1
GROUP BY Table1.Id




关于我可以用什么确切的查询函数来实现它的任何建议?

要求的结果: enter image description here

1 个答案:

答案 0 :(得分:0)

enter image description here

这给了我正确的结果。谢谢你的帮助。