选择以不同顺序随机查看返回数据

时间:2016-02-09 14:33:19

标签: sql sql-server

我从一个视图中得到一个奇怪的结果,当我列出下面的查询时,返回相同数量的记录,但随机顺序不同而没有内部的任何顺序。

 SELECT ROW_NUMBER() OVER(ORDER BY i.[InvoiceDate] ) AS [RowId],
            i.InvoiceSummaryId AS IdKey

       FROM [rpt].[InvoiceView] i
      INNER JOIN PIT.Period p
         ON p.PeriodMonth = i.PeriodMonth 
        AND p.PeriodYear = i.PeriodYear
        and p.TenantId = i.TenantId

       LEFT JOIN [rpt].[InventoryView] s
         ON s.[InvoiceSummaryId] = i.[InvoiceSummaryId]
        and s.TenantId = i.TenantId 

    WHERE i.InvoiceDate BETWEEN '2015-01-01' AND '2015-01-01'
      AND i.TenantId = 1

问题是由视图[rpt]。[InventoryView]引起的,如果我用视图删除左连接,我总是得到相同的结果,但是当我把它放回去时,结果顺序是随机的。我放左连接的原因是因为在视图[rpt]。[InventoryView]上,[InvoiceSummaryId]列可能为空。

这里[rpt]。[InventoryView]查看代码:

ALTER VIEW [rpt].[InventoryView]
AS
    SELECT  si.InvoiceSummaryId,
            si.TenantId,
            si.ServiceInventoryId,
            si.AccountId,
            si.AccountNumber,
            si.ServiceTypeId,
            si.ServiceTypeName AS ServiceType,
            si.PeriodId,
            si.ProviderId,
            si.ProviderName,
            pe.InvoiceDate,
            em.EmployeeId,

            ( CASE WHEN NULLIF(em.EmployeeFirstName, '') IS NULL
                   THEN ISNULL(NULLIF(RTRIM(em.EmployeeLastName), ''), '')
                   ELSE RTRIM(em.EmployeeFirstName) + ISNULL(', ' + NULLIF(RTRIM(em.EmployeeLastName), ''), '')
               END ) AS Employee,

            em.EmployeeStatusId,
            em.ManagerStatusId,
            em.EmployeeNumber,
            em.EmployeeEmail,
            em.ManagerId,
            em.ManagerFirstName,
            em.ManagerLastName,
            (CASE WHEN NULLIF(em.ManagerFirstName, '') IS NULL 
                  THEN ISNULL(NULLIF(RTRIM(em.ManagerLastName), ''), '') 
                  ELSE RTRIM(em.ManagerFirstName) + ISNULL(' ' + NULLIF(RTRIM(em.ManagerLastName), ''), '') 
              END) AS Manager, 

            em.ManagerNumber,
            org.OrganizationId,
            org.LevelId_1,
            org.LevelName_1,
            org.LevelId_2,
            org.LevelName_2,
            org.LevelId_3,
            org.LevelName_3,
            org.LevelId_4,
            org.LevelName_4,
            org.LevelId_5,
            org.LevelName_5,
            org.LevelId_6,
            org.LevelName_6,
            org.LevelId_7,
            org.LevelName_7,
            org.LevelId_8,
            org.LevelName_8,
            org.LevelId_9,
            org.LevelName_9,
            org.LevelId_10,
            org.LevelName_10,
            ha.EsnNumber,
            ha.Make,
            ha.Model,
            ha.Name AS HardwareName,
            ha.SerialNumber,
            ha.WarehouseName,
            co.Name AS [Contract],
            co.Cost AS ContractCost,
            sc.ServiceCategoryId,
            sc.Name AS ServiceCategory
       FROM pit.ServiceInventory AS si

      INNER JOIN pit.Period AS pe
         ON pe.PeriodId = si.PeriodId
        AND pe.TenantId = si.TenantId

       LEFT JOIN cimpl.ServiceType AS st
         ON st.ServiceTypeId = si.ServiceTypeId
        AND st.TenantId = si.TenantId

       LEFT JOIN cimpl.ServiceCategory AS sc
         ON sc.ServiceCategoryId = st.ServiceCategoryId
        AND sc.TenantId = si.TenantId

       LEFT JOIN pit.Employee AS em
         ON em.EmployeeIdKey = si.EmployeeIdKey

       LEFT JOIN pit.Organization AS org
         ON org.OrganizationIdKey = si.OrganizationIdKey

       LEFT JOIN pit.HardwareInventory AS ha
         ON ha.HardwareInventoryIdKey = si.HardwareInventoryIdKey

       LEFT JOIN pit.Contract AS co
         ON co.ContractIdKey = si.ContractIdKey;

服务器级别是否有任何配置使其与众不同?我正在使用SQL Server 2014。

2 个答案:

答案 0 :(得分:0)

如果您不使用视图,则可以随时查看随机订单。不保证基于Pk或其他任何东西的订单。 EVER。

答案 1 :(得分:0)

SQL是基于SET Theory构建的。

根据集合{1,2,3}等于{3,1,2}。因此无法保证您的声明在ORDER中返回。但是扩展语言如MSSQLSERVER中的TSQL,ORACLE中的PL-SQL都有一些实现顺序的机制,即ORDER BY。