-1

PaymentDate による SQL オーダーを取得できないことは理解していますが、結果は Payment Date オーダーではありません。PERIOD 列が実際の日付順になっていることを確認する簡単な方法はありますか?

以下の SQL は、「--order by f.PaymentDate」を追加するだけで完璧に機能します.' だから私はこれを回避する方法を考えようとしています

            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