2017-05-03 378 views
-1
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%。請幫忙。SQL:獲取百分比

Here is a screenshot

+5

你用相同的thing..you'll將明顯得到100%。 –

+0

我沒有足夠的信譽發佈圖片 – csowner17

+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()。這些取所有行的值的總和。

+0

它給了我100%仍然。 – csowner17

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], 
+0

儘管如此,我仍然得到100%。應該是1行,總數的百分比爲 – csowner17

+1

@ csowner17:糟糕,當然,結果集中只有一行,您可能需要一個「GROUP BY」。 – dnoeth

+0

雖然我希望它只有1個單行。 – csowner17