Sql 添加一列的值
我试图通过基于键值添加一列的值来获取记录。以下是我的疑问:Sql 添加一列的值,sql,sql-server-2008,Sql,Sql Server 2008,我试图通过基于键值添加一列的值来获取记录。以下是我的疑问: SELECT PM_ProductPayment.ProjectId, SalesDetail.SalesPerson, PM_ProductCost.ProductCost, dbo.PM_ProductPayment.ProductPayment, dbo.PM_ProductPayment.PaymentDate FROM dbo.PM_ProductPayment IN
SELECT
PM_ProductPayment.ProjectId, SalesDetail.SalesPerson,
PM_ProductCost.ProductCost, dbo.PM_ProductPayment.ProductPayment,
dbo.PM_ProductPayment.PaymentDate
FROM
dbo.PM_ProductPayment
INNER JOIN
dbo.PM_Product ON dbo.PM_ProductPayment.ProjectId = dbo.PM_Product.ProductId
INNER JOIN
dbo.PM_ProductCost ON dbo.PM_ProductPayment.ProductId = dbo.PM_ProductCost.ProductId
INNER JOIN
dbo.SalesDetail ON dbo.PM_Product.SalesPersonId = dbo.SalesDetail.ID
我得到的结果是:
现在,我想通过为每个产品和最后一个付款日期添加“付款”来获得单行
请优化我的查询或建议其他更好的方法
谢谢,您想要的结果是什么?请不要发布图片,而是我们可以复制/粘贴的代码。您可以使用
{}
-按钮在编辑器中格式化代码。谢谢juergen d。但如果我必须为“付款备注”添加一列,该怎么办?
SELECT pay.ProjectId,
sum(pay.ProductPayment),
max(pay.PaymentDate)
FROM dbo.PM_ProductPayment pay
INNER JOIN dbo.PM_Product pro ON pay.ProjectId = pro.ProductId
INNER JOIN dbo.PM_ProductCost cos ON pay.ProductId =cos.ProductId
INNER JOIN dbo.SalesDetail sal ON pro.SalesPersonId = sal.ID
GROUP BY pay.ProjectId