我寫了這段代碼並且工作。它顯示當月同比分組總量等,但我想說明個月,一年按升序排列,但它顯示了他們在隨機,即2016年6月,2016年5月分揀等如何按日期升序排序?
SELECT DATENAME(MONTH, pd.PaymentDate) + ' ' + DATENAME(YEAR, pd.PaymentDate) AS PDate,
SUM(Case when PT.PropertyTypeName = 'Commercial' then PD.PropertyDetailsTotalAmount else 0 END) as CommercialIncome,
SUM(Case when PT.PropertyTypeName = 'Residential' then PD.PropertyDetailsTotalAmount else 0 END) as ResidentialIncome,
COUNT(Case when PT.PropertyTypeName = 'Commercial' then PD.pk_PropertyDetails_PropertyDetailsID else NULL END) as TotalCommercialMaps,
COUNT(Case when PT.PropertyTypeName = 'Residential' then PD.pk_PropertyDetails_PropertyDetailsID else NULL END) as TotalResidentialMaps,
(Count(Case when PT.PropertyTypeName = 'Commercial' then PD.pk_PropertyDetails_PropertyDetailsID else NULL END)
+
COUNT(Case when PT.PropertyTypeName = 'Residential' then PD.pk_PropertyDetails_PropertyDetailsID else NULL END)) TotalMaps,
(SUM(Case when PT.PropertyTypeName = 'Commercial' then PD.PropertyDetailsTotalAmount else 0 END)
+
SUM(Case when PT.PropertyTypeName = 'Residential' then PD.PropertyDetailsTotalAmount else 0 END)) as TotalIncome
From PropertyDetails PD
Inner Join Properties P
ON PD.fk_Properties_ID= p.pk_Properties_ID
Inner Join PropertyTypes PT
ON PT.pk_PropertyTypes_PropertyTypeID= P.fk_PropertyTypes_ID
where
PD.Paid= 1
and PaymentDate >= @DateFrom AND PaymentDate < DATEADD(day,1,@DateTo)
Group By DATENAME(MONTH, pd.PaymentDate) + ' ' + DATENAME(YEAR, pd.PaymentDate)
這樣工作,因爲它不是由它分組? –
錯誤:\t \t \t ORDER BY pd.PaymentDate desc –
「PropertyDetails.PaymentDate」列在ORDER BY子句中無效,因爲它不包含在聚合函數或GROUP BY子句中。 –