我的SQL知识非常基础,所以这可能是一个非常简单的解决方案,当我运行查询时,其中一列中有重复的服务/项目保证金',如何删除重复项(请记住,我希望仍然显示其他栏目中的数字'产品保证金(如果可能的话,还可以加在一起)
这里是SQL: - 细节
SELECT DISTINCT
[dbo].[v_api_find_Invoice].[Company_Name] AS 'Company'
,[dbo].[v_api_find_Invoice].[Invoice_Number] AS 'id'
,SUM(
([dbo].[v_rpt_Product].[Quantity] * [dbo].[v_rpt_Product].[unit_price]) -
([dbo].[v_rpt_Product].[Quantity] * [dbo].[v_rpt_Product].[unit_cost])
) AS 'Product Margin'
,SUM([dbo].[v_rpt_Invoices].[Time_Amount] -
[dbo].[v_rpt_Invoices].[Progress_Amount_Applied]
) AS 'Service/Project Margin'
,[dbo].[v_api_find_Invoice].[DateInvoice] AS 'Date Invoice'
FROM [dbo].[v_api_find_Invoice] WITH(NOLOCK)
LEFT OUTER JOIN [dbo].[v_rpt_Product] WITH(NOLOCK)
ON [dbo].[v_rpt_Product].[billing_log_recid] =
[dbo].[v_api_find_Invoice].[Billing_Log_RecID]
LEFT OUTER JOIN [dbo].[v_rpt_Invoices] WITH(NOLOCK)
ON [dbo].[v_rpt_Invoices].[Billing_Log_RecID] =
[dbo].[v_api_find_Invoice].[Billing_Log_RecID]
GROUP BY [dbo].[v_api_find_Invoice].[Invoice_Number]
,[dbo].[v_api_find_Invoice].[DateInvoice]
,[dbo].[v_api_find_Invoice].[Company_Name]
,[dbo].[v_rpt_Invoices].[Time_Amount] -
[dbo].[v_rpt_Invoices].[Progress_Amount_Applied]
编辑:这里有一些示例结果,您可以看到重复项更好(重复6000.00的列是服务/项目保证金:
80613公司X 11.65 6000.00 2014-12-30
80613公司X 36.80 6000.00 2014-12-30
80613公司X 48.55 6000.00 2014-12-30
答案 0 :(得分:0)
从GROUP BY
dbo.v_rpt_Invoices.Time_Amount - dbo.v_rpt_Invoices.Progress_Amount_Applied
我希望你能让我用别名来清理那里的很多噪音:
SELECT
fi.Company_Name AS Company
, fi.Invoice_Number AS id
, SUM(rp.Quantity * (rp.unit_price - rp.unit_cost)) AS "Product Margin"
, SUM(ri.Time_Amount - ri.Progress_Amount_Applied) AS "Service/Project Margin"
, fi.DateInvoice AS "Date Invoice"
FROM
dbo.v_api_find_Invoice WITH (NOLOCK) as fi
LEFT OUTER JOIN dbo.v_rpt_Product WITH (NOLOCK) as rp
ON rp.billing_log_recid = fi.Billing_Log_RecID
LEFT OUTER JOIN dbo.v_rpt_Invoices WITH (NOLOCK) as ri
ON ri.Billing_Log_RecID = fi.Billing_Log_RecID
GROUP BY
fi.Company_Name
, fi.Invoice_Number
, fi.DateInvoice
答案 1 :(得分:-1)
将“服务/项目保证金”列添加到GROUP BY子句中。