2016-12-15 52 views
0

我有这个观点,我想按顺序获得订单。那么,我怎样才能在近期购买呢?如何在SQL视图中使用Order?

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等封闭在天来样订购...提前

感谢

+0

从视图中选择时使用顺序? – ZLK

+1

是的,当我选择封闭的天时,它将按顺序格式0-10,11-20,21-20等显示....不像0-10,41-50等。 –

回答

0

的观点是不允许到“按...排序”。你可以使用过程而不是视图。

+0

请指导我因为我是新手,所以我必须为此创建程序? –

+1

访问波纹管网站供您参考。 https://www.tutorialspoint.com/plsql/plsql_procedures.htm –

1

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

您可以订购在SELECT语句的观点一样,输出,

select * from XYZ_View order by ClosedInDays 

虽然可以By子句创建顺序的图如下

create view v_tables 
as 

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

go 

预期

它不会工作