-1

我知道我不能通过 PaymentDate 获得 SQL 订单,但我的结果不在 Payment Date Order 中。有没有一种简单的方法可以确保 PERIOD 列按实际日期顺序排列?

下面的 SQL 完美运行,只要我添加“--order by f.PaymentDate”我得到“列“PaymentItem.PaymentDate”在 ORDER BY 子句中无效,因为它不包含在聚合函数或 GROUP BY 子句中。所以我试图思考如何解决这个问题

            select g.SiteDescription,
                case a.Surname when '' then a.Company else a.Surname + ', ' + isnull(a.Forename,'') end as Landowner,
                h.PaymentTypeDesc as [RentalElection],
                d.RelevantProportion,@IN_showRelevantProportion as ShowRelevantProportion,
                g.SiteId,a.LandownerId,e.PaymentTypeId,e.PaymentCategoryId,     
                case @IN_OutputFormat 
                when 'Monthly' then 
                                    convert(char(3), f.PaymentDate, 0) + '-' + ltrim(Year(f.PaymentDate))
                when 'Annually' then
                                    ltrim(Year(f.PaymentDate)) 
                else 
                                    ltrim(Year(f.PaymentDate)) + ' Qtr ' + ltrim(datepart(quarter,f.PaymentDate))
                end as Period,
                sum(isnull(f.ActualPayment,0)) as Total
            from 
                [Site] g, 
                Landowner a,
                [Site] c,
                SiteAgreement d, 
                Payment e, 
                PaymentItem f,
                PaymentType h
                        where a.LandownerId = d.LandownerId 
                and g.SiteId = d.SiteId
                and e.SiteAgreementId = d.SiteAgreementId
                and f.PaymentId = e.PaymentId 
                and e.PaymentTypeId = h.PaymentTypeId
                and f.paymentdate between @IN_daysFrom and @IN_daysTo
                and isnull(f.ActualPayment,0) != 0

            group by g.SiteDescription,
                case a.Surname when '' then a.Company else a.Surname + ', ' + isnull(a.Forename,'') end,
                h.PaymentTypeDesc,
                d.RelevantProportion,
                g.SiteId,a.LandownerId,e.PaymentTypeId,e.PaymentCategoryId,
                case @IN_OutputFormat 
                when 'Monthly' then 
                        convert(char(3), f.PaymentDate, 0) + '-' + ltrim(Year(f.PaymentDate))
                when 'Annually' then
                        ltrim(Year(f.PaymentDate)) 
                else
                        ltrim(Year(f.PaymentDate)) + ' Qtr ' + ltrim(datepart(quarter,f.PaymentDate))
                end
            --order by f.PaymentDate
4

2 回答 2

2

按使用您的付款日期列的整个表达式排序:

ORDER BY case @IN_OutputFormat 
            when 'Monthly' then 
                    convert(char(3), f.PaymentDate, 0) + '-' + ltrim(Year(f.PaymentDate))
            when 'Annually' then
                    ltrim(Year(f.PaymentDate)) 
            else
                    ltrim(Year(f.PaymentDate)) + ' Qtr ' + ltrim(datepart(quarter,f.PaymentDate))
            end
于 2013-05-20T02:38:38.037 回答
0

我只是通过订购格式为 YYYY-MM 的字符串来实现这一点。MM当然需要左填充0:

CONVERT(CHAR(4), f.PaymentDate, 120) + ' ' + right('0'+ rtrim(ltrim(datepart(mm,f.PaymentDate))), 2)

于 2013-05-20T09:53:57.653 回答