我写了这段代码并且工作。它显示当月同比分组总量等,但我想说明个月,一年按升序排列,但它显示了他们在随机,即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子句中。 –