select
COUNT(pc.property_id) AS [Total Count of Closed],
SUM(pc.sale_price) AS [Total Sum of Sales Price],
SUM(pc.advanced_seller_proceeds) AS [Total Sum of Net Proceeds],
SUM(pc.sale_price) * 100 / SUM(pc.sale_price) AS [Percentage of Total Sales Price],
SUM(pc.advanced_seller_proceeds) * 100 / SUM(pc.advanced_seller_proceeds) AS [Percentage of Total Net Proceeds],
AVG(DATEDIFF(day,pd.list_dt,pc.close_dt)) AS [Average Days from List Date to Close Date]
from resnet_mysql.dbo.property_closings pc
join resnet_mysql.dbo.property_details pd
on pc.property_id = pd.property_id
where pc.res_id = 201
我正在努力获得总销售价格的百分比,但我一直保持100%。请帮忙。
答案 0 :(得分:1)
我认为你想要窗口功能。我还明确地使用非整数 - 以防万一(SQL Server对整数进行整数除法,因此1/2 = 0而不是0.5):
SUM(pc.sale_price) * 100.0 / SUM(SUM(pc.sale_price) OVER () AS [Percentage of Total Sales Price],
SUM(pc.advanced_seller_proceeds) * 100.0 / SUM(SUM(pc.advanced_seller_proceeds)) OVER () AS [Percentage of Total Net Proceeds],
请注意SUM(SUM())
和OVER ()
。它们取所有行的值之和。
答案 1 :(得分:1)
SUM(SUM(pc.sale_price)) OVER ()
使用窗口化聚合对总和进行求和:
SUM(pc.sale_price) * 100 / SUM(SUM(pc.sale_price)) OVER () AS [Percentage of Total Sales Price],
SUM(pc.advanced_seller_proceeds) * 100 / SUM(SUM(pc.advanced_seller_proceeds)) OVER () AS [Percentage of Total Net Proceeds],