如何在SQL视图中使用Order?

时间:2016-12-15 05:24:29

标签: sql-server

我有这个视图,我希望按顺序接近订单。那么,我如何通过closeindays订购?

Create view XYZ_View as     select      
    ROW_NUMBER() over(order by RecordNoID) as RecordNoNo,
    ROW_NUMBER() over(order by RecordNo) as RecordNum,
    XYZ_View .RecordNoID as RecordNoId,
    XYZ_View .RecordNo as RecordNo,
    XYZ_View .cActiveFlag,
    XYZ_View .nCreatedUserNo,
    XYZ_View .dCreatedOn,
    XYZ_View .nUserNo,
    XYZ_View .dModifyOn,
    XYZ_View .cReplicaFlag,
    XYZ_View .[Status],
    XYZ_View .CheckOutBy,
    XYZ_View .nVersionNo,
    XYZ_View .vVersionRemarks,
    XYZ_View .nDocID,
    XYZ_View .vSendRemarks,
    XYZ_View .nObjectVersionNo,
    XYZ_View .nRTID,
    XYZ_View .LocationName,
    XYZ_View .GroupName,
    XYZ_View .XYZ_Due_Date,
    XYZ_View .XYZ_Closure_Date,
    case
        when XYZ_View .[Status] like '%Lock%'
                then 'Close'
        else
                'Open'
    end  as Final_Status,

        case
        when XYZ_View .[Status] like '%Lock%'
                then
                    case
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)    >= 0
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 10
                                then '0 - 10'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 11
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 20
                                then '11 - 20'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 21
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 30
                                then '21 - 30'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 31
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 40
                                then '31 - 40'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 41
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 50
                                then '41 - 50'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 51
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 60
                                then '51 - 60'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,RXYZ_View .dModifyOn)   >= 61
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 70
                                then '61 - 70'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 71
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 80
                                then '71 - 80'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 81
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 90
                                then '81 - 90'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)   >= 91
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 100
                                then '91 - 100'
                        when DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn)  >= 101
                            and DATEDIFF(D,XYZ_View .dCreatedOn,XYZ_View .dModifyOn) <= 200
                                then '>= 101'
                    end
                else
                    '-1' end as ClosedInDays,
            case    when XYZ_View .[Status] like '%Lock%' and convert(date,dModifyOn,105)> convert(date,XYZ_Due_Date,105))
                then
                    'Yes'   end as ClosedAfterDueDate   from XYZ_View 

我需要在关闭的日子里订购,如0-10,11-20,21-30等序列......

提前致谢

2 个答案:

答案 0 :(得分:1)

默认情况下,视图和表格不会按顺序存储。

您可以在SELECT语句中对视图输出进行排序,例如

select * from XYZ_View order by ClosedInDays

虽然您可以使用Order By子句创建一个视图,如下所示

create view v_tables
as

select top 100 percent * from sys.tables order by name

go

它不会按预期工作

答案 1 :(得分:0)

查看不允许“按顺序”。你可以使用程序而不是查看。