通过在具有多个子查询的SQL Server 2008中被忽略的顺序

时间:2014-11-21 18:34:24

标签: sql-server sql-server-2008 sql-order-by

我有一份报告会返回患者的访问列表,但是访问顺序是完全混乱的。访问列表是在子查询中获得的,如下所示:

INNER JOIN (SELECT subject_id,
                               A.id [VisitID],
                               A.visit_schedule_id [VisitScheduleID],
                               B.client_visit_description [Visit],
                               visit_date_local [Visit Date],
                               call_date [Call Date]
                FROM tbl_visit A
                INNER JOIN tbl_visit_schedule B ON B.id = A.visit_schedule_id) F

有一个内部查询使用这个“F”块来获取相关值以显示给最终用户,如下所示:

SELECT TOP 9999999999
           A.[name] [Country Name], 
           B.[site_number] [Site Number], 
           D.[investigator_name] [Investigator Name], 
           C.[spid] [Patient Number], 
           E.[YOB] [Year of Birth], 
           F.[Visit] [Visit], 
           CONVERT(NVARCHAR, F.[Visit Date], 120) [Visit Date],
           ISNULL(pl.[Pack(s) Assigned], 'N/A') [Kit Assigned],
           H.[Confirmation (doc)]
    FROM   (

最后有一个外部选择显示给用户:

SELECT DISTINCT
   [Country Name], 
   [Investigator Name], 
   [Patient Number], 
   [Year of Birth], 
   --[Visit], 
   [Visit Date],
   [Kit Assigned],
   [Confirmation (doc)]
FROM   (

我有一个内部select语句的ORDER BY,如下所示:

ORDER BY CAST(F.[Visit Date] AS DATE) asc, F.[Call Date] asc

问题在于,当我显示所有内容时(“访问”未被注释掉),SQL会以无序方式返回结果,即使最后的顺序也是如此。如果我评论访问,但结果开始有序。

我不确定这个特定专栏对导致失败的报告的其余部分有何意义,其他人是否有任何关于此事的线索?

更新:这是整个查询:

SELECT DISTINCT site_id, site_number, 
       [Country Name], 
       [Investigator Name], 
       [Patient Number], 
       [Year of Birth], 
       [Visit], 
       [Visit Date],
       [Kit Assigned],
       [Confirmation (doc)]
FROM   (SELECT TOP 99999999999
        B.[id] [site_id], 
               B.[site_number] [site_number], 
               A.[name] [Country Name], 
               B.[site_number] [Site Number], 
               B.[site_number] [SiteNumber], 
               D.[investigator_name] [Investigator Name], 
               C.[spid] [Patient Number], 
               E.[YOB] [Year of Birth], 
               F.[Visit] [Visit], 
               CONVERT(NVARCHAR, F.[Visit Date], 120) [Visit Date],
               ISNULL(pl.[Pack(s) Assigned], 'N/A') [Kit Assigned],
               H.[Confirmation (doc)]
        FROM   (
                SELECT [id], 
                       [name], 
                       [code] [country_code] 
                FROM   [dbo].[tbl_country]) A 
               INNER JOIN [dbo].[tbl_site] B 
                       ON A.[id] = B.[country_id] 
               INNER JOIN [dbo].[tbl_subject] C 
                       ON B.[id] = C.[site_id] 
               LEFT JOIN (SELECT A.id, 
                                 A.inv_first_name + ' ' + A.inv_last_name 
                                 [investigator_name] 
                          FROM   vw_site A) D 
                      ON B.[id] = D.[id] 
               INNER JOIN (SELECT A.[subject_id], 
                                  B.int_value [YOB] 
                           FROM   vw_subject_info A 
                                  INNER JOIN tbl_custom_data B 
                                          ON B.row_id = A.subject_id 
                                             AND B.table_name = 'Subject' 
                                             AND B.[field_name] = 'Initial_DOB') 
                          E 
                       ON C.[id] = E.[subject_id] 
               INNER JOIN (
                            SELECT subject_id,
                                   VisitID,
                                   VisitScheduleID,
                                   site_id,
                                   site_number,
                                   visit [Visit],
                                   visit_date_local [Visit Date],
                                   call_date [Call Date]
                            FROM vw_visit
                           ) F 
                       ON C.[id] = F.[subject_id] 
              LEFT JOIN (SELECT [visit_id],
                          [int_value] [Confirmation (doc)]
                          FROM  [vw_visitDetail]
                          WHERE  [field_name] = 'docId'
                          ) H
                      ON F.[VisitID] = H.[visit_id]
              --Selecting the packs dispensed at latest dispensing visit              
               LEFT JOIN (SELECT A.[id] [visit_id],
               STUFF((SELECT ', ' + CAST(C.[client_pack_number] AS NVARCHAR(MAX))
                      FROM   [dbo].[tbl_visit_pack] B
                      INNER JOIN [dbo].[tbl_pack] C
                      ON B.[pack_id] = C.[id]
                      INNER JOIN dbo.tbl_pack_type pt
                      ON pt.id = c.pack_type_id
                      WHERE  A.[id] = B.[visit_id]    
                      FOR XML PATH('')), 1, 2, '') AS [Pack(s) Assigned]
               FROM   [dbo].[tbl_visit] A
               GROUP  BY A.[id]) pl
               ON pl.visit_id = F.visitid
               --Ordering
               ORDER BY CAST(F.[Visit Date] AS DATE) asc, F.[Call Date] asc
) rawquery 
--where

1 个答案:

答案 0 :(得分:1)

要按特定顺序获取输出,您需要在“rawquery”别名之后添加订单。由于它现在位于您的查询没有顺序,因此您无法知道结果的顺序。

SELECT DISTINCT site_id
                , site_number
                , [Country Name]
                , [Investigator Name]
                , [Patient Number]
                , [Year of Birth]
                , [Visit]
                , [Visit Date]
                , [Kit Assigned]
                , [Confirmation (doc)]
FROM   (SELECT TOP 99999999999 B.[id]                                   [site_id]
                               , B.[site_number]                        [site_number]
                               , A.[name]                               [Country Name]
                               , B.[site_number]                        [Site Number]
                               , B.[site_number]                        [SiteNumber]
                               , D.[investigator_name]                  [Investigator Name]
                               , C.[spid]                               [Patient Number]
                               , E.[YOB]                                [Year of Birth]
                               , F.[Visit]                              [Visit]
                               , CONVERT(NVARCHAR, F.[Visit Date], 120) [Visit Date]
                               , Isnull(pl.[Pack(s) Assigned], 'N/A')   [Kit Assigned]
                               , H.[Confirmation (doc)]
        FROM   (SELECT [id]
                       , [name]
                       , [code] [country_code]
                FROM   [dbo].[tbl_country]) A
               INNER JOIN [dbo].[tbl_site] B
                       ON A.[id] = B.[country_id]
               INNER JOIN [dbo].[tbl_subject] C
                       ON B.[id] = C.[site_id]
               LEFT JOIN (SELECT A.id
                                 , A.inv_first_name + ' ' + A.inv_last_name [investigator_name]
                          FROM   vw_site A) D
                      ON B.[id] = D.[id]
               INNER JOIN (SELECT A.[subject_id]
                                  , B.int_value [YOB]
                           FROM   vw_subject_info A
                                  INNER JOIN tbl_custom_data B
                                          ON B.row_id = A.subject_id
                                             AND B.table_name = 'Subject'
                                             AND B.[field_name] = 'Initial_DOB') E
                       ON C.[id] = E.[subject_id]
               INNER JOIN (SELECT subject_id
                                  , VisitID
                                  , VisitScheduleID
                                  , site_id
                                  , site_number
                                  , visit            [Visit]
                                  , visit_date_local [Visit Date]
                                  , call_date        [Call Date]
                           FROM   vw_visit) F
                       ON C.[id] = F.[subject_id]
               LEFT JOIN (SELECT [visit_id]
                                 , [int_value] [Confirmation (doc)]
                          FROM   [vw_visitDetail]
                          WHERE  [field_name] = 'docId') H
                      ON F.[VisitID] = H.[visit_id]
               --Selecting the packs dispensed at latest dispensing visit
               LEFT JOIN (SELECT A.[id]                                [visit_id]
                                 , Stuff((SELECT ', '
                                                 + Cast(C.[client_pack_number] AS NVARCHAR(MAX))
                                          FROM   [dbo].[tbl_visit_pack] B
                                                 INNER JOIN [dbo].[tbl_pack] C
                                                         ON B.[pack_id] = C.[id]
                                                 INNER JOIN dbo.tbl_pack_type pt
                                                         ON pt.id = c.pack_type_id
                                          WHERE  A.[id] = B.[visit_id]
                                          FOR XML PATH('')), 1, 2, '') AS [Pack(s) Assigned]
                          FROM   [dbo].[tbl_visit] A
                          GROUP  BY A.[id]) pl
                      ON pl.visit_id = F.visitid --Ordering 
        ORDER  BY Cast(F.[Visit Date] AS DATE) ASC
                  , F.[Call Date] ASC
) rawquery 
--where
Order By YourColumnHere