4

我在 MS SQL Management Studio 中为 SQL Server 2012 创建了一个查询,该查询使用 common_table_expression 自加入表。它在 MS SQL Studio 中正常工作,但在 Codeigniter 中没有返回结果或错误。

查询:

WITH rows AS 
    (
    SELECT *, ROW_NUMBER() OVER (ORDER BY u.[PatientID], u.[CreateDate]) AS rn
    FROM (
        SELECT a.[PatientID], a.[BGValue], a.[CreateDate], a.[HospitalUnit], 'MSHA' as 'Hospital', h.[system_name], f.[facility_code], f.[facility_name], 'IV' as 'Treatment'
        FROM [Analytics].[dbo].[MSHA_IVTreatment] a

        JOIN [Analytics].[dbo].[MSHA_Patients] p
        ON p.[PatientID] = a.[PatientID]

        JOIN [Analytics].[dbo].[Facilities] f
        ON f.[facility_code] = p.[facility_code]

        JOIN [Analytics].[dbo].[Hospitals] h
        ON f.[hospital] = h.[hospital]

        WHERE a.[CreateDate] BETWEEN '2013-05-01' AND '2013-05-31'

        UNION ALL

        SELECT a.[PatientID], a.[BGValue], a.[CreateDate], a.[HospitalUnit], 'MSHA' as 'Hospital', h.[system_name], f.[facility_code], f.[facility_name], 'SubQ' as 'Treatment'
        FROM [Analytics].[dbo].[MSHA_SubQTreatment] a

        JOIN [Analytics].[dbo].[MSHA_Patients] p
        ON p.[PatientID] = a.[PatientID]

        JOIN [Analytics].[dbo].[Facilities] f
        ON f.[facility_code] = p.[facility_code]

        JOIN [Analytics].[dbo].[Hospitals] h
        ON f.[hospital] = h.[hospital]

        WHERE a.[CreateDate] BETWEEN '2013-05-01' AND '2013-05-31'
        ) u
    ) 
SELECT  mc.[PatientID], mc.[BGValue], mc.[CreateDate], mc.[Hospital], mc.[system_name] as 'System', mc.[facility_code], mc.[facility_name], mc.[HospitalUnit], DATEDIFF(second, mp.[CreateDate], mc.[CreateDate])/60 as 'Interval', mc.[Treatment]
FROM    rows mc
JOIN    rows mp
ON      mc.rn = mp.rn + 1 and mc.[PatientID] = mp.[PatientID]
ORDER BY mc.[CreateDate] ASC;

当我将其放入变量并尝试在 codeigniter 中检索结果时,没有返回任何内容:

$result = $this->db->query($query);

CI_DB_sqlsrv_result Object
(
[conn_id] => Resource id #30
[result_id] => Resource id #41
[result_array] => Array
    (
    )

[result_object] => Array
    (
    )

[custom_result_object] => Array
    (
    )

[current_row] => 0
[num_rows] => -1
[row_data] => 
)

我曾尝试将查询放入事务中,但得到了相同的结果。

任何见解都非常感谢。

4

1 回答 1

1

可能这对你有帮助-

;WITH [rows] AS
(
     SELECT
            a.[PatientID]
          , a.[BGValue]
          , a.[createdate]
          , a.[HospitalUnit]
          , Hospital = 'MSHA' 
          , h.[system_name]
          , f.[facility_code]
          , f.[facility_name]
          , u.Treatment
          , rn = ROW_NUMBER() OVER (ORDER BY u.[PatientID], u.[createdate]) 
     FROM (
          SELECT
                 PatientID
               , BGValue
               , createdate
               , HospitalUnit
               , Treatment = 'IV' 
          FROM dbo.MSHA_IVTreatment
          WHERE createdate BETWEEN '20130501' AND '20130531'

          UNION ALL 

          SELECT
                 PatientID
               , BGValue
               , createdate
               , HospitalUnit
               , Treatment = 'SubQ' 
          FROM dbo.MSHA_SubQTreatment
          WHERE createdate BETWEEN '20130501' AND '20130531'
     ) a
     JOIN [dbo].[MSHA_Patients] p ON p.[PatientID] = a.[PatientID]
     JOIN [dbo].[Facilities] f ON f.[facility_code] = p.[facility_code]
     JOIN [dbo].[Hospitals] h ON f.[Hospital] = h.[Hospital]
)
SELECT
      mc.[PatientID]
    , mc.[BGValue]
    , mc.[createdate]
    , mc.[Hospital]
    , [System] = mc.[system_name]
    , mc.[facility_code]
    , mc.[facility_name]
    , mc.[HospitalUnit]
    , [Interval] = DATEDIFF(SECOND, mp.[createdate], mc.[createdate]) / 60
    , mc.[Treatment]
FROM [rows] mc
JOIN [rows] mp ON mc.rn = mp.rn + 1 AND mc.[PatientID] = mp.[PatientID]
ORDER BY mc.[createdate];
于 2013-07-18T05:57:33.947 回答